NO

Author Topic: Boolean return values  (Read 8911 times)

jev

  • Guest
Boolean return values
« on: August 30, 2012, 12:08:57 PM »
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:
Code: [Select]
#include <stdbool.h>
__declspec( dllexport ) bool __stdcall foo( void );

but in C# I need to define and use it as follows:
Code: [Select]
[DllImport( "foo.dll")]
public static extern int foo( void );

if ( (foo() & 0x01) == 0 )
{
// Do something interesting here
}
...which obviously should've been:
Code: [Select]
[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?
« Last Edit: August 30, 2012, 12:12:03 PM by jev »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Boolean return values
« Reply #1 on: August 30, 2012, 02:09:15 PM »
Quote
how large is a bool returnvalue?
1 byte
Quote
Does 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:
Code: [Select]
[DllImport( "foo.dll")]
[return: MarshalAs(UnmanagedType.U1)]
public static extern bool foo();
May the source be with you

jev

  • Guest
Re: Boolean return values
« Reply #2 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

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Boolean return values
« Reply #3 on: August 30, 2012, 07:17:14 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  ;)

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Boolean return values
« Reply #4 on: August 31, 2012, 12:06:00 PM »
The boolean type in C should be an integer (32 bits) as defined in windef.h
Code: [Select]
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

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: Boolean return values
« Reply #5 on: August 31, 2012, 02:14:39 PM »
Some functions exported from this DLL return a boolean (bool from stdbool.h).
The boolean type in C should be an integer (32 bits) as defined in windef.h
Code: [Select]
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

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Boolean return values
« Reply #6 on: August 31, 2012, 05:55:05 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

  • Guest
Re: Boolean return values
« Reply #7 on: September 03, 2012, 10:45:57 AM »
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.

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: Boolean return values
« Reply #8 on: September 03, 2012, 11:25:31 AM »
If '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