问题描述
如题,原来的程序是用C++编写的,加载了DLL调用如下(可知DLL中的函数名和参数)typedefint(*pGetSubEngineID)(char*modelName,int*sID,double*sModelStepSize);typedefint(*pGetBaseStepSize)(double*StepSize);typedefint(*pInitDataBus)();typedefint(*pReleaseDataBus)();typedefint(*pSimDataWrite)(intmodelID,intopt,intnID,constchar*szData,constintnDataLen);typedefint(*pSimDataClear)(intmodelID,intopt,intnID);typedefint(*pSimDataRead)(intmodelID,intopt,intnID,char*szData,unsignedintnDataLen,constunsignedintnDataSliceNumberMax,unsignedint*nDataSliceNumber);typedefint(*pSimDataTake)(intmodelID,intopt,intnID,char*szData,unsignedintnDataLen,constunsignedintnDataSliceNumberMax,unsignedint*nDataSliceNumber);typedefint(*pSimDataTakeWithWaitCondition)(intmodelID,intopt,intnID,char*szData,unsignedintnDataLen,constunsignedintnDataSliceNumberMax,unsignedint*nDataSliceNumber,constunsignedlongnSec);现在的需求需要重新用C#改写,我写的如下:除了int的没问题之外,其他的都会报错说签名不对应,我怀疑是参数有问题,哪位大虾能给看一下,谢谢啊![DllImport("Master.dll")]publicstaticexternintGetSubEngineID(stringmodelName,refintsID,refdoublesModelStepSize);[DllImport("Master.dll")]publicstaticexternintGetBaseStepSize(refdoubleStepSize);[DllImport("Master.dll")]publicstaticexternintInitDataBus();[DllImport("Master.dll")]publicstaticexternintReleaseDataBus();[DllImport("Master.dll")]publicstaticexternintSimDataWrite(intmodelID,intopt,intnID,stringszData,intnDataLen);[DllImport("Master.dll")]publicstaticexternintSimDataClear(intmodelID,intopt,intnID);[DllImport("Master.dll")]publicstaticexternintSimDataRead(intmodelID,intopt,intnID,stringszData,uintnDataLen,uintnDataSliceNumberMax,refuintnDataSliceNumber);[DllImport("Master.dll")]publicstaticexternintSimDataTake(intmodelID,intopt,intnID,stringszData,uintnDataLen,uintnDataSliceNumberMax,refuintnDataSliceNumber);[DllImport("Master.dll")]publicstaticexternintSimDataTakeWithWaitCondition(intmodelID,intopt,intnID,stringszData,uintnDataLen,uintnDataSliceNumberMax,refuintnDataSliceNumber,ulongnSec);
解决方案
解决方案二:
解决方案三:
引用1楼wangnaisheng的回复:谢谢回帖,我也参考了类似的文章,但还是报函数签名不一致。我猜可能是在c++里的const参数引起的,但是C#里不能在参数上加Const啊,此处该如何处理?
解决方案四:
///ReturnType:int///modelName:char*///sID:int*///sModelStepSize:double*publicdelegateintpGetSubEngineID(System.IntPtrmodelName,refintsID,refdoublesModelStepSize);///ReturnType:int///StepSize:double*publicdelegateintpGetBaseStepSize(refdoubleStepSize);///ReturnType:intpublicdelegateintpInitDataBus();///ReturnType:intpublicdelegateintpReleaseDataBus();///ReturnType:int///modelID:int///opt:int///nID:int///szData:char*///nDataLen:intpublicdelegateintpSimDataWrite(intmodelID,intopt,intnID,[System.Runtime.InteropServices.InAttribute()][System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]stringszData,intnDataLen);///ReturnType:int///modelID:int///opt:int///nID:intpublicdelegateintpSimDataClear(intmodelID,intopt,intnID);///ReturnType:int///modelID:int///opt:int///nID:int///szData:char*///nDataLen:unsignedint///nDataSliceNumberMax:unsignedint///nDataSliceNumber:unsignedint*publicdelegateintpSimDataRead(intmodelID,intopt,intnID,System.IntPtrszData,uintnDataLen,uintnDataSliceNumberMax,refuintnDataSliceNumber);///ReturnType:int///modelID:int///opt:int///nID:int///szData:char*///nDataLen:unsignedint///nDataSliceNumberMax:unsignedint///nDataSliceNumber:unsignedint*publicdelegateintpSimDataTake(intmodelID,intopt,intnID,System.IntPtrszData,uintnDataLen,uintnDataSliceNumberMax,refuintnDataSliceNumber);///ReturnType:int///modelID:int///opt:int///nID:int///szData:char*///nDataLen:unsignedint///nDataSliceNumberMax:unsignedint///nDataSliceNumber:unsignedint*///nSec:unsignedintpublicdelegateintpSimDataTakeWithWaitCondition(intmodelID,intopt,intnID,System.IntPtrszData,uintnDataLen,uintnDataSliceNumberMax,refuintnDataSliceNumber,uintnSec)