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

发布时间:2021-07-15 14:30:17 作者:chen
来源:亿速云 阅读:273

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

当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++动态链接库这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. C#调用C++ 动态链接库dll
  2. C# 调用C++接口

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++

上一篇:ASP.NET Core 2.0中MemoryCache如何使用

下一篇:ASP.NET Core中间件如何实现分布式 Session

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》