资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C#怎么调用C++动态链接库

这篇文章主要讲解了“C#怎么调用C++动态链接库”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C#怎么调用C++动态链接库”吧!

成都创新互联于2013年开始,先为鼓楼等服务建站,鼓楼等地企业,进行企业商务咨询服务。为鼓楼企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

当VC等调用C#写的COM时,用regasm   /tlb生成TLB文件,也可用tlbexp.exe,在VC等中加载TLB文件,当用C#调用VC等写的COM时,用tlbimp.exe,你可以写一个程序调试一下

下面介绍C#调用C++动态链接库方法。

添加System.Runtime.InteropServices命名空间

如是COM就直接用静态函数调用:                  

public   static   int   GetNum(                               int   lFileSeqNo,                               string   sExtType,                               string   sExtNumber,                               string   sFormID,                               string   sOperationDate,                               string   sSystemRegistDate,                               out   int   lCount,                               out   int   lErrorType,                               out   int   lErrorCode)                       {                               int   iRet;                                     WOBCom.ObjClass   obj   =   new   WOBCom.ObjClass();                                                             iRet   =   obj.GetNum(                                       lFileSeqNo,                                       sExtType,                                       sExtNumber,                                       sFormID,                                       sOperationDate,                                       sSystemRegistDate,                                       out   lCount,                                       out   lErrorType,                                       out   lErrorCode);                                     return   iRet;                       }

如不使COM是普通的DLL  

不能直接用  

只能在C++中加一个对外的接口:  

extern   "C"   __declspec(dllexport)   WOExtConRegObj*   OutGetObjConstructor();     extern   "C"   __declspec(dllexport)   void   OutGetObjDestructor(WOExtConRegObj*   outGetObj);           extern   "C"   __declspec(dllexport)   long   SelectDummyRecord(long   *lErrorType,             long   *lErrorCode,             WOExtConRegObj*   outGetObj);     //     extern   "C"   __declspec(dllexport)   WOExtConRegObj*   OutGetObjConstructor()     {               WOExtConRegObj*   outGetObj   =   new   WOExtConRegObj();                  return   outGetObj;     }           extern   "C"   __declspec(dllexport)   void   OutGetObjDestructor(WOExtConRegObj*   outGetObj)     {               delete   outGetObj;     }           extern   "C"   __declspec(dllexport)   long   SelectDummyRecord(long   *lErrorType,             long   *lErrorCode,             WOExtConRegObj*   outGetObj)     {     return   outGetObj->SelectDummyRecord(lErrorType,     lErrorCode);         }

就可直接用C#调用C++动态链接库了   

[DllImport("XXX.dll", EntryPoint="SelectDummyRecord", ExactSpelling=false, CallingConvention=CallingConvention.Cdecl)]      private   static   extern   int   SelectDummyRecord(out int lErrorType,out int lErrorCode,int outGetObj);       ///   < summary>      ///   < /summary>      ///   < remarks>      ///   < /remarks>                      ///   < param name="lErrorType">< /param>      ///   < param name="lErrorCode">< /param>      ///   < returns>< /returns>      public int SelectDummyRecord(out int lErrorType,out int lErrorCode)      {              int   intRtn;               intRtn   =   SelectDummyRecord(                      out   lErrorType,                      out   lErrorCode,                      m_OutGetObj);                return   intRtn;      }

这样就解决了C#调用C++写的动态链接库的问题。

感谢各位的阅读,以上就是“C#怎么调用C++动态链接库”的内容了,经过本文的学习后,相信大家对C#怎么调用C++动态链接库这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!


新闻名称:C#怎么调用C++动态链接库
分享链接:http://cdkjz.cn/article/pdcego.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220