C#的DllImport与C ++布尔函数不正确返回 [英] C# DllImport with C++ boolean function not returning correctly

查看:301
本文介绍了C#的DllImport与C ++布尔函数不正确返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++ DLL下面的函数

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())
 // return true;
 //else
 // return false;
 return false;
}

在我的C#类我有以下几点:

Inside my C# class I have the following:

[DllImport("Whisper.dll", EntryPoint="Exist", CallingConvention=CallingConvention.Cdecl)]
        public static extern bool Exist(string name);

然而,每当我把我的函数它总是返回true,甚至当我注释掉我的小功能,并使其返回false。我有什么毛病我的调用约定或其他任何问题与P /调用我的DLL的感觉,可能与串并为const char *对应的,但现在我完全一无所知。我究竟做错了什么?为什么它不是假返回true?

Yet, whenever I call my function it ALWAYS returns true, even when I commented out my little function and made it return false. I have the feeling there is something wrong with my calling convention or any other issue with P/Invoking my DLL, probably corresponding with the string and const char*, but for now I am completely clueless. What am I doing wrong? Why does it return true instead of false?

修改我已经想通了,这有什么做的为const char *或字符串,因为这个问题有一个空的功能仍然存在。我试着改变CDECL和STDCALL既不正常工作之间的调用约定。我也成功地调试我的DLL和它被称为正确和确实返回false,但一旦回到C#它在某种程度上是真实的。改变字符集也没有影响。我确信我提供我的,每次我的DLL的最新和正确版本的C#程序,所以这不应该是一个问题,以及。再次,我对为什么当我其实返回false是结果为真无能完全

I have figured out this has nothing to do with the const char* or string, because the problem persists with an empty function. I've tried changing the calling convention between Cdecl and StdCall and neither work correctly. I've also managed to debug my DLL and it's being called correctly and does indeed return false, but once back into C# it somehow is true. Changing the CharSet also had no effect. I've made sure I've supplied my C# program with the latest and correct version of my DLL each time, so that shouldn't be an issue aswell. Again, I am completely clueless on why the result is true when I'm in fact returning false.

EDIT2: SOReader与解决的另一个重要问题,看我的意见建议提供给我。可悲的是,它并没有解决问题的回报。

SOReader provided me with a suggestion which fixes another important issue, see my comment. Sadly, it does not fix the return issue.

EDIT3:我的结论是不断变化的存在(布尔)的返回类型为(int)的突然使它返回正确的数量(真= 1,假= 0)。这将意味着,有可能是C ++的布尔和C#的布尔之间的问题。我可以继续使用一个int作为一个布尔值,但仍无法解释最初的问题。也许别人可以见识一下这个吗?也许它的事实,我用64做的(虽然两者pojects被编译为X86)

I have concluded that changing the return type of Exist (bool) into (int) suddenly makes it return the correct number (true = 1, false = 0). That would mean that there may be an issue between C++'s bool and C#'s bool. I can continue using an int as a bool, but that would still not explain the original problem. Maybe somebody else can enlighten me on this one? Perhaps it has to do with the fact that I'm using x64 (although both pojects are compiled as x86)

推荐答案

我发现你的问题的解决方案。您的声明应符合本编组pceded $ P $:
[返回:的MarshalAs(UnmanagedType.I1)

I found the solution for your problem. Your declaration should be preceded with this marshaling: [return:MarshalAs(UnmanagedType.I1)]

所以一切都应该是这样的:结果
函数[DllImport(Whisper.dll,入口点=存在,CallingConvention = CallingConvention.Cdecl)]结果
[返回:的MarshalAs(UnmanagedType.I1)结果
公共静态外部布尔存在([的MarshalAs(UnmanagedType.LPStr)字符串名);

so everything should look like this:
[DllImport("Whisper.dll", EntryPoint="Exist", CallingConvention=CallingConvention.Cdecl)]
[return:MarshalAs(UnmanagedType.I1)]
public static extern bool Exist([MarshalAs(UnmanagedType.LPStr)] string name);

我在很简单的例子测试,它的工作!

I tested it in my very simple example and it worked!

修改结果
为什么出现这种情况? C定义为布尔4个字节INT(如有些人所说的)和C ++把它定义为1个字节。 C#团队决定使用4个字节的布尔默认的PInvoke期间,因为大多数系统API函数使用4个字节的值布尔。如果你想改变这种行为,你有一个编组指定您希望使用1个字节的值来做到这一点。

EDIT
Why this happens? C defines bool as 4 bytes int (as some of you have said) and C++ defines it as 1 byte. C# team decided to use 4 byte bool as default during PInvoke because most of the system API function use 4 bytes values as bool. If you want to change this behavior you have to do it with marshaling specifying that you want to use 1 byte value.

这篇关于C#的DllImport与C ++布尔函数不正确返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