溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#怎么調(diào)用C++動(dòng)態(tài)鏈接庫

發(fā)布時(shí)間:2021-07-15 14:30:17 來源:億速云 閱讀:286 作者:chen 欄目:編程語言

這篇文章主要講解了“C#怎么調(diào)用C++動(dòng)態(tài)鏈接庫”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“C#怎么調(diào)用C++動(dòng)態(tài)鏈接庫”吧!

當(dāng)VC等調(diào)用C#寫的COM時(shí),用regasm   /tlb生成TLB文件,也可用tlbexp.exe,在VC等中加載TLB文件,當(dāng)用C#調(diào)用VC等寫的COM時(shí),用tlbimp.exe,你可以寫一個(gè)程序調(diào)試一下

下面介紹C#調(diào)用C++動(dòng)態(tài)鏈接庫方法。

添加System.Runtime.InteropServices命名空間

如是COM就直接用靜態(tài)函數(shù)調(diào)用:                  

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++中加一個(gè)對(duì)外的接口:  

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#調(diào)用C++動(dòng)態(tài)鏈接庫了   

[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#調(diào)用C++寫的動(dòng)態(tài)鏈接庫的問題。

感謝各位的閱讀,以上就是“C#怎么調(diào)用C++動(dòng)態(tài)鏈接庫”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)C#怎么調(diào)用C++動(dòng)態(tài)鏈接庫這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI