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

成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供靖西網(wǎng)站建設(shè)、靖西做網(wǎng)站、靖西網(wǎng)站設(shè)計、靖西網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、靖西企業(yè)網(wǎng)站模板建站服務(wù),10余年靖西做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
下面介紹C#調(diào)用C++動態(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++中加一個對外的接口:
- 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++動態(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++寫的動態(tài)鏈接庫的問題。
【編輯推薦】
- C#程序中的數(shù)據(jù)顯 示:自定義標(biāo)簽和XML、XSL
- C#自定義事件是如何生成的
- C# 自定義控件dll文件的生成步驟
- C#自定義快捷鍵的實現(xiàn)
- C#自定義事件的步驟介紹
網(wǎng)頁題目:C#調(diào)用C++動態(tài)鏈接庫方法介紹
當(dāng)前URL:http://fisionsoft.com.cn/article/coijopg.html


咨詢
建站咨詢
