C# DllImport with C++ boolean function not returning correctly

后端 未结 7 841
长发绾君心
长发绾君心 2020-11-29 04:20

I have the following function in a C++ DLL

extern \"C\" __declspec(dllexport) bool Exist(const char* name)
{
 //if (g_Queues.find(name) != g_Queues.end())
 /         


        
7条回答
  •  感动是毒
    2020-11-29 04:54

    C'sbool is actually int, as there is no boolean type in the original C language. That means that if C#'s DLLImport is designed to interop with C code, then they will expect that C#'s bool to correspond to C's int. While this still doesn't explain why false would become true, fixing it should fix the problem.

    http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.unmanagedtype.aspx

    This says that UnmanagedType.Bool is the Win32 BOOL, which is an int.

提交回复
热议问题