News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

windows error

Started by ragdog, December 13, 2007, 04:42:55 PM

Previous topic - Next topic

ragdog

hi

i have a problem with createwindow i compile this and cannot create window with this error: Could not create window


DEFHEIGHT  EQU 500
DEFWIDTH   EQU 300

WinMain PROC hInstance:DWORD, hPrevInstance:DWORD, CmdLn:DWORD, dwShow:DWORD
LOCAL wndcls:WNDCLASSEX
LOCAL message:MSG
LOCAL rct:RECT

mov wndcls.cbSize, SIZEOF WNDCLASSEX
mov wndcls.style, CS_VREDRAW or CS_HREDRAW or CS_BYTEALIGNWINDOW
mov wndcls.lpfnWndProc, OFFSET WndProc
mov wndcls.lpszClassName, OFFSET g_ClassName
mov wndcls.lpszMenuName, NULL
m2m wndcls.hInstance, hInstance
mov wndcls.hbrBackground, ( COLOR_BTNFACE + 1 )

INVOKE LoadIcon, NULL, IDI_APPLICATION
mov wndcls.hIcon, EAX
mov wndcls.hIconSm, EAX

INVOKE LoadCursor, NULL, IDC_ARROW
mov wndcls.hCursor, EAX

INVOKE RegisterClassEx, ADDR wndcls

INVOKE GetSystemMetrics, SM_CXSCREEN
push EAX
INVOKE GetSystemMetrics, SM_CYSCREEN
pop ECX
shr ECX, 1
shr EAX, 1
sub ECX, ( DEFHEIGHT / 2 )
sub EAX, ( DEFWIDTH / 2 )

INVOKE CreateWindowEx, WS_EX_OVERLAPPEDWINDOW, \
OFFSET g_ClassName, OFFSET g_AppTitle, \
WS_OVERLAPPEDWINDOW or WS_VISIBLE, \
ECX, EAX, DEFHEIGHT, DEFWIDTH, NULL, NULL, g_hInstance, NULL


mov g_hWindow, EAX
.IF EAX == NULL
INVOKE MessageBox, NULL, CTEXT("Could not create window"), NULL, ( MB_OK + MB_ICONERROR )
ret
.ENDIF

INVOKE GetClientRect, g_hWindow, ADDR rct
mov rct.top, 20
sub rct.bottom, 20
sub rct.right, 50
INVOKE CreateWindowEx, NULL, \
CTEXT("EDIT"), NULL, \
WS_CHILD or WS_VISIBLE or WS_BORDER or ES_LEFT or ES_AUTOHSCROLL, \
rct.left, rct.bottom, rct.right, rct.top, g_hWindow, 300, g_hInstance, NULL
mov g_hTextEdit, EAX
.IF EAX == NULL
INVOKE MessageBox, NULL, CTEXT("Could not create edit control"), NULL, ( MB_OK + MB_ICONERROR )
ret
.ENDIF

INVOKE GetClientRect, g_hWindow, ADDR rct
sub rct.bottom, 20
sub rct.right, 50
INVOKE CreateWindowEx, NULL, \
CTEXT("BUTTON"), CTEXT("Send"), \
WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON, \
rct.right, rct.bottom, 50, 20, g_hWindow, 400, g_hInstance, NULL
mov g_hSendText, EAX

INVOKE ShowWindow, g_hWindow, dwShow
INVOKE UpdateWindow, g_hWindow

MainLoop:

INVOKE GetMessage, ADDR message, NULL, NULL, NULL
.IF EAX != 0
.IF message.message == WM_KEYDOWN
.IF message.wParam == VK_RETURN
INVOKE SendMessage, g_hWindow, WM_COMMAND, 400, NULL
jmp MainLoop
.ENDIF
.ENDIF
INVOKE TranslateMessage, ADDR message
INVOKE DispatchMessage, ADDR message
jmp MainLoop
.ENDIF

mov EAX, message.wParam
ret

WinMain ENDP



donkey

Hi RagDog,

The error you are getting is one that you have generated if for any reason at all the handle returned from CreateWindowEx is NULL, you should use the Windows error codes, it will make things a little clearer.

ErrorString DB 256 DUP (?)
...
invoke GetLastError
test eax, eax
jz @F
invoke FormatMessage,FORMAT_MESSAGE_FROM_SYSTEM, 0, eax, 0, offset ErrorString, SIZEOF ErrorString-1, 0
invoke MessageBox, 0, offset ErrorString, 0, 0
@@:


I would also check for an error from RegisterClassEx as that is also a source of many window creation problems

Donkey
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

Larry Hammick

INVOKE CreateWindowEx, WS_EX_OVERLAPPEDWINDOW, \
OFFSET g_ClassName, OFFSET g_AppTitle, \        ;you want ADDR and not OFFSET here
WS_OVERLAPPEDWINDOW or WS_VISIBLE, \
ECX, EAX, DEFHEIGHT, DEFWIDTH, NULL, NULL, g_hInstance, NULL

Tedd

Quote from: Larry Hammick on December 17, 2007, 09:03:56 PM
INVOKE CreateWindowEx, WS_EX_OVERLAPPEDWINDOW, \
OFFSET g_ClassName, OFFSET g_AppTitle, \        ;you want ADDR and not OFFSET here
WS_OVERLAPPEDWINDOW or WS_VISIBLE, \
ECX, EAX, DEFHEIGHT, DEFWIDTH, NULL, NULL, g_hInstance, NULL

That will make no difference whatsoever.


The problem is RegisterClass - you didn't fill your WNDCLASSEX struct correctly :P
("wndcls.cbClsExtra" and "wndcls.cbWndExtra" should be zeroed if not used)
No snowflake in an avalanche feels responsible.

ToutEnMasm


A local variable is fill with the content of the stack and you have two choice.
Used a macro like ZEROLOCALES to initialise all the locales to zero.
Or initialise all the variables of the structure,one by one.
Take care also that the named class (registerclassex) must be unique.