[Mono-list] PInvoke works with ms runtime but not mono BUG??
Neil Cawse
neilcawse@hotmail.com
Tue, 15 Oct 2002 17:19:42 -0400
<html><div style='background-color:'><DIV>
<P>I am trying to run a simple windows message pump class under Windows XP using Mono.</P>
<P>When running the following class using mono class1.exe, the app fails to create the hwnd.</P>
<P>Yet running the same class using ms runtime runs perfectly.</P>
<P>Doesnt matter if i compile with mcs or ms compiler - gives same result.</P>
<P>Could it be that the mono jit/mint are not keeping the WNDPROC address fixed?? Not sure - its beyond me.</P>
<P>Can anyone help?</P>
<P>If i can get this to work properly then WOW!</P><FONT color=#0000ff size=2>
<P>using</FONT><FONT size=2> System;</P></FONT><FONT color=#0000ff size=2>
<P>using</FONT><FONT size=2> System.Runtime.InteropServices;</P></FONT><FONT color=#0000ff size=2>
<P>class</FONT><FONT size=2> Class1</P>
<P>{</P>
<P></FONT><FONT color=#0000ff size=2>static</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>void</FONT><FONT size=2> Main(</FONT><FONT color=#0000ff size=2>string</FONT><FONT size=2>[] args)</P>
<P>{</P>
<P>WNDCLASS wc = </FONT><FONT color=#0000ff size=2>new</FONT><FONT size=2> WNDCLASS();</P>
<P>wc.style=0;</P>
<P>wc.lpfnWndProc = </FONT><FONT color=#0000ff size=2>new</FONT><FONT size=2> WNDPROC(MyWndProc);</P>
<P>wc.hbrBackground = 6;</P>
<P>wc.lpszClassName = "NativeFormClass";</P>
<P></FONT><FONT color=#0000ff size=2>if</FONT><FONT size=2> (RegisterClass(wc)==0) </P>
<P>{</P>
<P>Console.WriteLine("Failed to create window class");</P>
<P>}</P>
<P></FONT><FONT color=#0000ff size=2>else</FONT><FONT size=2> </P>
<P>{</P>
<P></FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> hwnd = CreateWindowEx(0, "NativeFormClass", "NativeForm",0x10cf0000,</P>
<P>-2147483648, -2147483648, -2147483648, -2147483648,</P>
<P>0, 0,0, 0);</P>
<P></FONT><FONT color=#0000ff size=2>if</FONT><FONT size=2> (hwnd==0) </P>
<P>{</P>
<P>Console.WriteLine("Failed to create HWND");</P>
<P>}</P>
<P></FONT><FONT color=#0000ff size=2>else</FONT><FONT size=2> </P>
<P>{</P>
<P>MSG msgvalue;</P>
<P></FONT><FONT color=#0000ff size=2>while</FONT><FONT size=2> (GetMessage(</FONT><FONT color=#0000ff size=2>out</FONT><FONT size=2> msgvalue, 0,0,0)) </P>
<P>{</P>
<P>TranslateMessage(</FONT><FONT color=#0000ff size=2>ref</FONT><FONT size=2> msgvalue);</P>
<P>DispatchMessage(</FONT><FONT color=#0000ff size=2>ref</FONT><FONT size=2> msgvalue);</P>
<P>}</P>
<P>Console.WriteLine("Done");</P>
<P>}</P>
<P>}</P>
<P>}</P>
<P>[DllImport("user32", EntryPoint="GetMessageA", CharSet = CharSet.Ansi)]</P>
<P></FONT><FONT color=#0000ff size=2>static</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>extern</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>bool</FONT><FONT size=2> GetMessage(</FONT><FONT color=#0000ff size=2>out</FONT><FONT size=2> MSG msgvalue, </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> hwnd, </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> minFilter, </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> maxFilter);</P>
<P>[DllImport("user32", EntryPoint="DispatchMessageA", CharSet = CharSet.Ansi)]</P>
<P></FONT><FONT color=#0000ff size=2>static</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>extern</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> DispatchMessage(</FONT><FONT color=#0000ff size=2>ref</FONT><FONT size=2> MSG msgvalue);</P>
<P>[DllImport("user32")]</P>
<P></FONT><FONT color=#0000ff size=2>static</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>extern</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>bool</FONT><FONT size=2> TranslateMessage(</FONT><FONT color=#0000ff size=2>ref</FONT><FONT size=2> MSG msgValue);</P>
<P>[DllImport("user32", EntryPoint="RegisterClassA", CharSet = CharSet.Ansi)]</P>
<P></FONT><FONT color=#0000ff size=2>static</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>extern</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> RegisterClass(WNDCLASS wc);</P>
<P></FONT><FONT color=#0000ff size=2>static</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> MyWndProc (</FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> hWnd, </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> msgvalue, </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> wParam, </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> lParam) </P>
<P>{</P>
<P></FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> retval = 0;</P>
<P></FONT><FONT color=#0000ff size=2>switch</FONT><FONT size=2>(msgvalue) </P>
<P>{</P>
<P></FONT><FONT color=#0000ff size=2>case</FONT><FONT size=2> 2:</P>
<P>PostQuitMessage(0);</P>
<P></FONT><FONT color=#0000ff size=2>break</FONT><FONT size=2>;</P>
<P></FONT><FONT color=#0000ff size=2>default</FONT><FONT size=2>:</P>
<P>retval = DefWindowProc(hWnd, msgvalue, wParam, lParam);</P>
<P></FONT><FONT color=#0000ff size=2>break</FONT><FONT size=2>;</P>
<P>}</P>
<P></FONT><FONT color=#0000ff size=2>return</FONT><FONT size=2> retval;</P>
<P>}</P>
<P>[StructLayout(LayoutKind.Sequential)]</P>
<P></FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>struct</FONT><FONT size=2> MSG </P>
<P>{</P>
<P></FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> hwnd;</P>
<P></FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> message;</P>
<P></FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> wParam;</P>
<P></FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> lParam;</P>
<P></FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> itime;</P>
<P></FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> pt_x;</P>
<P></FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> pt_y;</P>
<P>}</P>
<P>[DllImport("user32", EntryPoint="PostQuitMessage")]</P>
<P></FONT><FONT color=#0000ff size=2>static</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>extern</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>void</FONT><FONT size=2> PostQuitMessage(</FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> nExitCode);</P>
<P>[DllImport("user32", EntryPoint="DefWindowProcA", CharSet = CharSet.Ansi)]</P>
<P></FONT><FONT color=#0000ff size=2>static</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>extern</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> DefWindowProc(</FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> hWnd, </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> msg, </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> wParam, </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> lParam);</P>
<P>[DllImport("user32", EntryPoint="CreateWindowExA", CharSet =CharSet.Ansi)]</FONT><FONT color=#008000 size=2>//, CharSet=CharSet.Unicode)]</P></FONT><FONT size=2>
<P></FONT><FONT color=#0000ff size=2>static</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>extern</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> CreateWindowEx(</FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> dwExStyle, </FONT><FONT color=#0000ff size=2>string</FONT><FONT size=2> lpszClassName, </FONT><FONT color=#0000ff size=2>string</FONT><FONT size=2> lpszWindowName,</P>
<P></FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> style, </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> x, </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> y, </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> width, </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> height, </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> hWndParent, </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> hMenu, </P>
<P></FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> hInst, </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> pvParam);</P>
<P>[StructLayout(LayoutKind.Sequential)]</P>
<P></FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>class</FONT><FONT size=2> WNDCLASS </P>
<P>{</P>
<P></FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> style;</P>
<P></FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> WNDPROC lpfnWndProc;</P>
<P></FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> cbClsExtra;</P>
<P></FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> cbWndExtra;</P>
<P></FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> hInstance;</P>
<P></FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> hIcon;</P>
<P></FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> hCursor;</P>
<P></FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> hbrBackground;</P>
<P></FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>string</FONT><FONT size=2> lpszMenuName;</P>
<P></FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>string</FONT><FONT size=2> lpszClassName;</P>
<P>}</P>
<P></FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>delegate</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> WNDPROC( </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> hWnd, </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> msgvalue, </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> wParam, </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> lParam);</P>
<P>}</P>
<P></FONT></P></DIV></div><br clear=all><hr>Surf the Web without missing calls! Get MSN Broadband. <a href="http://g.msn.com/8HMAEN/2023">Click Here</a> </html>