News:

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

Few doubts in WinMain fuction parameters

Started by b0nd, January 19, 2010, 09:34:09 AM

Previous topic - Next topic

b0nd

Hello All,

Ok, first of all the declaration that I am new to assembly and has started with some basic tutorials and iczelion's series and using masm and radasm.

I've highlighted the code where I've doubt and has deleted much of the code to keep post shorter.



[u]WinMain proto :DWORD,:DWORD,:DWORD,:DWORD[/u]   <---------------- Here

.data?
   hInstance HINSTANCE ?
   CommandLine LPSTR ?

.code

start:
invoke GetModuleHandle, NULL
mov    hInstance,eax

        [u]invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT[/u]   <------------------------- Here
invoke ExitProcess,eax

WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
LOCAL wc:WNDCLASSEX
LOCAL msg:MSG
LOCAL hwnd:HWND

mov   wc.cbSize,SIZEOF WNDCLASSEX
mov   wc.style, CS_HREDRAW or CS_VREDRAW
mov   wc.lpfnWndProc, OFFSET WndProc
mov   wc.cbClsExtra,NULL
mov   wc.cbWndExtra,NULL
      [u]  push  hInstance  [/u]                                   <-----------------------------    Here
pop   wc.hInstance

invoke RegisterClassEx, addr wc
INVOKE CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,\
           WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,\
           CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,\
           hInst,NULL
mov   hwnd,eax

[u]WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM[/u]   <--------------------- Here

.IF uMsg==WM_DESTROY
invoke PostQuitMessage,NULL
.ELSEIF uMsg==WM_CREATE
;
.ELSE
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
ret
.ENDIF

xor eax,eax
ret
WndProc endp

end start


1. The prototype of WinMain. I understand that it would be called later in program, so need to tell the prototype of it. But then why not they give the prototype of WndProc at the beginning of program? Is it because it was never called by passing parameters to it and was only used here "mov   wc.lpfnWndProc, OFFSET WndProc"

2. At the lines "push hInstance and pop  wc.hInstance". Should that  be "hInst" with push instead of hInstance? Because that was passed into the "hInst" parameter of WinMain. So WinMain should be aware of "hInst" instead of "hInstance". Here I've noticed that it doesn't matter and program is running on both the occasions. I am just seeking clarification.


Thanks in advance

hutch--

bOnd,

The PUSH/POP is just a technique to copy a memory operand to another memory operand as there is no direct mnemonic to do it. It can also be done through a register in speed critical code but loading a structure is hardly a problem.

In MASM if you want to have a stack frame, locals and get the arguments by name you put it into a PROC and for a PROC you need a PROTOtype.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

b0nd

Quote from: hutch-- on January 19, 2010, 10:45:05 AM
bOnd,

The PUSH/POP is just a technique to copy a memory operand to another memory operand as there is no direct mnemonic to do it. It can also be done through a register in speed critical code but loading a structure is hardly a problem.


I got your point hutch, here my concern is about the parameter "hInstance".
How WinMain knows about it? My understanding says, it being declared in .data? section is some what a global variable. Otherwise it was taken in the variable "hInst" while calling the WinMain function. That's why whether I use "hInstance" or just "hInst", program is running smoothly.

Am I right?

BogdanOntanu

Quote from: b0nd on January 19, 2010, 11:22:39 AM
...
How WinMain knows about it? My understanding says, it being declared in .data? section is some what a global variable. Otherwise it was taken in the variable "hInst" while calling the WinMain function. That's why whether I use "hInstance" or just "hInst", program is running smoothly.

Am I right?

Yes.

hInstance is a global variable and hInst is an argument (thus local to it's PROC) but in this code they both have the same value.
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

hutch--

bOnd,

The app is not coded properly but it actually does not matter as Bogdan suggested. The "hInstance" global value is passed to the WinMain procedure where "hinst" is a local value but then its not used. Hardly a problem but if you were being tidy about it you would either use the GLOBAL and not have a WinMain OR you wo7uld use the local in its scope in the WinMain.

Unused locals are a common redundancy in programs that usually don't matter much but if you like to write tidy accurate code, its a good idea not to write unused values on the stack.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

b0nd

Got it guys!!

Thanks for clearing out the doubts. This was the dummy code generated by "win32EXE.tpl" template of RadASM. I was analyzing it.


Thanks :)