C#调用C++DLL时参数该如何对应

问题描述

如题,原来的程序是用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)

时间: 2024-12-29 21:34:06

C#调用C++DLL时参数该如何对应的相关文章

c# 调用 c++dll 时c#的代码怎么写

问题描述 c# 调用 c++dll 时c#的代码怎么写 #include "stdlib.h " #include #include #include #include "windows.h" #include #define m_col 3 #define m_dim 3 void InvMatrix(const double M[][m_col], const int n, double invM[][m_col]); void LinearFit(const

c#调用c++dll时const int*类型参数应该怎么对应

问题描述 如题,C++里原型为:intpSimDataWrite(intmodelID,intopt,intnID,constchar*szData,constintnDataLen);intpSimDataTake(intmodelID,intopt,intnID,char*szData,unsignedintnDataLen,constunsignedintnDataSliceNumberMax,unsignedint*nDataSliceNumber);两个函数的参数里都有const,查了

调用C++DLL时遇到结构体类型难题

问题描述 C++DLL里的一个函数:HRESULTDSStream_EnumVideoCompressor(VideoCompressorInfo*pInfo,int*piVidCompNum)得到系统中安装的视频压缩算法的信息.pInfo:指向一片预先分配的VideoCompressorInfo结构.如果为NULL,piVidCompNum将得到系统中安装的视频压缩算法的数目.piVidCompNum:如果pInfo不为NULL,piVidCompNum表示pInfo中包含多少个VideoCo

C#调用VC DLL 输出参数 急!!!

问题描述 被调dll函数ykt_readopencardno_local(char*pRegion,char*pDeptid,char*pDeptKey,char*pCardtype,char*pCardno,char*pWarnmsg,char*pErrmsg);C#调用代码:[DllImport("kernel32.dll")]privateexternstaticIntPtrLoadLibrary(Stringpath);[DllImport("kernel32.dll

求助,C#调用C++dll时提示:尝试读取或写入受保护的内存。这通常指示其他内存已损坏。

问题描述 C++dll的头文件接口描述如下:#pragmapack(push,1)typedefstructXCtrlStatus{XCtrlStatus(unsignedlongV=0){*reinterpret_cast<unsignedlong*>(this)=V;}unsignedcharERR:1;unsignedcharAUTO:1;unsignedcharRUN:2;unsignedcharDIR:2;unsignedchar:1;unsignedchar:1;unsignedc

VB.Net 调用C++ DLL时涉及到回调函数的问题

问题描述 问题描述:C/C++中的回调函数的定义如下:typedefint(*STREAM_DIRECT_READ_CALLBACK)(ULONGchannelNumber,void*DataBuf,DWORDLength,intFrameType,void*context);我在VB.Net中定义的回调函数如下:PublicDelegateFunctionSTREAM_DIRECT_READ_CALLBACK(ByValchannelNumberAsUInt32,ByValDataBufAsI

调用“过程名”时参数个数或类型错误 第1行 第7列

问题描述 客户端代码:try{i=WebSvr.svr.CHECK05DOCPLU(docno,textBox1.Text.Trim(),outqty,outerrmsg);}catch(ExceptionE1){Program.MsgDlgOK(E1.Message);return;}客户端存根注册:publicintCHECK05DOCPLU(stringp_docno,stringp_bcd,outintp_qty,outstringerrmsg){object[]results=this

部署在server 2008 iis7.0的WebServices方法调用win32.dll时出错

问题描述 部署在iis7.0的WebServices的一个方法调用了win32.dll一旦调用这个方法应用程序池也关闭!页面出现无法访问!但是在部署在xpiis5.0的同样的WebServices就可以正常访问.是不是权限问题还是IIS7.0配置问题!是不是iis7.0的安全机制默认禁止调用win32非托管类库.如果是如何配置..请教高手................................. 解决方案

C#调用C++dll传入参数后位数丢失

问题描述 publicstructRequest{[MarshalAs(UnmanagedType.ByValTStr,SizeConst=12)]publicstringTransAmount;}SPOS.Requestres=newSPOS.Request();res.TransAmount="000000000012";intret=SPOS.MapService(refres);代码中定义了TransAmount为12位,传入的也是12位,但是跟踪发现,最后调用MapServi