-
April 20th, 2010, 04:30 PM
#1
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|