views:

64

answers:

2

VB.NET 2008 Express

Private Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Integer, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hwndParent As Integer, ByVal hMenu As Integer, ByVal hInstance As Integer, ByRef lpParam As Object) As Integer

Private Const WS_EX_APPWINDOW = &H40000
Private Const WS_EX_WINDOWEDGE = &H100&
Private Const WS_SYSMENU = &H80000
Private Const WS_CAPTION = &HC00000
Private Const WS_MINIMIZEBOX = &H20000
Private Const WS_THICKFRAME = &H40000
Private Const WS_MAXIMIZEBOX = &H10000
Private Const WS_OVERLAPPEDWINDOW = (WS_OVERLAPPED Or WS_CAPTION Or WS_SYSMENU Or WS_THICKFRAME Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX)

Dim AppHandle As Int32 = System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32()

Dim WindowHandle as integer = CreateWindowEx(WS_EX_APPWINDOW Or WS_EX_WINDOWEDGE, "MyWindow", "MyTitle", WS_OVERLAPPEDWINDOW, 0, 0, 640, 480, 0, 0, AppHandle, Nothing)

CreateWindowEx returns 0?

+2  A: 

You need to check for NULL after calling CreateWindowEx, and if it is, you can then immediately call Marshal.GetLastWin32Error for the reason why.

Nick
GetLastWin32Error returns 1407
PeanutPower
ERROR_CANNOT_FIND_WND_CLASS1407 (0x57F)Cannot find window class.
PeanutPower
+2  A: 

Possible reasons are that you forgot to register the "MyWindow" window class with RegisterClass/Ex(), didn't set the window procedure correctly or don't properly handle the WM_CREATE message. Also, your P/Invoke declaration is wrong, it won't work on 64-bit operating systems.

Don't write this kind of code yourself, Windows Forms is a very nice wrapper around CreateWindowEx().

Hans Passant