Any idea what can cause “vshost32.exe has stopped working” in Visual Studio 2013?

て烟熏妆下的殇ゞ 提交于 2019-12-30 20:30:52

问题


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..."?


回答1:


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.




回答2:


Quit Visual Studio and Relaunch in Administrator Mode. It Work!!!



来源:https://stackoverflow.com/questions/36153362/any-idea-what-can-cause-vshost32-exe-has-stopped-working-in-visual-studio-2013

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!