日期:2014-05-20  浏览次数:20827 次

如何在C#中调用C++写的DLL文件?
如题

------解决方案--------------------
就像调用系统API的一样来调用就行了.比如:
[DllImport( "SystemDll.dll ", EntryPoint = "GetMousePosition ", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern bool InternalGetMousePosition(UIntPtr wparam, IntPtr lparam, ref int x, ref int y);

------解决方案--------------------
使用DllImportAttribute
------解决方案--------------------
SystemDll可以放到系统目录下或当前程序执行目录下,或被注册就可以了.
------解决方案--------------------
举一个例子:
假设这两端代码在同一个类中


step 1
[DllImport( "my.dll ", CharSet=CharSet.Auto)]
public static extern int test(int num, string text, uint type);


step 2:
using System.Runtime.InteropServices;

test(0, "Hello!!! ", 0);

------解决方案--------------------
先导入名空间
using System.Runtime.InteropServices;

然后导入DLL
[DllImport( "test.dll ")]
private static extern int samplefun();

最后调用
int a=sample();
------解决方案--------------------
还要注意调用方法的时候,参数数据类型的对应。尤其是指针部分的对应和处理。