The situation:
In my C# solution (VS 2013) I added a C project. The C project contains a function that is declared in C# with:
[DllImport("My_C_Dll.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void My_C_Function(IntPtr OutputArray, int Input);
The function is called with this code:
var OutputArray = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(UInt16)) * OutputLength);
My_C_Function(OutputArray, Input);
Int16[] ManagedOutputArray= new Int16[OutputLength];
Marshal.Copy(OutputArray, ManagedOutputArray, 0, OutputLength);
Marshal.FreeHGlobal(OutputArray);
The issue:
When "My_C_Function" is called the application hangs (actually is the Visual Studio Hosting process "vshost32-clr2.exe" that hangs) when ALL the following conditions are satisfied:
when I run WITH the debugger attached (starting application from Visual Studio)
when I DISABLE the "Enable native code debugging" in the Project properties
when I ENABLE the "Enable the Visual Studio hosting process"
My idea
It seems that "vshost32-clr2.exe" stops working because it is not able to execute the C extern function if the "native code debugging" is disabled. It seems that it needs some feature that is given only when "native code debugging" is enabled.
My issue seems the same of AccessViolationException goes away when native code debugging is enabled but no one has already answered why this happens.
My question WHY? For another project I already did a different solution that calls a different C extern functions without such issue (I can leave the "hosting process" enabled and the "native code debugging" disabled without any problem) but I cannot find the difference between the two solutions that leads to this error.
Any help would be greatly appreciated!