问题描述
现在有一个C++的dll,原来是C++项目使用,现在有一个C#项目想使用这个DLL,调用这个dll的时候一些类型映射,不知道C++中的类型,应该映射成C#中的什么类型,该怎么写,请教大神。C++调用DLL的代码如下主要不知道下面这三种C++中的参数,用C#改写该映射成什么类型constTCHAR*sLogDirconststd::list<conststd::pair<LPCSTR,LPCSTR>>&listParamsParamPair*pPairs
reaper.htypedefint(*FP_INIT)(HANDLEhWnd,constTCHAR*sLogDir);typedefvoid(*FP_TRACK_EVENT)(constTCHAR*sCategory,constTCHAR*sAction,constTCHAR*sLabel,INTiValue,#ifndefUNICODEstd::list<std::pair<std::string,std::string>>&listParams);#elsestd::list<std::pair<std::wstring,std::wstring>>&listParams);#endiftypedefvoid(*FP_TRACK_EVENT_NEW)(constTCHAR*sCategory,constTCHAR*sAction,constTCHAR*sLabel,INTiValue,#ifndefUNICODEconststd::list<conststd::pair<LPCSTR,LPCSTR>>&listParams);#elseconststd::list<conststd::pair<LPCWSTR,LPCWSTR>>&listParams);#endiftypedefvoid(*FP_TRACK_EVENT2)(constTCHAR*sCategory,constTCHAR*sAction,constTCHAR*sLabel,INTiValue,intiPairCount,ParamPair*pPairs);classReaper{public:intinit(HANDLEhWnd,constTCHAR*sLogDir);voidtrackEvent(constTCHAR*sCategory,constTCHAR*sAction,constTCHAR*sLabel,INTiValue,#ifndefUNICODEstd::list<std::pair<std::string,std::string>>&listParams);#elsestd::list<std::pair<std::wstring,std::wstring>>&listParams);#endifvoidtrackEventNew(constTCHAR*sCategory,constTCHAR*sAction,constTCHAR*sLabel,INTiValue,#ifndefUNICODEconststd::list<conststd::pair<LPCSTR,LPCSTR>>&listParams);#elseconststd::list<conststd::pair<LPCWSTR,LPCWSTR>>&listParams);#endifvoidtrackEvent2(constTCHAR*sCategory,constTCHAR*sAction,constTCHAR*sLabel,INTiValue,intiPairCount,ParamPair*pPairs);}
reaper.cppFP_TRACK_EVENTReaper::_fpTrackEvent=NULL;FP_TRACK_EVENT_NEWReaper::_fpTrackEventNew=NULL;FP_TRACK_EVENT2Reaper::_fpTrackEvent2=NULL;boolReaper::initLib(){_fpInit=(FP_INIT)::GetProcAddress(_hLib,"initialize");_fpTrackEvent=(FP_TRACK_EVENT)::GetProcAddress(_hLib,"trackEvent");_fpTrackEventNew=(FP_TRACK_EVENT_NEW)::GetProcAddress(_hLib,"trackEventNew");_fpTrackEvent2=(FP_TRACK_EVENT2)::GetProcAddress(_hLib,"trackEvent2");}
C#中我现在做了如下,主要不知道类型到底该映射成什么样请指教Reaper.cspublicclassReaper{publicdelegateintFP_INIT(IntPtrhWnd,stringsLogDir);publicdelegatevoidFP_TRACK_EVENT(stringsCategory,stringsAction,stringsLabel,intiValue,Dictionary<string,string>listParams);publicdelegatevoidFP_TRACK_EVENT_NEW(stringsCategory,stringsAction,stringsLabel,intiValue,Dictionary<string,string>listParams);publicdelegatevoidFP_TRACK_EVENT2(stringsCategory,stringsAction,stringsLabel,intiValue,intiPairCount,Dictionary<string,string>listParams);publicboolinitLib(){DllWrapper_hLib=newDllWrapper();_hLib.Init("reaper.dll");FP_INIT_fpInit=(FP_INIT)_hLib.GetFunctionAddress("initialize",typeof(FP_INIT));FP_TRACK_EVENT_fpTrackEvent=(FP_TRACK_EVENT)_hLib.GetFunctionAddress("trackEvent",typeof(FP_TRACK_EVENT));FP_TRACK_EVENT_NEW_fpTrackEventNew=(FP_TRACK_EVENT_NEW)_hLib.GetFunctionAddress("trackEventNew",typeof(FP_TRACK_EVENT_NEW));FP_TRACK_EVENT2_fpTrackEvent2=(FP_TRACK_EVENT2)_hLib.GetFunctionAddress("trackEvent2",typeof(FP_TRACK_EVENT2));}}publicclassDllWrapper{privateIntPtrdllHnd;[DllImport("Kernel32")]publicstaticexternIntPtrGetProcAddress(IntPtrhandle,stringfuncname);[DllImport("Kernel32")]publicstaticexternIntPtrLoadLibrary(stringname);[DllImport("Kernel32")]publicstaticexternIntPtrFreeLibrary(IntPtrhandle);publicvoidInit(stringmoduleName){this.dllHnd=DllWrapper.LoadLibrary(moduleName);}publicDelegateGetFunctionAddress(stringfunctionName,Typet){IntPtrprocAddress=DllWrapper.GetProcAddress(this.dllHnd,functionName);returnMarshal.GetDelegateForFunctionPointer(procAddress,t);}publicvoidFinit(){DllWrapper.FreeLibrary(this.dllHnd);this.dllHnd=newIntPtr(0);}}
解决方案
本帖最后由 dudou681 于 2016-03-19 11:42:13 编辑
解决方案二:
三个统统能用IntPtr代替不过第二个要在c++那侧构造,c#只能作为中间人传递
解决方案三:
从C#调用其他语言的DLL时,参数只能是两边都支持的基本类型,如整数,字符串,字节数组,以及由此组成的结构体.pair泛型我看肯定不行,而且方法的参数必须是STDCALL约定
解决方案四:
谢谢楼上两位的回复我想知道具体一点的,不知道是否有大神可以指教下
解决方案五:
希望今天上班的人多,有大神可以帮助我解决