简体   繁体   English

从C#WPF应用程序调用非托管Windows DLL时出错

[英]Error calling an unmanaged windows DLL from a C# WPF application

I am working on a project that requires implementing am unmanaged windows DLL. 我正在开发一个需要实现非托管Windows DLL的项目。 The DLL is used to communicate with a USB device. DLL用于与USB设备通信。 My code is in C# and WPF. 我的代码是在C#和WPF中。 To initialize the DLL I call a function called: 要初始化DLL,我调用一个名为的函数:

InitTimerDll(Int32 wHandle, ref dllInitParams initParams);

When calling this function I have to pass a struct called dllInitParams and the Handle that the control is bound to. 调用此函数时,我必须传递一个名为dllInitParams的结构和控件绑定的句柄。 I am using DllImport for function pointer as such: 我正在使用DllImport作为函数指针:

[DllImport("myDll.dll")]
public static extern void InitTimerDll(Int32 wHandle, ref dllInitParams initParams);

Here is my struct: 这是我的结构:

public struct dllInitParams
{
    public UInt16 simp;

    public UInt16 simt;
}

All of the above are in a separate class called myDllInterface.cs. 以上所有都在一个名为myDllInterface.cs的单独类中。 Here is how I call the InitTimerDll function from my WPF form: 以下是我从WPF表单调用InitTimerDll函数的方法:

public IntPtr Handle
{
    get { return (new System.Windows.Interop.WindowInteropHelper(this)).Handle; }
}

private void initTime_Click(object sender, RoutedEventArgs e)
{   
    myDllInterface.dllInitParams initParams = new myDllInterface.dllInitParams();
    initParams.simp = 0;
    myDllInterface.InitTimerDll(this.Handle.ToInt32(), ref initParams);
}

The first part of the above code explains how I get the handle and the initTime_Click shows how I initialize the struct, call the initTimeDll function by passing the handle and the struct to it. 上面代码的第一部分解释了我如何获取句柄,initTime_Click显示了我如何初始化结构,通过将句柄和结构传递给它来调用initTimeDll函数。 I have copied the dll file in the directory that the code runs in. My code compiles just fine but it creates an error when I click on the initTime button. 我已经在代码运行的目录中复制了dll文件。我的代码编译得很好,但是当我点击initTime按钮时会产生错误。 Error: 错误:

An unhandled exception of type 'System.AccessViolationException' occurred in ProbeCTRL.exe ProbeCTRL.exe中发生了未处理的“System.AccessViolationException”类型异常

Additional information: Attempted to read or write protected memory. 附加信息:尝试读取或写入受保护的内存。 This is often an indication that other memory is corrupt. 这通常表明其他内存已损坏。

Why is this happening? 为什么会这样?

Without knowing exactly what the InitTimerDll() function does with the 'this' pointer, I would focus on the params structure. 如果不确切知道InitTimerDll()函数对'this'指针的作用,我会专注于params结构。 Try adding a structure layout markup like the following: 尝试添加如下结构布局标记:

[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct dllInitParams
{
    public UInt16 simp;
    public UInt16 simt;
}

Also, double check that your structure is complete and accurate. 另外,仔细检查您的结构是否完整和准确。

I found the problem. 我发现了这个问题。 The code is fine the problem was the dll file, which was corrupted. 代码很好,问题是dll文件,它已损坏。 A proper copy of the dll file took care of the problem. dll文件的正确副本解决了这个问题。 When using dll in your codes it is quite important to make sure you have accurate information, function calls, data types to passed and so on. 在代码中使用dll时,确保准确的信息,函数调用,传递的数据类型等非常重要。 Thanks everyone for your help. 谢谢大家的帮助。

Have a look at the PInvoke tutorial: http://msdn.microsoft.com/en-us/library/aa288468%28v=vs.71%29.aspx 看一下PInvoke教程: http//msdn.microsoft.com/en-us/library/aa288468%28v=vs.71%29.aspx

as Jim Gomes points out: 正如Jim Gomes所指出的那样:

[StructLayout(LayoutKind.Sequential)]

or something similar is definitely important. 或类似的东西绝对重要。

Also, you're only initializing one of the variables in your struct. 此外,您只是初始化结构中的一个变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM