當VC等調用C#寫的COM時,用regasm /tlb生成TLB文件,也可用tlbexp.exe,在VC等中加載TLB文件,當用C#調用VC等寫的COM時,用tlbimp.exe,你能夠寫一個程序調試一下函數
下面介紹C#調用C++動態連接庫方法。spa
添加System.Runtime.InteropServices命名空間調試
如是COM就直接用靜態函數調用: code
- 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 orm
不能直接用 htm
只能在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++動態連接庫了 rem
- [DllImp
ort( "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++寫的動態連接庫的問題。get