Any idea what could cause “vshost32.exe to stop working” in Visual Studio 2013?

The C # WPF application I am working in contains many calls to an unmanaged external DLL. All calls to the DLL work as expected during normal operation of the application (that is, outside the Visual Studio debugger). However, when debugging from Visual Studio 2013, calling one specific method in the DLL causes the application to crash:

vshost32.exe stops working

This is how I import the method:

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

... and so I call the DLL method:

 try { version = ClientGetVersion(); } catch (Exception ex) { // Error handling omitted for clarity... } 

Visual Studio seems to use the vshost32.exe process to host applications during a debugging session ( VSHOST is the hosting process ). In addition, “When invoking certain APIs, it may be affected when the hosting process is enabled.” In these cases, you must turn off the hosting process in order to return the correct results. "(See the MSDN article How to turn off the hosting process .) Disabling the" Enable Visual Studio Hosting Process "option in Project> Properties ...> Debug, as shown below, really eliminates problem:

enter image description here

Can anyone understand what specifically can cause this problem with "... calls to specific APIs ..."?

+5
source share
2 answers

The vshost32.exe error is caused by the incorrect DllImport statement - the return type of the external DLL cannot be a string, it must be IntPtr.

Here is the corrected code:

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

... and this is a redesigned DLL method call:

 string version; try { version = Marshal.PtrToStringAnsi(ClientGetVersion()); } catch (Exception ex) { // Error handling omitted for clarity... } 

Thanks @HansPassant for the answer.

+1
source

Close Visual Studio and restart in administrator mode. It works!!!

0
source

Source: https://habr.com/ru/post/1245548/


All Articles