Boolean return values

Started by jev, August 30, 2012, 12:08:57 PM

Previous topic - Next topic

jev

I've built a DLL using PellesC that needs to be used from C#. Some functions exported from this DLL return a boolean (bool from stdbool.h). How large are these things? Logically, they should be 1 bit ofcourse, but one function executes a "return false" that in C# sometimes is interpreted as 'true'. A bit of research showed the LSB in the return value can be trusted but the rest of the word cannot, so in the header of the DLL it says:

#include <stdbool.h>
__declspec( dllexport ) bool __stdcall foo( void );


but in C# I need to define and use it as follows:

[DllImport( "foo.dll")]
public static extern int foo( void );

if ( (foo() & 0x01) == 0 )
{
// Do something interesting here
}

...which obviously should've been:

[DllImport( "foo.dll")]
public static extern bool foo( void );

if (!foo() )
{
// Do something interesting here
}

Unfortunately, the DLL's API is fixed because the same sources are used in another platform's implementation. I can't just change the return values from bool to int (which would be a debatable workaround anyway).

So, what it boils down to: how large is a bool returnvalue? Does it match C#'s idea of a boolean?

TimoVJL

Quotehow large is a bool returnvalue?
1 byte
QuoteDoes it match C#'s idea of a boolean?
No, C# Bool is A 4-byte Boolean value (true != 0, false = 0). This is the Win32 BOOL type.

http://msdn.microsoft.com/en-us/library/ms182206(v=vs.80).aspx
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.unmanagedtype(v=vs.80).aspx

Use this:
[DllImport( "foo.dll")]
[return: MarshalAs(UnmanagedType.U1)]
public static extern bool foo();
May the source be with you

jev

Thanks, that seems to work just fine. C# really is hell for an (embedded) C-dinosaur like me  :o

Bitbeisser

Quote from: jev on August 30, 2012, 03:24:53 PM
Thanks, that seems to work just fine. C# really is hell for an (embedded) C-dinosaur like me  :o
C# is hell for everyone...  ;D

Ralf  ;)

frankie

The boolean type in C should be an integer (32 bits) as defined in windef.h
typedef int BOOL;

The typical definitions for TRUE is 1, FALSE is 0.
Anyway you should consider FALSE the 0, TRUE everything else (from 1 to 0xffffffff)
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Stefan Pendl

Quote from: jev on August 30, 2012, 12:08:57 PMSome functions exported from this DLL return a boolean (bool from stdbool.h).
Quote from: frankie on August 31, 2012, 12:06:00 PMThe boolean type in C should be an integer (32 bits) as defined in windef.h typedef int BOOL;

So the problem is that the OP uses the standard C header to get highest portability instead of using the Windows type.

If the project doesn't need to be portable, one can use the Windows type to get a simple C# code ;)
---
Stefan

Proud member of the UltraDefrag Development Team

TimoVJL

Quote from: Bitbeisser on August 30, 2012, 07:17:14 PM
C# is hell for everyone...  ;D

Ralf  ;)
But it is available for everyone with csc.exe (and VB.Net with vbc.exe).
PS: http://forum.pellesc.de/index.php?topic=2976.msg15881#msg15881

Timppa  ;)
May the source be with you

jev

Quote from: Stefan Pendl on August 31, 2012, 02:14:39 PM
OP uses the standard C header to get highest portability
Exactly. I need portability because the same source runs on several microcontrollers too. Since those controllers are pretty resource-challenged :o I just can't waste the extra 24 bits by using int. If 'bool' does not conform to the windows standard, that's a problem created by Wintendo and/or the C compiler.

Stefan Pendl

Quote from: jev on September 03, 2012, 10:45:57 AMIf 'bool' does not conform to the windows standard, that's a problem created by Wintendo and/or the C compiler.

bool and BOOL are two different things, since C is case sensitive.

bool is portable and based on the ANSI C standard, but BOOL is Windows only.
---
Stefan

Proud member of the UltraDefrag Development Team