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就直接用静态函数调用:                  

  1. public   static   int   GetNum(     
  2.                           int   lFileSeqNo,     
  3.                           string   sExtType,     
  4.                           string   sExtNumber,     
  5.                           string   sFormID,     
  6.                           string   sOperationDate,     
  7.                           string   sSystemRegistDate,     
  8.                           out   int   lCount,     
  9.                           out   int   lErrorType,     
  10.                           out   int   lErrorCode)     
  11.                   {     
  12.                           int   iRet;     
  13.       
  14.                           WOBCom.ObjClass   obj   =   new   WOBCom.ObjClass();     
  15.                               
  16.                           iRet   =   obj.GetNum(     
  17.                                   lFileSeqNo,     
  18.                                   sExtType,     
  19.                                   sExtNumber,     
  20.                                   sFormID,     
  21.                                   sOperationDate,     
  22.                                   sSystemRegistDate,     
  23.                                   out   lCount,     
  24.                                   out   lErrorType,     
  25.                                   out   lErrorCode);     
  26.       
  27.                           return   iRet;     
  28.                   }     

如不使COM是普通的DLL  

不能直接用  

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

  1. extern   "C"   __declspec(dllexport)   WOExtConRegObj*   OutGetObjConstructor();     
  2. extern   "C"   __declspec(dllexport)   void   OutGetObjDestructor(WOExtConRegObj*   outGetObj);     
  3.       
  4. extern   "C"   __declspec(dllexport)   long   SelectDummyRecord(long   *lErrorType,     
  5.         long   *lErrorCode,     
  6.         WOExtConRegObj*   outGetObj);     
  7. //     
  8. extern   "C"   __declspec(dllexport)   WOExtConRegObj*   OutGetObjConstructor()     
  9. {     
  10.           WOExtConRegObj*   outGetObj   =   new   WOExtConRegObj();        
  11.           return   outGetObj;     
  12. }     
  13.       
  14. extern   "C"   __declspec(dllexport)   void   OutGetObjDestructor(WOExtConRegObj*   outGetObj)     
  15. {     
  16.           delete   outGetObj;     
  17. }     
  18.       
  19. extern   "C"   __declspec(dllexport)   long   SelectDummyRecord(long   *lErrorType,     
  20.         long   *lErrorCode,     
  21.         WOExtConRegObj*   outGetObj)     
  22. {     
  23. return   outGetObj->SelectDummyRecord(lErrorType,     
  24. lErrorCode);         
  25. }     

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

  1. [DllImport("XXX.dll", EntryPoint="SelectDummyRecord", ExactSpelling=false, CallingConvention=CallingConvention.Cdecl)]     
  2.  private   static   extern   int   SelectDummyRecord(out int lErrorType,out int lErrorCode,int outGetObj);     
  3.  
  4.  ///   < summary>     
  5.  ///   < /summary>     
  6.  ///   < remarks>     
  7.  ///   < /remarks>                     
  8.  ///   < param name="lErrorType">< /param>     
  9.  ///   < param name="lErrorCode">< /param>     
  10.  ///   < returns>< /returns>     
  11.  public int SelectDummyRecord(out int lErrorType,out int lErrorCode)     
  12.  {     
  13.          int   intRtn;     
  14.  
  15.          intRtn   =   SelectDummyRecord(     
  16.                  out   lErrorType,     
  17.                  out   lErrorCode,     
  18.                  m_OutGetObj);       
  19.          return   intRtn;     
  20.  }    

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

【编辑推荐】

  1. C#程序中的数据显 示:自定义标签和XML、XSL
  2. C#自定义事件是如何生成的
  3. C# 自定义控件dll文件的生成步骤
  4. C#自定义快捷键的实现
  5. C#自定义事件的步骤介绍
责任编辑:book05 来源: hi.baidu
相关推荐

2024-03-01 20:59:11

C#DLL开发

2011-05-18 17:15:45

2009-08-28 16:19:30

C#实现修改动态链接库

2010-02-01 17:37:35

C++调用C链接库

2012-01-06 10:25:50

JavaDLLC++

2021-09-01 05:11:13

C# 动态链接库

2023-03-15 15:58:11

Python动态库C++

2011-04-08 09:52:44

C++C#DLL

2009-08-24 18:09:13

C#调用Oracle数

2023-05-09 08:24:11

JNA链接库代码

2009-09-17 18:14:05

C#动态数组

2023-08-02 10:10:00

C#C++

2009-08-20 12:29:46

C#调用PInvoke

2009-08-07 16:10:20

C#调用API

2009-08-25 14:42:41

由C++转向C#

2009-08-31 18:05:14

C#调用WalkTre

2009-08-31 16:33:28

C#调用Dispose

2023-11-29 08:31:20

PythonRust

2009-08-11 14:26:56

C#动态调用WebSe

2011-06-21 18:02:14

Qt 动态 链接库
点赞
收藏

51CTO技术栈公众号