C#调用C++的DLL模块

上一篇 / 下一篇  2008-04-08 18:24:40 / 个人分类:c++

主要参考了两篇文章《C#中简单调用c/c++旧模块》 和《基于Visual C++6.0DLL编程实现》,复杂的理论和概念就不说了,简略地写一个实现的全过程吧:

1、在Visual Studio 6.0中用C++语方创建dll文件。

新建工程时选择”Win32 Dynamic-link library ”,在MyDll.h文件中按如下方式声明函数:

#define LIBEXPORT_API extern "C" __declspec(dllexport)

LIBEXPORT_API int Max(int a, int b);

MyDll.cpp中实现这个函数。

生成MyDll.dllMyDll.lib

2、在Visual C# .net中引用dll文件

MyDll.dllMyDll.lib拷贝到可执行文件目录下。

添加引用using System.Runtime.InteropServices;

按如下方式声明一个将要引用MyDll.dll中函数的类:

  public class RefComm
  {
  [DllImport("LibEncrypt.dll",
   EntryPoint="Max",           // 注意:引号内必须为完整的函数名,不能有空格
   CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
   public static extern int Max (int a,int b);
  }

使用如下语句调用:

int iSum = RefComm.Max(2,3);  

 

引用:

http://www.alixixi.com/Dev/Web/ASPNET/aspnet3/2007/2007050734689.html

http://www.itpub.net/archiver/tid-825811.html

http://www.vckbase.com/document/viewdoc/?id=1370

http://blog.csdn.net/notno/archive/2006/07/17/932335.aspx

http://gongyuzhuo.blog.163.com/blog/static/36304420076311305926/

http://www.pcvz.com/Program/Programs/CCC/CCCsystem/Program_98258.html

http://blog.csdn.net/pansiom/archive/2006/01/01/568096.aspx

http://blog.chinaunix.net/u/18297/showart_296028.html


TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar