在 .net 中使用非托管代码

发布于 2024-07-22 04:27:55 字数 1736 浏览 3 评论 0原文

案例:

有一个 .net 应用程序调用非托管 C 代码。 使用方法:

public static class MiracleCreator
{
    [DllImport("Library.dll")]
    private static extern void RunUnmanaged(string fileName);

    public static void Run(string fileName)
    {
        RunUnmanaged(fileName);
    }
}

在 Windows 窗体应用程序中使用,并通过 OpenFileDialog 获取所需的文件名。 代码:

if (openFileDialog.ShowDialog() == DialogResult.OK)
{
    MiracleCreator.Run(openFileDialog.FileName);
}

问题:

在 Windows 窗体应用程序中多次执行代码后,openFileDialog 出现异常:“尝试读取或写入受保护的内存。这通常表明其他内存已损坏。” >

在寻找解决方案时:

尝试“更可靠”地使用 OpenFileDialog 没有帮助。 像这个解决方案一样(试图给出链接,但是“不允许新用户添加超链接”:)):

public class Invoker
{
    public OpenFileDialog InvokeDialog;
    private Thread InvokeThread;
    private DialogResult InvokeResult;

    public Invoker()
    {
        InvokeDialog = new OpenFileDialog();
        InvokeThread = new Thread(new ThreadStart(InvokeMethod));
        InvokeThread.SetApartmentState(ApartmentState.STA);
        InvokeResult = DialogResult.None;
    }

    public DialogResult Invoke()
    {
        InvokeThread.Start();
        InvokeThread.Join();
        return InvokeResult;
    }

    private void InvokeMethod()
    {
        InvokeResult = InvokeDialog.ShowDialog();
    }
}

用法:

        Invoker I = new Invoker();

        if (I.Invoke() == DialogResult.OK)
        {
            MessageBox.Show(I.InvokeDialog.FileName, "Test Successful.");
        }
        else
        {
            MessageBox.Show("Test Failed.");
        }

问题:

异常真的是由非托管代码引起的吗? 是否会出现其他可能的问题(破坏与 OpenFileDialog 不同的东西)? 对此更好的方法是什么?

感谢您的每一个想法/解决方案。

The case:

There is a .net application calling unmanaged C code. Used method for this:

public static class MiracleCreator
{
    [DllImport("Library.dll")]
    private static extern void RunUnmanaged(string fileName);

    public static void Run(string fileName)
    {
        RunUnmanaged(fileName);
    }
}

It is used in a Windows Forms application and the needed file name is obtained by OpenFileDialog. Code:

if (openFileDialog.ShowDialog() == DialogResult.OK)
{
    MiracleCreator.Run(openFileDialog.FileName);
}

The problem:

After several executions of the code in the Windows Forms application the openFileDialog gets broken with exception: "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

In searching for solution:

Trying "more reliable" use of OpenFileDialog doesn't help. Like this solution (tried to give the link, but "new users aren't allowed to add hyperlinks" :) ):

public class Invoker
{
    public OpenFileDialog InvokeDialog;
    private Thread InvokeThread;
    private DialogResult InvokeResult;

    public Invoker()
    {
        InvokeDialog = new OpenFileDialog();
        InvokeThread = new Thread(new ThreadStart(InvokeMethod));
        InvokeThread.SetApartmentState(ApartmentState.STA);
        InvokeResult = DialogResult.None;
    }

    public DialogResult Invoke()
    {
        InvokeThread.Start();
        InvokeThread.Join();
        return InvokeResult;
    }

    private void InvokeMethod()
    {
        InvokeResult = InvokeDialog.ShowDialog();
    }
}

Usage :

        Invoker I = new Invoker();

        if (I.Invoke() == DialogResult.OK)
        {
            MessageBox.Show(I.InvokeDialog.FileName, "Test Successful.");
        }
        else
        {
            MessageBox.Show("Test Failed.");
        }

Questions:

Is the exception really caused by the unmanaged code? Could other possible problems be expected (breaking something different from the OpenFileDialog)? What is the better approach for this?

Thank you for every idea/solution.

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

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

发布评论

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

评论(2

嘿咻 2024-07-29 04:27:55

您应该为方法声明的 string 参数指定适当的 MarshalAs 属性。 就像是:

[DllImport("Library.dll")]
private static extern void RunUnmanaged(
  [MarshalAs(UnmanagedType. ... )] string fileName);

You should specify the appropriate MarshalAs attribute for the string parameter of the method declaration. Something like:

[DllImport("Library.dll")]
private static extern void RunUnmanaged(
  [MarshalAs(UnmanagedType. ... )] string fileName);
只有一腔孤勇 2024-07-29 04:27:55

仅当从加载了任何数据的模态表单中公开 saveAs 时,通过 .NET Windows 窗体应用程序(在 Windows 7 32 位上运行)中的托管代码使用 SaveFileDialog 才会出现相同的错误来自数据库。 经过几个小时的调试和试验/错误后,我的目光落在了一个我以前没有意识到的属性上:在 VB.NET 中,我编写了代码

Dim sfv As New System.Windows.Forms.SaveFileDialog
   With sfv  
     .AutoUpgradeEnabled = False
     '[...] 

,错误就消失了。

Same errror using SaveFileDialog via managed code in a .NET Windows Forms application (running on Windows 7 32-bit) only when exposing the saveAs from a modal form with any data loaded from a db. After ugly hours of debug and trial/errors, my eyes fell on a property I did not realize was there before: in VB.NET, I wrote

Dim sfv As New System.Windows.Forms.SaveFileDialog
   With sfv  
     .AutoUpgradeEnabled = False
     '[...] 

and the error disappeared.

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