Strange problem with Visual Studio.
There is a program in C # that needs to get some string resources from a regular native .dll library. In a strongly abbreviated form, the meaning of the code is something like

[DllImport("kernel32.dll", CharSet = CharSet.Auto)] static extern IntPtr LoadLibrary(string filename); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern int LoadString(IntPtr hInstance, uint uId, out StringBuilder buffer, int bufferMax); //... IntPtr _libPtr = LoadLibrary(filename); StringBuilder sb = new StringBuilder(); LoadString(_libPtr, id, out sb, 0); return sb.ToString(); 

In Visual Studio 2010 and younger, this code works fine, but in 2012 and older, something strange happens: on the LoadString (...) call, the debug simply ends without issuing any errors - as if the program just worked. Tested on the same project with the same data, and even a test project was created with this function alone - the behavior is preserved. Tested in VS 2012, 2013, 2015.
I suspect that there is some potentially influencing setting, but nothing like that was found in Google, nor by comparison of configurations ...

  • Try lowering the version of the .net framework to <= 4.0 - Mstislav Pavlov
  • @Bezarius .net in all cases was 4.0. It was possible to work around this by replacing it with the following: [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern int LoadString(IntPtr hInstance, uint uId, out IntPtr buffer, int bufferMax); IntPtr resource; var length = LoadString(_libPtr, id, out resource, 0); return length == 0 ? null : Marshal.PtrToStringAuto(resource, length); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern int LoadString(IntPtr hInstance, uint uId, out IntPtr buffer, int bufferMax); IntPtr resource; var length = LoadString(_libPtr, id, out resource, 0); return length == 0 ? null : Marshal.PtrToStringAuto(resource, length); - nkAlex

0