AccessViolationException,尝试读取或写入受保护的内存
我使用的 dll 包含用于与特定硬件交互的非托管代码,并且我尝试从 C# 使用它,但我不断收到 AccessViolationException。
是什么原因造成的?我该如何解决?
namespace FingerPrint {
public unsafe partial class Form1 : Form {
[DllImport("MyDll.dll")]
public static extern int DoesExist();
public unsafe Form1() {
InitializeComponent();
MessageBox.Show(DoesExist() + "");
}
}
}
I'm using a dll that contains unmanaged code for interacting with specific hardware, and I'm trying to use it from C#, but I keep getting an AccessViolationException.
What's causing it, and how can I fix it?
namespace FingerPrint {
public unsafe partial class Form1 : Form {
[DllImport("MyDll.dll")]
public static extern int DoesExist();
public unsafe Form1() {
InitializeComponent();
MessageBox.Show(DoesExist() + "");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
非托管代码的所有故障模式中大约 95% 都会产生访问冲突。您可以通过错误的 [DllImport] 声明来触发此类故障模式。但发布的DoesExist() 无法触发。
请联系 DLL 的供应商或作者以获得支持。如果你这么容易就失败了,他们应该可以轻松地重现错误并使用调试器及其源代码进行诊断。
为了完整起见,AccessViolation 的最典型原因是:
Roughly 95% of all failure modes for unmanaged code produce an access violation. You can trigger such a failure mode by getting the [DllImport] declaration wrong. But DoesExist() as posted cannot trigger one.
Contact the vendor or the author of the DLL for support. They should have little trouble reproducing the fault and diagnosing it with a debugger and their source code if you got it to fail so easily.
For completeness, the most typical causes of AccessViolation: