April 20th, 2010, 05:30 PM
Managed C++ call Win32 Api with managed data
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 :
public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);
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)
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
static bool GetIconInfo(IntPtr hIcon, IconInfo^ pIconInfo);
static IntPtr CreateIconIndirect(IconInfo^ icon);
Cursor^ ControlsHelpers::CreateCursor(Bitmap^ bmp, int xHotSpot, int yHotSpot)
IntPtr ptr = bmp->GetHicon();
IconInfo^ tmp = gcnew IconInfo();
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?!
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,
Tags for this Thread
Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!