using a;vs2003--工程--引用--右键--添加引用--选择你需要的c++的a.dll。例如
VC++6写的DLL test.dll 用的StdCall模式[DllImport(@"d:\test.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
public static extern int getname(string StrPort , ref string e);
VC++6写的DLL test.dll 用的StdCall模式[DllImport(@"d:\test.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
public static extern int getname(string StrPort , ref string e);
现在可以写一个函数代码,在DllDemo.cpp文件中添加这些代码。也可以使用新的文件添加代码;
extern “C“ __declspec(dllexport) int Add(int a,int b)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
TRACE("Entering DllAdd\n"); return a+b;
}编译工程。下面建立一个C#的WinForm程序测试DllDemo:
启动vs.net,建立新的C#工程,选择WinForm应用程序。
在Form1.cs中添加引用:using System.Runtime.InteropServices;
在pulic class Form1声明的开头添加代码:
[DllImport("MotorControlDll.dll",EntryPoint="Add",ExactSpelling=false,CallingConvention=CallingConvention.Cdecl)]
public static extern int Add(int a,int b);至于DllImport属性的用法可以察看MSDN,对于各项参数有详细的说明。最后还要记得将DllDemo生成的位于Debug文件中DllDemo.dll文件加入到C#的WinForm程序的bin目录下。
这样就可以直接使用Add(int a,int b)函数。