I'm trying to build a C# PInvoke for LoadImage, but I can't figure out how to pass the name parameter. I'm trying to test it by loading a system icon (in the example below the for use with WNDCLASSEX / RegisterClassEx, in place of LoadIcon).
I've tried three signatures:
This post suggested just putting new IntPtr around the value would work:
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern IntPtr LoadImage(IntPtr hInst, IntPtr name, uint type, int cx, int cy, uint fuLoad);
User32.LoadImage(Kernal32.GetModuleHandle("user32.dll"), new IntPtr(32513), 1, 16, 16, 0);
As per pinvoke.net, tried passing the value as a string:
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern IntPtr LoadImage(IntPtr hInst, string name, uint type, int cx, int cy, uint fuLoad);
User32.LoadImage(Kernal32.GetModuleHandle("user32.dll"), "32513", 1, 16, 16, 0);
User32.LoadImage(Kernal32.GetModuleHandle("user32.dll"), "#32513", 1, 16, 16, 0);
This article suggested simply using a ushort would do the trick.
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern IntPtr LoadImage(IntPtr hInst, ushort name, uint type, int cx, int cy, uint fuLoad);
User32.LoadImage(Kernal32.GetModuleHandle("user32.dll"), 32513, 1, 16, 16, 0);
In all cases, the LoadImage function fails (according to GetLastWin32Error) with System error code:
ERROR_RESOURCE_TYPE_NOT_FOUND 1813 (0x715): The specified resource type cannot be found in the image file.
This suggests the value for name cannot be located in user32.dll. In C++ you'd use MAKEINTRESOURCE to get a pointer for the name value. Looking at this and this, it suggests I need my IntPtr to substitute its final bytes with a ushort representing the integer value needed. If that is the most likely issue (and simply passing a ushort doesn't work), how can I do that?