Hi i posted my question in comp.lang.asm.x86 and a person replied to try ask here. Here ihave searched for the word WM_QUERYENDSESSION and found about 10 topics but unfortunately none gave me enough to solve my problem. Probably because i'm a bit noop with assembler  :sad:. However i hope someone here can help me with this problem. If you have paypal i'm also willing to pay/transfer upto 30$ for a complete solution.

In an application i need a little code (filesaving) which must be executed when a user shut down the computer (xp or vista). To detect a shutdown i know that the method to use is WM_QUERYENDSESSION as shown in link below:


But it must be implemented as a callback and i have no idea how to do it in
assembler. I use masm compiler and will be very glad if anyone can help me
with this piece of code? It can be a problem that my program doesn't has a window or dos shell and i'm not sure that it is possible to solve this problem wihout having an invisible window or dos shell? If soo it must be invisible when using Alt-Tab also.

Posted on 2007-06-09 10:53:00 by maxleif
Hi maxlief
You don?t have to pay us to help you. The reason why we are here is to help each other.

Take a look at any example of the package from where you got MASM that intercepts a WM_XXX message from the message pump. Add a new one for WM_QUERYENDSESSION and handle there your file saving.

Another place to look for is OA32. As far as I remember, nearly all demos include an interceptor for this message.



Posted on 2007-06-09 11:43:55 by Biterider
I had a similar situation, saving the data to a file when shutdown is detected,
but the program has a window.  Using WM_ENDSESSION worked.


            invoke CreateFile,ADDR szFName,GENERIC_WRITE,

            mov hFileOut, eax
            mov bWritten, 0
            mov filelen, 2048  ; going to write a 2048 byte file

            invoke WriteFile,hFileOut,ADDR DataSpot,filelen,ADDR bWritten,NULL
            invoke CloseHandle,hFileOut
            invoke PostQuitMessage,NULL
Posted on 2007-06-09 15:36:33 by dsouza123
Thanks for replies. I tried to look in the masm samples (EXAMPLE1, EXAMPLE2....) and i found out that many of the samples contains code like:

CallModalDialog hInstance,0,DlgProc,NULL

DlgProc proc hWin:DWORD,uMsg:DWORD,wParam:DWORD,lParam:DWORD
    Switch uMsg
        ......        return 1
      Case WM_COMMAND
    return 0
DlgProc endp

but i think this is the call to make a window. Is it the same call i must do to detect WM_ENDSESSION? or is it another call. Unfortunately google-searching for "uMsg == WM_ENDSESSION" doesn't gives any assembler samples so i'm a bit lost here. I tried looking for OA32 but not sure where to find about it. About money i know it's help to help  :) but i just though this is a tough one because i can't find any samples eventhough it should be a pretty wellknown function to manage a shutdown.
Posted on 2007-06-10 08:05:50 by maxleif
Hi maxlief
All messages sent from the OS to the app, regardless of the type of app, go through the message pump. Most of the times, it routes the messages to a procedure that identifies the messages and takes appropriate actions.
The example you took is correct. Simply add a ?Case WM_QUERYENDSESSION? and the code you need to save your files. That?s all.

If you are using OA32, take a look at ?\ObjAsm32\Examples\Demo03\Demo03.inc, line 51.
There you can find the definition for an event handler (OnClose) that is fired when the app receives and WM_CLOSE or WM_QUERYENDMESSAGE message.

Posted on 2007-06-10 10:49:53 by Biterider

WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
.IF    uMsg==WM_CREATE
            some other code
            already shown
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
      some code
WndProc endp
Posted on 2007-06-10 22:18:15 by dsouza123
Cool thanks a lot. I will try implement it later. Seems to be easy  :D
Posted on 2007-06-13 04:24:19 by maxleif