P/Invoke returning and passing bool's between C# and C++

  • Thread starter Thread starter Philippe F. Bertrand
  • Start date Start date
P

Philippe F. Bertrand

Summary - What should a C# bool map to in C++? Are return types
different?

I have lots of methods declared with bool parameters and bool return
type which appear to work without any problems but one method in
particular seems to always return true.

The method is declared in C# as

[DllImport("my.dll")] private static extern
bool T_Cursor_GetBoolean(
int key, short columnID, ErrorCode *code
);

And in C++ as

extern "C" {
__declspec(dllexport) bool T_Cursor_GetBoolean(
int natKey, short cid, int *code
);
}

__declspec(dllexport) bool T_Cursor_GetBoolean(
int natKey, short cid, int *code
)
{
bool out = some stuff;
return out;
}

Stepping through the code in Visual Studio on the desktop, I see the
unmanaged code returning false and the managed code receiving true.

I need this code to work on both the .NET framework and the
CompactFramework.

Is there a way to globally change the marshalling of bool's and will
this work in the compact framework?

- Thanks
Philippe
 
Philippe,
Summary - What should a C# bool map to in C++? Are return types
different?

Depends on your marshaling settings.

The default is a 32-bit Win32 BOOL.
With [MarshalAs(UnamangedType.VariantBool)] you get a 16-bit
VARAIANT_BOOL.
With [MarshalAs(UnamangedType.I1)] you get a one byte C++ bool (or
Win32 BOOLEAN).

So in your case, I'd try to change the declaration to

[DllImport("my.dll")]
[return: MarshalAs(UnmanagedType.I1)]
private static extern bool T_Cursor_GetBoolean( ...


That's how it works on the desktop CLR anyway. I'm not familiar enough
with .NET CF to say how it works there.



Mattias
 
Back
Top