flat assembler
Message board for the users of flat assembler.

Index > Windows > Can't figure it out

Author
Thread Post new topic Reply to topic
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak
Every time i try to manually retype this example by Iczelion, it ends up not working. The program dosn't crash, but the window never shows up. I know, pretty bad screwing up the second lesson in a tutorial, but perhaps if i saw what i was doing wrong i could stop making this error every time i retype this thing. I can't learn the win32 api if i can't even get this down.

Code:
use32
format PE gui 4.0

include '%fasminc%\win32ax.inc'

entry main
;----------------------------------------------------------------------------------------
macro initapp Thandle {
      ;program initialization
      invoke GetModuleHandle, 0 ;Gets our program's location in memory
      mov [Thandle], eax ;puts it in Thandle
      }
;----------------------------------------------------------------------------------------
macro initwnd phandle, wincls, style, hwndfnc, wClsName {
      mov  [wincls+WNDCLASSEX.hInstance],phandle
      mov  [wincls+WNDCLASSEX.style],style
      mov  [wincls+WNDCLASSEX.lpfnWndProc],hwndfnc
      mov  [wincls+WNDCLASSEX.lpszClassName],wClsName
      mov  [wincls+WNDCLASSEX.hbrBackground],COLOR_WINDOW+1
      invoke    LoadIcon,NULL,IDI_APPLICATION
      mov  [wincls+WNDCLASSEX.hIcon],eax
      invoke    LoadCursor,NULL,IDC_ARROW
      mov  [wincls+WNDCLASSEX.hCursor],eax
      invoke    RegisterClass,wincls }
;----------------------------------------------------------------------------------------
macro msgloop {
.msgloop:
invoke  GetMessage, msg, 0, 0, 0
        or eax, eax
        jz .endmsgloop
invoke  TranslateMessage, msg
invoke  DispatchMessage, msg
        jmp .msgloop
.endmsgloop: }
;----------------------------------------------------------------------------------------
section '.code' readable writeable executable
;-----------------------START OF PROGRAM-----------------------------
;--------------------Window Variables--------------------
Title file 'ram.txt'
ClassName du "KOHLRAK" ;Never actually seen, so i put that there for uniqueness.
WindowClass WNDCLASSEX ;Our window class
msg MSG ;Variable that holds information of what is to be done with the window.
hwnd dd ? ;handle for our window
;Program Variables
Thandle dd ? ;handle for our program
;--------------------START--------------------
main:
        initapp Thandle
        initwnd eax, WindowClass,CS_HREDRAW or CS_VREDRAW, Window_Handling_Func, ClassName

invoke  CreateWindowEx, NULL,\
               ClassName,\
               Title,\
               WS_OVERLAPPEDWINDOW,\
               CW_USEDEFAULT,\
               CW_USEDEFAULT,\
               CW_USEDEFAULT,\
               CW_USEDEFAULT,\
               NULL,\
               NULL,\
               Thandle,\
               NULL

invoke  ShowWindow, [hwnd], SW_SHOW
invoke  UpdateWindow, [hwnd]

        msgloop

invoke ExitProcess, 0
;------------------WINDOW HANDLING FUNCTION------------------------
proc Window_Handling_Func, cWnd, cMsg, cWparam, cLparam
          push ebx esi edi ;Safety
          cmp [cMsg], WM_DESTROY
          jne .noclose
invoke    PostQuitMessage, 0
.noclose:
invoke    DefWindowProc, [cWnd], [cMsg], [cWparam], [cLparam]
          pop edi esi ebx
          ret
endp
;------------------------------------------------------------------

section '.idata' readable writeable data import

library kernel, 'kernel32.dll',\
        user, 'user32.dll'

import kernel, ExitProcess, 'ExitProcess',\
               GetModuleHandle, 'GetModuleHandleW'

