Multimobile Development: Building Applications for any Smartphone
using RegisterClassEx
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.interop.


Eric Bouchard
Hello !

Is there anyway to call RegisterClassEx function in user32.lib with managed
code ?

[DllImport("USER32")]
private static extern int RegisterClassEx(ref WNDCLASSEX wcex);

public delegate int WNDPROC(IntPtr hWnd, uint uMessage, UIntPtr wPraram,
IntPtr lParam);

[StructLayout(LayoutKind.Sequential)]
public class WNDCLASSEX {
public uint cbSize = 48;
public uint style = 0;
[MarshalAs(UnmanagedType.FunctionPtr)] public WNDPROC lpfnWndProc;
public int cbClsExtra = 0;
public int cbWndExtra = 0;
public IntPtr hInstance = IntPtr.Zero;
public IntPtr hIcon = IntPtr.Zero;
public IntPtr hCursor = IntPtr.Zero;
public IntPtr hbrBackground = IntPtr.Zero;
[MarshalAs(UnmanagedType.LPTStr)] public string lpszMenuName = null;
[MarshalAs(UnmanagedType.LPTStr)] public string lpszClassName = null;
public IntPtr hIconSm = IntPtr.Zero;
}

and when I call RegisterClassEx, a non zero value is retured, and
GetLastError is 126
(The specified module could not be found.)

can anyone help me ?

Reply to this message...
Vote that this is a GOOD answer...
 
Really good experience at the Apple Store
MonoDroid – looking *awesome*
 
    
Mattias Sjögren
Eric,

[Original message clipped]

Of course there is :-)

[Original message clipped]

wcex should only be a ref parameter if WNDCLASSEX is defined as a
value type (struct). When you make it a reference type (class), you
should change it to a by value parameter, decorated with
[MarshalAs(UnmanagedType.LPStruct)].

> public uint cbSize = 48;

Magic numbers are bad! ;-) I suggest you use Marshal.SizeOf instead.

[Original message clipped]

You can't rely on what GetLastError returns in managed code. Instead,
you should add SetLastError=true to the function's DllImport
attribute, and then check the error code with
Marshal.GetLastWin32Error after the call.

Mattias

===
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Reply to this message...
Vote that this is a GOOD answer...
 
First volume of Multimobile Development nearly ready to go to press
A mention on Developing for the iPhone and Android: The pros and cons
 
 
System.IntPtr
System.Runtime.InteropServices.LayoutKind
System.Runtime.InteropServices.Marshal
System.Runtime.InteropServices.UnmanagedType
System.UIntPtr




Multimobile Development: Building Applications for any Smartphone
Ad
BootFX
Reliable and powerful .NET application framework.
iOS, Android and Windows Phone Development Training and Consultancy
Hosted by RackSRV Communications
 
Multimobile Development: Building Applications for any Smartphone
Copyright © AMX Software Ltd 2008-2010. Portions copyright © Matthew Baxter-Reynolds 2001-2010. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - 4.0.30129.1734