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

发布于 2024-12-20 14:56:53 字数 1291 浏览 0 评论 0原文

我正在开发一个需要实现非托管 Windows DLL 的项目。 DLL 用于与 USB 设备通信。我的代码是 C# 和 WPF 语言。 为了初始化 DLL,我调用一个名为: 的函数:

InitTimerDll(Int32 wHandle, ref dllInitParams initParams);

调用此函数时,我必须传递一个名为 dllInitParams 的结构以及控件绑定到的句柄。 我使用 DllImport 作为函数指针:

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

这是我的结构:

public struct dllInitParams
{
    public UInt16 simp;

    public UInt16 simt;
}

以上所有内容都在一个名为 myDllInterface.cs 的单独类中。以下是我如何从 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);
}

上面代码的第一部分解释了如何获取句柄,initTime_Click 显示了如何初始化该结构,通过将句柄和结构传递给它来调用 initTimeDll 函数。我已将 dll 文件复制到代码运行的目录中。我的代码编译得很好,但当我单击 initTime 按钮时,它会产生错误。 错误:

ProbeCTRL.exe 中发生类型为“System.AccessViolationException”的未处理异常

附加信息:尝试读取或写入受保护的内存。这通常表明其他内存已损坏。

为什么会发生这种情况?

I am working on a project that requires implementing am unmanaged windows DLL. The DLL is used to communicate with a USB device. My code is in C# and WPF.
To initialize the DLL I call a function called:

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.
I am using DllImport for function pointer as such:

[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. Here is how I call the InitTimerDll function from my WPF form:

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. 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.
Error:

An unhandled exception of type 'System.AccessViolationException' occurred in ProbeCTRL.exe

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Why is this happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巴黎盛开的樱花 2024-12-27 14:56:53

在不确切知道 InitTimerDll() 函数对“this”指针执行什么操作的情况下,我将重点关注 params 结构。尝试添加如下所示的结构布局标记:

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

此外,请仔细检查您的结构是否完整且准确。

Without knowing exactly what the InitTimerDll() function does with the 'this' pointer, I would focus on the params structure. 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.

戏舞 2024-12-27 14:56:53

我发现了问题。代码没问题,问题是 dll 文件已损坏。 dll 文件的正确副本可以解决该问题。在代码中使用 dll 时,确保拥有准确的信息、函数调用、要传递的数据类型等非常重要。
感谢大家的帮助。

I found the problem. The code is fine the problem was the dll file, which was corrupted. A proper copy of the dll file took care of the problem. 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.
Thanks everyone for your help.

一场信仰旅途 2024-12-27 14:56:53

查看 PInvoke 教程: http:// /msdn.microsoft.com/en-us/library/aa288468%28v=vs.71%29.aspx

正如 Jim Gomes 指出的:

[StructLayout(LayoutKind.Sequential)]

或类似的东西肯定很重要。

另外,您只是初始化结构中的变量之一。

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

as Jim Gomes points out:

[StructLayout(LayoutKind.Sequential)]

or something similar is definitely important.

Also, you're only initializing one of the variables in your struct.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文