Managed C++ call Win32 Api with managed data
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Managed C++ call Win32 Api with managed data

  1. #1
    Join Date
    Apr 2010
    Posts
    3

    Question Managed C++ call Win32 Api with managed data

    Dear All,
    I have decided to port an application that I wrote In C# in Managed C++ as I see that there is an advantage of mixing (directly,without DLL) managed and unmanaged code.I am mainly a pure C++ Win32 programmer.
    The app is WinForm based.Under C#, I had this :

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);

    [DllImport("user32.dll")]
    public static extern IntPtr CreateIconIndirect(ref IconInfo icon);

    public struct IconInfo
    {
    public bool fIcon;
    public int xHotspot;
    public int yHotspot;
    public IntPtr hbmMask;
    public IntPtr hbmColor;
    }
    public static Cursor CreateCursor(Bitmap bmp, int xHotSpot, int yHotSpot)
    {
    Bitmap x;
    IntPtr ptr = bmp.GetHicon();
    IconInfo tmp = new IconInfo();
    GetIconInfo(ptr, ref tmp);
    tmp.xHotspot = xHotSpot;
    tmp.yHotspot = yHotSpot;
    tmp.fIcon = false;
    ptr = CreateIconIndirect(ref tmp);
    return new Cursor(ptr);
    }

    I need the same think in managed c++,in order to be able to use the resulting cursor in managed code,so,I wrote that:

    ref struct IconInfo
    {
    bool fIcon;
    Int32 xHotspot;
    Int32 yHotspot;
    IntPtr hbmMask;
    IntPtr hbmColor;
    };

    [DllImport("user32.dll")]
    static bool GetIconInfo(IntPtr hIcon, IconInfo^ pIconInfo);

    [DllImport("user32.dll")]
    static IntPtr CreateIconIndirect(IconInfo^ icon);

    Cursor^ ControlsHelpers::CreateCursor(Bitmap^ bmp, int xHotSpot, int yHotSpot)
    {
    IntPtr ptr = bmp->GetHicon();
    IconInfo^ tmp = gcnew IconInfo();
    GetIconInfo(ptr,tmp);
    tmp->xHotspot = xHotSpot;
    tmp->yHotspot = yHotSpot;
    tmp->fIcon = false;
    ptr = CreateIconIndirect(tmp);
    return gcnew Cursor(ptr);
    }

    The problem is that the GetIconInfo call fails (IconInfo structure containt is mostly NULL).

    I am a bit confused from what I learnt the managed C++ has two new operators for unmanaged (^ = * and % = &),maybe I am wrong with the call?!

    Any idea?

    I would like also to say that it is very hard to find good information about managed C++,I think
    that this language is very powerfull for heavy processing (MFC is dead,there is WinForms and still able to use fast unmanaged).

    Thank you in advance,
    Best regards.
    N.

  2. #2
    Join Date
    Apr 2010
    Posts
    20

    Re: Managed C++ call Win32 Api with managed data

    Shouldn't IconInfo be a value struct? Also, shouldn't you declare the following:

    Code:
    [DllImport("user32.dll")]
    static bool GetIconInfo(IntPtr hIcon, IconInfo^ pIconInfo);
    
    // as
    
    [DllImport("user32.dll")]
    static bool GetIconInfo(IntPtr hIcon,[MarshalAs(UnmanagedType::LPStruct)] IconInfo ^ pIconInfo);
    Last edited by CppCoder2010; April 20th, 2010 at 10:18 PM.

  3. #3
    Join Date
    Apr 2010
    Posts
    3

    Re: Managed C++ call Win32 Api with managed data

    Hi,
    Thank you for your reply,I forgot to add that,anyway,it still does not work.
    In addition,I needed to add align marshaling on the struct,this is essential.
    The GetIconInfo still does nothing with the struct and returns true.
    Any idea?

    Best regards.

    N.




    -------------------------------------------------
    The code is now the follow (For info) :

    [StructLayout(LayoutKind::Sequential)]
    ref struct IconInfo
    {
    bool fIcon;
    Int32 xHotspot;
    Int32 yHotspot;
    IntPtr hbmMask;
    IntPtr hbmColor;
    };
    [DllImport("user32.dll")]
    [returnvalue:MarshalAs(UnmanagedType::Bool)]
    static bool GetIconInfo(IntPtr hIcon,[MarshalAs(UnmanagedType::LPStruct)] IconInfo^ pIconInfo);

    [DllImport("user32.dll")]
    static IntPtr CreateIconIndirect([MarshalAs(UnmanagedType::LPStruct)] IconInfo^ icon);

  4. #4
    Join Date
    Apr 2010
    Posts
    3

    Resolved Re: Managed C++ call Win32 Api with managed data

    Hi,
    I got it!

    The problem was resolved by adding the argument direction on the declaration ([In,Out]):

    public:
    [StructLayout(LayoutKind::Sequential)]
    ref struct IconInfo
    {
    Boolean fIcon;
    UInt32 xHotspot;
    UInt32 yHotspot;
    IntPtr hbmMask;
    IntPtr hbmColor;
    };
    [DllImport("user32.dll")]
    static Boolean GetIconInfo([In] IntPtr hIcon,[Out] [MarshalAs(UnmanagedType::LPStruct)] IconInfo^ pIconInfo);

    [DllImport("user32.dll")]
    static IntPtr CreateIconIndirect([In] [MarshalAs(UnmanagedType::LPStruct)] IconInfo^ icon);

    Hope that this can help!

    Best regards.

    Nassim

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center