bool __stdcall __declspec(dllexport) InitHW(char *name, char *model, int& type)
因此,我尝试的代码如下所示,它给出了system.AccessViolation异常:
[DllImport("extIO_IC7610.dll", CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern bool InitHW(string name, string model, int type);
private unsafe void Initialize()
{
try
{
bool result;
string name = "Test";
string model = "Model";
int type = 3;
result = InitHW(name, model, type);
}
catch (Exception ex)
{
}
}
有人能告诉我我在理解上的错误吗?
根据评论,我将事情更改为如下:
[DllImport("extIO_IC7610.dll", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.I1)]
public unsafe static extern bool InitHW(string name, string model, ref int type);
unsafe private void Initialize())
{
try
{
bool result;
string name = "";
string model = "";
int type = 3;
result = InitHW(name, model, ref type);
}
catch (Exception ex)
{
}
}
这仍然不起作用。我现在得到一个错误,堆栈是不平衡的,因为签名不匹配。我认为字符串是正确的,但是&int参数可能仍然是一个问题。
汤姆