C#调用dll在win7,win10,VS2010下运行正常,但在winXP中出现"尝试读取或写入受保护的内存”,求助!
调用代码:
_res = IsRs485(_rs485Address) ?
Reader2800.ConnectScanner485(ref _mHScanner, com, baud, _rs485Address) :
Reader2800.ConnectScanner(ref _mHScanner, com, baud);
ConnectScanner方法:
// apiReturn _stdcall ConnectScanner(HANDLE *hScanner, char *szPort, int nBaudRate);
[DllImport(dllPath, EntryPoint = "ConnectScanner", SetLastError = true,
CharSet = CharSet.Ansi, ExactSpelling = false,
CallingConvention = CallingConvention.StdCall)]
public static extern ushort ConnectScanner(ref IntPtr hScanner, string szPort, int nBaudRate);
ConnectScanner485方法:
//apiReturn _stdcall ConnectScanner485(HANDLE *hScanner, char *szPort, int nBaudRate);
[DllImport(dllPath, EntryPoint = "ConnectScanner485", SetLastError = true,
CharSet = CharSet.Ansi, ExactSpelling = false,
CallingConvention = CallingConvention.StdCall)]
public static extern ushort ConnectScanner485(ref IntPtr hScanner, string szPort, int nBaudRate, int address);
参数不匹配,之前遇到过这样的问题。检查一下你的入参。或者是该dll不支持XP
方法参数不匹配
是哪一个参数?要如何匹配?
应该是该DLL 不支持XP
调用非c#的代码,调用了非托管的dll[DllImport(dllPath, EntryPoint = "ConnectScanner485", SetLastError = true,
CharSet = CharSet.Ansi, ExactSpelling = false,
CallingConvention = CallingConvention.StdCall)]
然后参数越界,导致内存访问失败。
第一 dll 不支持xp ,第二xp是否有参数或者配置不同。