Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1039176
  • 博文数量: 264
  • 博客积分: 6005
  • 博客等级: 大校
  • 技术积分: 2798
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-08 20:15
文章分类

全部博文(264)

文章存档

2011年(42)

2010年(213)

2009年(4)

2008年(2)

2007年(3)

分类:

2010-08-03 18:22:04

当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++写的动态链接库的问题。

阅读(4630) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~