调用dll时c#中的访问冲突错误
access violation error in c# when calling dll
在像这样的 Vb6 代码中:
Public Declare Sub PLCReadWord Lib "COM3964R.dll" Alias "plc_fetchword" _
(com As Long, _
Db As Long, _
Dw As Long, _
data As Long, _
RisOper As Long)
在 C# 端转换为:
[DllImport("..\..\dll\Com3964r.dll", EntryPoint = "plc_fetchword")]
public static extern void PLCReadWord(
int com,
int Db,
int Dw,
int data,
int RisOper);
使用 Visual Studio 2013 - Framework 3.5 - x86 模式编译。我的系统有 64 位 CPU 和 64 位 Windows 10.
当运行程序系统在以下代码处给出错误。我觉得myDummyData
和myDummyError
(都是Int32
类型)不能写成:
ClassPLC.PLCReadWord(Convert.ToInt32(txtCommPort.Text),
Convert.ToInt32(txtDbRead.Text),
Convert.ToInt32(txtDwRead.Text),
myDummyData, myDummyError);
VB6默认通过传递参数ByRef
。这对应于 C# 中的 ref
。将导入函数的 C# 声明中的所有参数更改为 ref
。
在像这样的 Vb6 代码中:
Public Declare Sub PLCReadWord Lib "COM3964R.dll" Alias "plc_fetchword" _
(com As Long, _
Db As Long, _
Dw As Long, _
data As Long, _
RisOper As Long)
在 C# 端转换为:
[DllImport("..\..\dll\Com3964r.dll", EntryPoint = "plc_fetchword")]
public static extern void PLCReadWord(
int com,
int Db,
int Dw,
int data,
int RisOper);
使用 Visual Studio 2013 - Framework 3.5 - x86 模式编译。我的系统有 64 位 CPU 和 64 位 Windows 10.
当运行程序系统在以下代码处给出错误。我觉得myDummyData
和myDummyError
(都是Int32
类型)不能写成:
ClassPLC.PLCReadWord(Convert.ToInt32(txtCommPort.Text),
Convert.ToInt32(txtDbRead.Text),
Convert.ToInt32(txtDwRead.Text),
myDummyData, myDummyError);
VB6默认通过传递参数ByRef
。这对应于 C# 中的 ref
。将导入函数的 C# 声明中的所有参数更改为 ref
。