在 .net 中使用非托管代码
案例:
有一个 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该为方法声明的
string
参数指定适当的MarshalAs
属性。 就像是:You should specify the appropriate
MarshalAs
attribute for thestring
parameter of the method declaration. Something like:仅当从加载了任何数据的模态表单中公开
saveAs
时,通过 .NET Windows 窗体应用程序(在 Windows 7 32 位上运行)中的托管代码使用SaveFileDialog
才会出现相同的错误来自数据库。 经过几个小时的调试和试验/错误后,我的目光落在了一个我以前没有意识到的属性上:在 VB.NET 中,我编写了代码,错误就消失了。
Same errror using
SaveFileDialog
via managed code in a .NET Windows Forms application (running on Windows 7 32-bit) only when exposing thesaveAs
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 wroteand the error disappeared.