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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Philippe F. Bertrand

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

    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.d ll")] private static extern
    bool T_Cursor_GetBoo lean(
    int key, short columnID, ErrorCode *code
    );

    And in C++ as

    extern "C" {
    __declspec(dlle xport) bool T_Cursor_GetBoo lean(
    int natKey, short cid, int *code
    );
    }

    __declspec(dlle xport) bool T_Cursor_GetBoo lean(
    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
    CompactFramewor k.

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

    - Thanks
    Philippe
  • Mattias Sjögren

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

    Philippe,
    [color=blue]
    >Summary - What should a C# bool map to in C++? Are return types
    >different?[/color]

    Depends on your marshaling settings.

    The default is a 32-bit Win32 BOOL.
    With [MarshalAs(Unama ngedType.Varian tBool)] you get a 16-bit
    VARAIANT_BOOL.
    With [MarshalAs(Unama ngedType.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.d ll")]
    [return: MarshalAs(Unman agedType.I1)]
    private static extern bool T_Cursor_GetBoo lean( ...


    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

    --
    Mattias Sjögren [MVP] mattias @ mvps.org

    Please reply only to the newsgroup.

    Comment

    • Chris Tacke, eMVP

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

      In the CF there's no COM interop, so it's always 32-bits. Use an int.

      --
      Chris Tacke, eMVP
      Advisory Board Member

      ---
      Windows CE Product Manager
      Applied Data Systems


      "Philippe F. Bertrand" <pfbca@yahoo.ca > wrote in message
      news:dd10e1c2.0 310031045.6ef71 fb0@posting.goo gle.com...[color=blue]
      > 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.d ll")] private static extern
      > bool T_Cursor_GetBoo lean(
      > int key, short columnID, ErrorCode *code
      > );
      >
      > And in C++ as
      >
      > extern "C" {
      > __declspec(dlle xport) bool T_Cursor_GetBoo lean(
      > int natKey, short cid, int *code
      > );
      > }
      >
      > __declspec(dlle xport) bool T_Cursor_GetBoo lean(
      > 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
      > CompactFramewor k.
      >
      > Is there a way to globally change the marshalling of bool's and will
      > this work in the compact framework?
      >
      > - Thanks
      > Philippe[/color]


      Comment

      Working...