C#调用C++动态链接库方法介绍
当VC等调用C#写的COM时,用regasm /tlb生成TLB文件,也可用tlbexp.exe,在VC等中加载TLB文件,当用C#调用VC等写的COM时,用tlbimp.exe,你可以写一个程序调试一下
下面介绍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++动态链接库了
相关推荐
chensen 2020-11-14
leihui00 2020-09-16
二十不悔三十而立 2020-08-19
shining0 2020-08-02
TyrionZK 2020-07-26
TreasureZ 2020-07-26
natloc 2020-07-19
Bonrui编程路 2020-07-18
TyrionZK 2020-07-18
TyrionZK 2020-07-04
TreasureZ 2020-06-25
TreasureZ 2020-06-20
TreasureZ 2020-06-16
jameszgw 2020-06-14
Bonrui编程路 2020-06-13
Bonrui编程路 2020-06-07