任何想法可能导致“vshost32.exe已停止工作”在Visual Studio 2013? [英] Any idea what can cause "vshost32.exe has stopped working" in Visual Studio 2013?

查看:517
本文介绍了任何想法可能导致“vshost32.exe已停止工作”在Visual Studio 2013?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理的C#WPF应用程序包含对非托管外部DLL的许多调用。所有对DLL的调用在正常运行应用程序(即Visual Studio调试程序之外)时正常工作。但是当从Visual Studio 2013进行调试时,DLL中的一个特定方法的调用会使应用程序崩溃:



)。此外,在启用主机进程时,可能会影响对某些API的调用,在这种情况下,需要禁用主机进程才能返回正确的结果。 (请参阅MSDN文章如何:禁用托管过程)。在Project> Properties ...> Debug中禁用启用Visual Studio托管过程选项,如下所示,确实解决了这个问题:





p>

vshost32.exe错误是由不正确的DllImport语句引起的 - 外部DLL的返回类型不能是字符串,它必须是IntPtr



以下是更正的代码:

  [DllImport Client.dll,CallingConvention = CallingConvention.Cdecl)] 
private static extern IntPtr ClientGetVersion();

...这是对DLL方法的修改调用:

  string version; 

try
{
version = Marshal.PtrToStringAnsi(ClientGetVersion());

}
catch(Exception ex)
{
//为了清楚起见错误处理省略...
}

感谢@HansPassant的答案。


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:

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:

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:

Does anyone have any idea what specifically could cause this issue with "...calls to specific APIs..."?

解决方案

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.

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:

string version;

try
{
  version = Marshal.PtrToStringAnsi(ClientGetVersion());

}
catch (Exception ex)
{
  // Error handling omitted for clarity...
}

Thanks to @HansPassant for the answer.

这篇关于任何想法可能导致“vshost32.exe已停止工作”在Visual Studio 2013?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