3

I want to use a function in a C++ DLL in my C# application using DLLImport:

BOOL IsEmpty( DWORD KeyID, BOOL *pFlag )

I tried many combinations but in vain, like:

public extern static bool IsEmpty(int KeyID, ref bool pFlag);

The method returns false (that means an error).

Any idea how to do that?

Pang
  • 9,564
  • 146
  • 81
  • 122
user1717487
  • 41
  • 1
  • 6

4 Answers4

4

To quote "Willy" (with amendments):

Beware the booleans!

Win32 defines different versions of booleans.

1) BOOL used by most Win32 API's, is an unsigned int a signed int (4 bytes)

2) BOOLEAN is a single byte, only used by a few win32 API's!!

3) and C/C++ has it's builtin 'bool' which is a single byte

...and to add what @tenfour pointed out:

4) the even more bizarre VARIANT_BOOL

typedef short VARIANT_BOOL;
#define VARIANT_TRUE ((VARIANT_BOOL)-1)
#define VARIANT_FALSE ((VARIANT_BOOL)0)

The signed or unsigned nature shouldn't matter for BOOL, as the only "false" pattern is 0. So try treating it as a 4 byte quantity...however you interface with a DWORD may be satisfactory, (I've not dealt with Windows 64-bit conventions.)

Community
  • 1
  • 1
1

BOOL in Win32 is a typedef of int, so you should just change bool to Int32, so the definition is int IsEmpty(uint KeyID, ref int pFlag)

BigBoss
  • 6,904
  • 2
  • 23
  • 38
1

because in c++ BOOL is defined as int. You should use

    [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
    public static extern  bool IsEmpty(uint KeyID, ref int pFlag) ;
Manuel Amstutz
  • 1,311
  • 13
  • 33
  • To be more precise: BOOL is defined inside the Win32 API header files (not any C++ standard, though C++ added a `bool` that did not exist in C). MSDN does however say it's a [signed int from WinDef.h](http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx)...not an unsigned as from the source I quoted. Although signed/unsigned technically should not matter as only 0 is FALSE and that has the same bit pattern regardless. :-/ – HostileFork says dont trust SE Oct 03 '12 at 15:40
0

Thank you for your help!

finally this works for me

public extern static int IsEmpty( int KeyID, int[] pFlag)

user1717487
  • 41
  • 1
  • 6