import user, LoadIcon, 'LoadIconW',\
             LoadCursor, 'LoadCursorW',\
             RegisterClass, 'RegisterClassW',\
             CreateWindowEx, 'CreateWindowExW',\
             ShowWindow, 'ShowWindow',\
             GetMessage, 'GetMessageW',\
             TranslateMessage, 'TranslateMessage',\
             DispatchMessage, 'DispatchMessageW',\
             PostQuitMessage, 'PostQuitMessage',\
             DefWindowProc, 'DefWindowProcW',\
             UpdateWindow, 'UpdateWindow'    


Thanks in advance.
Post 13 Jan 2007, 17:59
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
You are using ShowWindow and UpdateWindow with a never initialised variable. add "mov [hwnd], eax" after window creation. On the registration class add
Code:
      mov  [wincls+WNDCLASS.cbClsExtra], 0
      mov  [wincls+WNDCLASS.cbWndExtra], 0 
      mov  [wincls+WNDCLASS.lpszMenuName], NULL    


And NULL terminate the strings.
Code:
ClassName du "KOHLRAK",0    


Note that your txt must be unicode and null terminated (probably not very common), but size you are using it to set the tittle bar text which need a single line of text better don't use an external TXT file and do something like
Code:
Title du 'KOHLRAK', 0    


[edit]I forgot the most important part, replace all occurrencies of WNDCLASSEX with WNDCLASS[/edit]
Post 13 Jan 2007, 18:36
View user's profile Send private message Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak
LocoDelAssembly wrote:
You are using ShowWindow and UpdateWindow with a never initialised variable. add "mov [hwnd], eax" after window creation. On the registration class add
Code:
      mov  [wincls+WNDCLASS.cbClsExtra], 0
      mov  [wincls+WNDCLASS.cbWndExtra], 0 
      mov  [wincls+WNDCLASS.lpszMenuName], NULL    



oops... And shouldn't the cbWndExtra and lpszMenuName be 0ed by default?

Quote:
And NULL terminate the strings.
Code:
ClassName du "KOHLRAK",0    


I can't beleive that i forgot to null terminate that thing, anyway...

Quote:
Note that your txt must be unicode and null terminated (probably not very common), but size you are using it to set the tittle bar text which need a single line of text better don't use an external TXT file and do something like
Code:
Title du 'KOHLRAK', 0    


I did null terminate the TXT file, i made sure that i did that. lol

Quote:
[edit]I forgot the most important part, replace all occurrencies of WNDCLASSEX with WNDCLASS[/edit]


That deffinately threw me off... But why dosn't WNDCLASSEX work for that...?


Last edited by kohlrak on 13 Jan 2007, 19:16; edited 1 time in total
Post 13 Jan 2007, 19:11
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Because you are calling RegisterClass instead of RegisterClassEx
Post 13 Jan 2007, 19:16
View user's profile Send private message Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak
oooooooooooh... Well... after all that, my code is now

Code:
use32
format PE gui 4.0

include '%fasminc%\win32ax.inc'

entry main
;----------------------------------------------------------------------------------------
macro initapp Thandle {
      ;program initialization
      invoke GetModuleHandle, 0 ;Gets our program's location in memory
      mov [Thandle], eax ;puts it in Thandle
      }
;----------------------------------------------------------------------------------------
macro initwnd phandle, wincls, style, hwndfnc, wClsName {
      mov  [wincls+WNDCLASSEX.hInstance],phandle
      mov  [wincls+WNDCLASSEX.style],style
      mov  [wincls+WNDCLASSEX.lpfnWndProc],hwndfnc
      mov  [wincls+WNDCLASSEX.lpszClassName],wClsName
      mov  [wincls+WNDCLASSEX.hbrBackground],COLOR_WINDOW+1
      invoke    LoadIcon,NULL,IDI_APPLICATION
      mov  [wincls+WNDCLASSEX.hIcon],eax
      invoke    LoadCursor,NULL,IDC_ARROW
      mov  [wincls+WNDCLASSEX.hCursor],eax
      invoke    RegisterClass,wincls
      mov  [wincls+WNDCLASS.cbClsExtra], 0
      mov  [wincls+WNDCLASS.cbWndExtra], 0
      mov  [wincls+WNDCLASS.lpszMenuName], NULL}
