I am just starting to pick up win32 programming.
This is how a basic messagebox looks like
invoke MessageBox, NULL, ADDR MsgText, ADDR MsgTitle, MB_OK
The last parameter pushed before the call to messagebox is supposed to be "Handle to the owner window of the message box to be created. If this parameter is NULL, the message box has no owner window."
Can anyone explains to me what handle means basically? In this case, is it supposed to be an address or something?
Can someone provide to me an example code (in masm) what it looks like if i dont use NULL as the parameter.
Thank you for your time.
Hi techno,
Welcome on board. A handle is just a unique ID and with a function like MessageBox, the handle is usually the parent window that calls the message box. When you use handle "0" it defaults to the desktop which in effect means it has no parent.
Thanks hutch for the help.
A handle is a unique ID? Can you show me some code examples where the handle is not 0 / NULL. All the messagebox examples i have seen so far uses NULL for its handle. Much thanks.
Hello Technomancer. A handle is simply a DWORD or four bytes. It can be any value. You get a handle by querying a window (or control, file, device etc) for a handle value. Here's an example of how a window handle is obtained:
MyProc PROC uses esi hWin:DWORD,uMsg:DWORD,wParam:DWORD,lParam:DWORD
mov eax,hWin
mov MyWindowHandle,eax
...
MyProc ENDP
Now MyWindowHandle contains the proper value (handle) of the MyProc window. When you plug MyWindowHandle into MessageBox, the messagebox becomes a "child" of MyWindowHandle. With a NULL it has no parent so is treated like a separate program window.
Some handles must be freed, like GlobalAlloc. These are different in that the "handle" returned is actually a pointer to the reserved memory location. Here's one way to allocate and free 256kB of RAM:
invoke GlobalAlloc,GPTR,262144
mov MyMemHandle,eax
... do something with the memory...
invoke GlobalFree,MyMemHandle
Correct me if i'm wrong but as i know most of the handles are unique only for the calling process while IDs are globally unique for all processes.
Hi asmfan,
Handles are global in nature, pseudo-handles (ie hProcess (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getcurrentprocess.asp)) are local to your process. Many people do not distinguish between the two but they are treated differently at MSDN. Memory handles are always local.
Thanks Edgar.