wpf 知道什么会导致 Visual Studio 2013 中的“vshost32.exe 已停止工作”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36153362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Any idea what can cause "vshost32.exe has stopped working" in Visual Studio 2013?
提问by YetMoreStuff
A C# WPF application I am working on contains many calls to an unmanaged external DLL. All calls to the DLL work as expected when running the application normally (i.e. outside the Visual Studio debugger). However when debugging from within Visual Studio 2013, a call to one specific method in the DLL crashes the application:
我正在处理的 AC# WPF 应用程序包含许多对非托管外部 DLL 的调用。当应用程序正常运行时(即在 Visual Studio 调试器之外),对 DLL 的所有调用都按预期工作。但是,在 Visual Studio 2013 中进行调试时,调用 DLL 中的一个特定方法会使应用程序崩溃:
This is how I import the method:
这是我导入方法的方式:
[DllImport("Client.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern string ClientGetVersion();
...and this is how I call the DLL method:
...这就是我调用 DLL 方法的方式:
try
{
version = ClientGetVersion();
}
catch (Exception ex)
{
// Error handling omitted for clarity...
}
It appears that Visual Studio uses the vshost32.exe process to host applications during a debugging session (VSHOST - the Hosting Process). Furthermore, "Calls to certain APIs can be affected when the hosting process is enabled. In these cases, it is necessary to disable the hosting process to return the correct results." (See the MSDN article How to: Disable the Hosting Process). Disabling the "Enable the Visual Studio hosting process" option in Project > Properties... > Debug, as shown below, does indeed fix the problem:
Visual Studio 似乎使用 vshost32.exe 进程在调试会话期间托管应用程序(VSHOST - 托管进程)。此外,“启用托管进程时,对某些 API 的调用可能会受到影响。在这些情况下,有必要禁用托管进程以返回正确的结果。” (请参阅 MSDN 文章如何:禁用托管进程)。在 Project > Properties... > Debug 中禁用“启用 Visual Studio 托管进程”选项,如下所示,确实解决了问题:
Does anyone have any idea what specifically could cause this issue with "...calls to specific APIs..."?
有没有人知道“...调用特定 API...”的具体原因是什么?
采纳答案by YetMoreStuff
The vshost32.exe error is caused by an incorrect DllImport statement - the return type of the external DLL cannot be string, it must be IntPtr.
vshost32.exe 错误是由不正确的 DllImport 语句引起的 - 外部 DLL 的返回类型不能是字符串,它必须是 IntPtr。
Here is the corrected code:
这是更正后的代码:
[DllImport("Client.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr ClientGetVersion();
...and this is the revised call to the DLL method:
...这是对 DLL 方法的修改后的调用:
string version;
try
{
version = Marshal.PtrToStringAnsi(ClientGetVersion());
}
catch (Exception ex)
{
// Error handling omitted for clarity...
}
Thanks to @HansPassant for the answer.
感谢@HansPassant 的回答。
回答by cocamelange
Quit Visual Studio and Relaunch in Administrator Mode. It Work!!!
退出 Visual Studio 并以管理员模式重新启动。这行得通!!!


