Yet*_*uff 5 c# wpf dll vshost32 visual-studio
我正在处理的AC#WPF应用程序包含许多对非托管外部DLL的调用.在正常运行应用程序时(即在Visual Studio调试器之外),对DLL的所有调用都按预期工作.但是,在Visual Studio 2013中进行调试时,对DLL中某个特定方法的调用会使应用程序崩溃:
这是我导入方法的方法:
[DllImport("Client.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern string ClientGetVersion();
Run Code Online (Sandbox Code Playgroud)
...这就是我调用DLL方法的方法:
try
{
version = ClientGetVersion();
}
catch (Exception ex)
{
// Error handling omitted for clarity...
}
Run Code Online (Sandbox Code Playgroud)
看来Visual Studio在调试会话期间使用vshost32.exe进程来托管应用程序(VSHOST - 托管进程).此外,"启用托管过程时,可能会影响对某些API的调用.在这些情况下,必须禁用托管过程以返回正确的结果." (请参阅MSDN文章如何:禁用主机进程).在Project> Properties ...> Debug中禁用"启用Visual Studio宿主进程"选项,如下所示,确实解决了问题:
有没有人知道具体可能导致这个问题的"...调用特定的API ......"?
vshost32.exe 错误是由不正确的 DllImport 语句引起的 - 外部 DLL 的返回类型不能是 string,它必须是 IntPtr。
这是更正后的代码:
[DllImport("Client.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr ClientGetVersion();
Run Code Online (Sandbox Code Playgroud)
...这是对 DLL 方法的修改后的调用:
string version;
try
{
version = Marshal.PtrToStringAnsi(ClientGetVersion());
}
catch (Exception ex)
{
// Error handling omitted for clarity...
}
Run Code Online (Sandbox Code Playgroud)
感谢@HansPassant 的回答。
| 归档时间: |
|
| 查看次数: |
8836 次 |
| 最近记录: |