;----------------------------------------------------------------------------------------
macro msgloop {
.msgloop:
invoke  GetMessage, msg, 0, 0, 0
        or eax, eax
        jz .endmsgloop
invoke  TranslateMessage, msg
invoke  DispatchMessage, msg
        jmp .msgloop
.endmsgloop: }
;----------------------------------------------------------------------------------------
section '.code' readable writeable executable
;-----------------------START OF PROGRAM-----------------------------
;--------------------Window Variables--------------------
Title du "KOHLRAK", 0
ClassName du "KOHLRAK",0 ;Never actually seen, so i put that there for uniqueness.
WindowClass WNDCLASS ;Our window class
msg MSG ;Variable that holds information of what is to be done with the window.
hwnd dd ? ;handle for our window
;Program Variables
Thandle dd ? ;handle for our program
;--------------------START--------------------
main:
        initapp Thandle
        initwnd eax, WindowClass,CS_HREDRAW or CS_VREDRAW, Window_Handling_Func, ClassName

invoke  CreateWindowEx, NULL,\
               ClassName,\
               Title,\
               WS_OVERLAPPEDWINDOW,\
               CW_USEDEFAULT,\
               CW_USEDEFAULT,\
               CW_USEDEFAULT,\
               CW_USEDEFAULT,\
               NULL,\
               NULL,\
               Thandle,\
               NULL
               mov [hwnd], eax

invoke  ShowWindow, [hwnd], SW_SHOW
invoke  UpdateWindow, [hwnd]

        msgloop

invoke ExitProcess, 0
;------------------WINDOW HANDLING FUNCTION------------------------
proc Window_Handling_Func, cWnd, cMsg, cWparam, cLparam
          push ebx esi edi ;Safety
          cmp [cMsg], WM_DESTROY
          jne .noclose
invoke    PostQuitMessage, 0
.noclose:
invoke    DefWindowProc, [cWnd], [cMsg], [cWparam], [cLparam]
          pop edi esi ebx
          ret
endp
;------------------------------------------------------------------

section '.idata' readable writeable data import

library kernel, 'kernel32.dll',\
        user, 'user32.dll'

import kernel, ExitProcess, 'ExitProcess',\
               GetModuleHandle, 'GetModuleHandleW'

import user, LoadIcon, 'LoadIconW',\
             LoadCursor, 'LoadCursorW',\
             RegisterClass, 'RegisterClassW',\
             CreateWindowEx, 'CreateWindowExW',\
             ShowWindow, 'ShowWindow',\
             GetMessage, 'GetMessageW',\
             TranslateMessage, 'TranslateMessage',\
             DispatchMessage, 'DispatchMessageW',\
             PostQuitMessage, 'PostQuitMessage',\
             DefWindowProc, 'DefWindowProcW',\
             UpdateWindow, 'UpdateWindow'    


Still dosn't show the window...
Post 13 Jan 2007, 19:22
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
LocoDelAssembly wrote:

[edit]I forgot the most important part, replace all occurrencies of WNDCLASSEX with WNDCLASS[/edit]


Quote:
oops... And shouldn't the cbWndExtra and lpszMenuName be 0ed by default?

I assumed you want a code that will work even when the structure resides in run-time allocated memory or in stack memory where you can't trust that the memory is zero initialised.
Post 13 Jan 2007, 19:40
View user's profile Send private message Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak
Oops... finally i get it runnin'. lol See, i'm looking at a tut by iczellion and some other guy's fasm rewrite of the tutorial, but some things are slightly different between the 2 tutorials, and i guess that's where my errors are comming from.

Thanks, bud.
Post 13 Jan 2007, 19:48
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.