问题描述
原函数typdedefstructtagParam{charPort[3];charCtrlID[3];charParams[1024];charIPPort[5];charIPAddr[15];}unsignedshortOpenComm(Params&lParam,intsType)如下为我转换后的[StructLayoutAttribute(LayoutKind.Sequential,CharSet=CharSet.Ansi,Pack=1)]privatestructParam{[MarshalAs(UnmanagedType.ByValTStr,SizeConst=3)]publicstringPort;[MarshalAs(UnmanagedType.ByValTStr,SizeConst=3)]publicstringCtrlID;[MarshalAs(UnmanagedType.ByValTStr,SizeConst=1024)]publicstringParams;[MarshalAs(UnmanagedType.ByValTStr,SizeConst=4)]publicstringIPPort;[MarshalAs(UnmanagedType.ByValTStr,SizeConst=15)]publicstringIPAddr;}privateshortsType=401;[DllImport("CM60.dll")]privatestaticexternshortOpenComm(refParampar,shortstype);publicshortOpen(){Parampar=newParam();par.Port="003";returnOpenComm(refpar,sType);}在调用Open()方法时一直不成功,请各位帮一下忙呀。不好意思没分了给我的点少哈
解决方案
解决方案二:
1、Param怎么定义的?
解决方案三:
同样碰到的问题,启发很大啊
解决方案四:
int32short16
解决方案五:
int还是int··
解决方案六:
提示啥错误?调用约定设置下试试
解决方案七:
我觉得应该是byte[]
解决方案八:
字符串后面会加个结尾,看看是不是这个原因
解决方案九:
[StructLayoutAttribute(LayoutKind.Sequential,CharSet=CharSet.Ansi,Pack=1)]publicstructParam{[MarshalAs(UnmanagedType.ByValArray,SizeConst=3)]publicbyte[]Port;[MarshalAs(UnmanagedType.ByValArray,SizeConst=3)]publicbyte[]CtrlID;[MarshalAs(UnmanagedType.ByValArray,SizeConst=6)]publicbyte[]Params;[MarshalAs(UnmanagedType.ByValArray,SizeConst=4)]publicbyte[]IPPort;[MarshalAs(UnmanagedType.ByValArray,SizeConst=15)]publicbyte[]IPAddr;}[DllImport("CM60.dll")]privatestaticexternshortOpenComm(refParampar,shortstype);[DllImport("CM60.dll")]privatestaticexternshortCloseComm(refParampar,shortstype);[DllImport("CM60.dll")]privatestaticexternshortSearchCtrl(refParampar,shortstype);[DllImport("CM60.dll")]privatestaticexternshortCollect20(refParampar,shortstype);[DllImport("CM60.dll")]privatestaticexternshortGetNewRecordCount(refParampar,shortstype);publicParampar=newParam();privateshortsType=100;privatevoidbtnReadRecord_Click(objectsender,EventArgse){par.Port=newbyte[]{0x30,0x30,0x31};par.CtrlID=newbyte[]{0x30,0x30,0x31};par.Params=newbyte[]{0x31,0x39,0x32,0x30,0x30,0x3B};shortsh=0;par.Port[2]=Convert.ToByte(48+Convert.ToInt16(cbPort.Text.Substring(3)));sh=OpenComm(refpar,sType);if(sh==105)MessageBox.Show("OK");elseMessageBox.Show(sh.ToString());sh=SearchCtrl(refpar,sType);if(sh==105)MessageBox.Show("OK");elseMessageBox.Show(sh.ToString());sh=CloseComm(refpar,sType);if(sh==105)MessageBox.Show("OK");elseMessageBox.Show(sh.ToString());}