C#调用DLL 转于(codeProject)

本文介绍如何使用Visual Studio创建一个简单的DLL文件,并通过C#应用程序来调用DLL中的函数。首先创建一个名为TestLib的DLL项目,然后编写一个输出'Hello from DLL!'的函数。接着创建一个C#项目,引入System.Runtime.InteropServices命名空间,使用DllImport属性引用DLL文件,最终实现从C#中调用DLL的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Introduction

After spending some time trying to implement this simple task, I started to search similar code examples over the Internet. I was really very surprised when found that all examples were slightly different from what I needed. Finally, I realized that there is no (at least spending 30 min in the net) easy-to-use example, that�s why I decided to write this article.

Assuming that you already know what a DLL is, let's begin with creating a simple one.

  1. Start Visual Studio .NET.
  2. Go to File->New->Project.
  3. Select Visual C++ Project, and from the 'Templates', select 'Win32 Project'.
  4. Give the name to your project. This will be the name of your final DLL (in my case: TestLib).
  5. Press OK.
  6. Select DLL from 'Application Type' ('Application Settings' tab).
  7. Check 'Empty Project' (we need to create our project from scratch, right?), and press Finish.

OK, now we should attach an empty source file to our blank project.

  1. Start Solution Explorer (if it's not displayed).
  2. Right click to the 'Source Files', Add->Add New Item then select 'C++ File' and give the name to it.
  3. Press 'Open'.

In the opened window, enter the following code:

Please note that __declspec(dllexport) is an obligatory prefix which makes DLL functions available from an external application.

extern 'C' (with brackets) is also very important, it shows that all code within brackets is available from 'outside'. Although code will compile even without this statement, during runtime, you'll get a very unpleasant error. So, do not forget to include it.

Build this application and your DLL will be ready-to-use.

Now it's time to create an application which will use our DLL, as the main title explains. We will create this type of application using Microsoft's C#.
Creating a simple C# application:

   1. Start Visual Studio .NET.
   2. Go to File->New->Project.
   3. Select Visual C# Project. From the 'Templates', you can either select 'Console Application', or an 'Empty Project' just like it was described above.
   4. Give the name to your application.
   5. Press OK.

Into the specified class, insert the following two lines:

In C#, keyword extern indicates that the method is implemented externally.

Your code should look something like this:

Please, note that System.Runtime.InteropServices is required for operations with the DLL.

According to MSDN:

    'The System.Runtime.InteropServices namespace provides a collection of classes useful for accessing COM objects, and native APIs from .NET'

OK, now build this application, and then copy the previously built DLL into the Debug/Release directory of the current application. The DLL should be in the same directory as your main application.

Now start an application. If everything you've made was correct, you should get something like this:

 

 

That's all, really simple isn't it?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值