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?

3
  • Minor note, DWORD is an unsigned 32-bit type. The corresponding type in C# is uint, not int. Commented Oct 3, 2012 at 15:09
  • bool is the standard C++ type. BOOL is a MFC type. they are not the same. Commented Oct 3, 2012 at 15:11
  • public extern static int IsEmpty(int KeyID, ref int pFlag) Commented Oct 3, 2012 at 15:11

4 Answers 4

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.)

Sign up to request clarification or add additional context in comments.

2 Comments

I don't believe that the size of BOOL nor DWORD changes on 64-bit Windows.
I'd add the even more bizarre VARIANT_BOOL where VARIANT_FALSE is -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)

Comments

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) ;

1 Comment

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...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. :-/
0

Thank you for your help!

finally this works for me

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.