C#调用C++dll函数出现“尝试读取或写入受保护的内存”
c++函数代码:extern "C" __declspec(dllexport)
void RemoveByte(BYTE* inData,BYTE* outData,struct_Package &vStructPackageP){
int i=0,j=0,k=0,m=0;
struct_Package* vStructPackage=&vStructPackageP;
for(i=1;i<33;i++){
if(OppositeTurnByte(inData[i],inData[i+1],outData+j)){
i++;
}
j++;
if(j==12){
vStructPackage->messageId=(WORD)(outData[0]<<8);
vStructPackage->messageId=vStructPackage->messageId|(WORD)outData[1];
vStructPackage->cutPackage=(outData[2]&0x20)>>5;
vStructPackage->rsaMode=(outData[2]&0x1c)>>2;
vStructPackage->messageContentLength=(WORD)(outData[2]&03<<8);
vStructPackage->messageContentLength=vStructPackage->messageContentLength|((WORD)(outData[3]));
vStructPackage->phoneNumber[0]=outData[4];
vStructPackage->phoneNumber[1]=outData[5];
vStructPackage->phoneNumber[2]=outData[6];
vStructPackage->phoneNumber[3]=outData[7];
vStructPackage->phoneNumber[4]=outData[8];
vStructPackage->phoneNumber[5]=outData[9];
vStructPackage->messageNumber=(WORD)(outData[10]<<8);
vStructPackage->messageNumber=vStructPackage->messageNumber|((WORD)outData[11]);
if(vStructPackage->cutPackage==0)//不分包
{
for(k=i+1;k<=i+(vStructPackage->messageContentLength+1)*2;k++){
if(OppositeTurnByte(inData[k],inData[k+1],outData+j)){
k++;
}
j++;
if(j==vStructPackage->messageContentLength+13){
for(m=0;m<vStructPackage->messageContentLength;m++){
vStructPackage->messageContent[m]=outData[12+m];
if(vStructPackage->rsaMode==1)
{
//消息体解密
}
}
vStructPackage->checkCode=outData[12+vStructPackage->messageContentLength];
vStructPackage->checkCode=OppositeXorByte(vStructPackage->checkCode,outData,vStructPackage->messageContentLength+12);//反异或
return;
}
}
}else{ //分包
for(k=i+1;k<=i+(vStructPackage->messageContentLength+5)*2;k++){
if(OppositeTurnByte(inData[k],inData[k+1],outData+j)){
k++;
}
j++;
if(j==vStructPackage->messageContentLength+17){
vStructPackage->packageCount=(WORD)(outData[12]<<8);
vStructPackage->packageCount=vStructPackage->packageCount|((WORD)outData[13]);
vStructPackage->packageNumber=(WORD)(outData[14]<<8);
vStructPackage->packageNumber=vStructPackage->packageNumber|((WORD)outData[15]);
for(m=0;m<vStructPackage->messageContentLength;m++){
vStructPackage->messageContent[m]=outData[16+m];
if(vStructPackage->rsaMode==1)
{
//消息体解密
}
}
vStructPackage->checkCode=outData[16+vStructPackage->messageContentLength];
vStructPackage->checkCode=OppositeXorByte(vStructPackage->checkCode,outData,vStructPackage->messageContentLength+16);//反异或
return;
}
}
}
}
}
}
C#代码:
private void button1_Click(object sender, EventArgs e)
{
byte[] inData=new byte[18]{0x7e,0x00,0x01,0x00,0x03,0x01,0x02,0x03,0x04,0x05,0x06,0x00,0x02,0x01,0x02,0x03,0x00,0x7e};
byte[] outData=new byte[1024];
struct_Package v_Struct=new struct_Package();
RemoveByte(inData, outData, ref v_Struct);
}
问题:c++ RemoveByte函数代码调用过程中未出现异常,在RemoveByte(inData, outData, ref v_Struct)函数返回后,出现“尝试读取或写入受保护的内存。这通常指示其他内存已损坏” 不知道是inData、outData的原因,还是v_Struct结构体的缘故。
inData和outData只是我传入的两个参数而已,我最终想返回的还是v_Struct结构体。 C# C++ --------------------编程问答-------------------- 坐等前辈指教。
补充:.NET技术 , C#