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

后端 未结 2 1910
予麋鹿
予麋鹿 2021-01-18 18:06

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. outs

2条回答
  •  -上瘾入骨i
    2021-01-18 18:44

    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.

提交回复
热议问题