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

8 篇文章 0 订阅

下面介绍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++写的动态链接库的问题。        

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值