C#调用Delphi写的DLL,报“尝试读取或写入受保护的内存”错误,请各位朋友指点!
我C#程序里面调用我自己用Delphi写的DLL,调用InitReader这个函数正常,但调用RWCom这个函数后就报“尝试读取或写入受保护的内存”错误,请各位朋友指教!代码如下:[DllImport("ExportMeter.dll")]
public static extern bool InitReader(string ComName, int Rate, int ParityBit, int iIntervalTimeout, int iDelay);
[DllImport("ExportMeter.dll")]
public static extern string RWCom(string ComData, bool bAns);
private string SendReceivedData(string strCmd, string strMsg, bool bBcc)
{
string strReceivedData = "";
string strReturnData = "";
byte[] ASendData = GetSendByteArray(strCmd, strMsg, bBcc);
string strSendData = pf.ByteArrayToHexString(ASendData, false);
//向串口发送数据
bool bRevDataOK = false;
byte[] AReceivedData = null;
if (ASendData[0] == 6) //发送数据后立即关闭串口更改波特率再打开串口接收数据
{
strReturnData = RWComUpdBaudrate(strSendData, SerialParam[0], int.Parse(SerialParam[0]), 2, 100, 3000);
if (!strReturnData.Equals(""))
{
AReceivedData = pf.HexStringToByteArray(strReturnData);
bRevDataOK = true;
}
}
else
{
strReturnData = RWCom(strSendData, true);
if (!strReturnData.Equals(""))
{
AReceivedData = pf.HexStringToByteArray(strReturnData);
bRevDataOK = true;
}
}
if (bRevDataOK) //读取数据成功
{
strReceivedData = Encoding.ASCII.GetString(AReceivedData);
strReceivedData = strReceivedData.ToUpper();
ShowReceiveData(strReceivedData);
}
return strReceivedData;
}
执行strReturnData = RWCom(strSendData, true);这行时就报“尝试读取或写入受保护的内存”错误
DLL中RWCom这个函数的定义是function RWCom(ComData: String; bAns: Boolean): String; stdcall;且这个函数没有任何问题,为何会报这个错,我都快疯了。调用InitReader这个函数正常。跪请各位大侠帮忙看一下,谢谢!