flat assembler
Message board for the users of flat assembler.

Index > Windows > Message loop

Author
Thread Post new topic Reply to topic
Sasha



Joined: 17 Nov 2011
Posts: 93
Sasha 03 Jun 2013, 14:30
GetMessage returns zero when the wm_quit is posted. It returns -1 on error, and nonzero when there is other messages. Nonzero doesn't mean 1

The code from the Template example.
Code:
  msg_loop:
        invoke  GetMessage,msg,NULL,0,0
        cmp     eax,1
        jb      end_loop  
        jne     msg_loop  ;we are in dead loop when GetMessage returns -1

;we are here when eax==1

        invoke  TranslateMessage,msg
        invoke  DispatchMessage,msg
        jmp     msg_loop

  error:
        invoke  MessageBox,NULL,_error,NULL,MB_ICONERROR+MB_OK

  end_loop:        ;eax=0
        invoke  ExitProcess,[msg.wParam]               
    


This example works. That means, that GetMessage actually returns 1(And not any other NONZERO). But the right way to do somethig like this.(Actually, I use this code)

Code:
  messageLoop:
        invoke  GetMessage,Msg,0,0,0
        cmp     eax,0 
        jle     EndLoop
        invoke  TranslateMessage,Msg ;eax>0 signed
        invoke  DispatchMessage,Msg
        jmp     MessageLoop

  endLoop:
        jnz     GetMessageError ;eax==-1 

        invoke  ExitProcess,[Msg.wParam] ;eax==0 
    
Post 03 Jun 2013, 14:30
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4060
Location: vpcmpistri
bitRAKE 03 Jun 2013, 14:36

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 03 Jun 2013, 14:36
View user's profile Send private message Visit poster's website Reply with quote
Sasha



Joined: 17 Nov 2011
Posts: 93
Sasha 03 Jun 2013, 14:46
Just after posting this, I understood, that my axample goes to error on ALL negative numbers. Here is another, better example:
Code:
 MessageLoop:
        invoke  GetMessage,Msg,0,0,0
        or      eax,eax
        je      finish
        inc     eax
        je      GetMessageError
        invoke  TranslateMessage,Msg
        invoke  DispatchMessage,Msg
        jmp     MessageLoop                                                                       
    
Post 03 Jun 2013, 14:46
View user's profile Send private message Reply with quote
Sasha



Joined: 17 Nov 2011
Posts: 93
Sasha 03 Jun 2013, 14:55
bitRAKE wrote:
http://blogs.msdn.com/b/oldnewthing/archive/2013/03/22/10404367.aspx


Thanks. Your article says, that if you're sure in the parameters you are passing to GetMessage, means they are hardcoded and tested in compiler time you will never get a -1
Post 03 Jun 2013, 14:55
View user's profile Send private message Reply with quote
Sasha



Joined: 17 Nov 2011
Posts: 93
Sasha 03 Jun 2013, 15:07
The code in Ddraw example and also, I've seen this somewhere else, uses GetMessage after the PeekMessage shows that there is a message pending. Why not just get it with PM_REMOVE?

Code:
  messageLoop:
        invoke  PeekMessage,Msg,NULL,0,0,PM_REMOVE
        or      eax,eax
        jne     ismessage

  dosomething:

;Do something

        jmp     messageloop

  ismessage:
        cmp     [Msg.message],WM_QUIT
        je      finish
        invoke  TranslateMessage,Msg
        invoke  DispatchMessage,Msg
        jmp     messageloop

  finish:
        invoke  ExitProcess,[Msg.wParam]             
    
Post 03 Jun 2013, 15:07
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 04 Jun 2013, 21:16
Sasha wrote:
Thanks. Your article says, that if you're sure in the parameters you are passing to GetMessage, means they are hardcoded and tested in compiler time you will never get a -1
There is more to it. I made a quick glance at my User32.DLL, 6.1.7601.17514 (win7sp1_rtm.101119-1850). It reveals something quite different from what MSDN says about GetMessageA(): if wMsgFilterMin has any bit higher than 16th set (seems funny because Hungarian prefix w implies that even 16th bit can't be set Wink) or wMsgFilterMax too, except when wMsgFilterMax==-1 (i.e. condition is wMsgFilterMin & ~0x1FFFF || wMsgFilterMax & ~0x1FFFF && wMsgFilterMax != -1), function sets ERROR_INVALID_PARAMETER status and returns 0 (not -1 as one may expect).

As about PeekMessage(…, PM_REMOVE), it's better to handle input in separate thread than to intersperse output routine with occasional input checks. Your suggestion seems pretty valid though (another method may be using WM_TIMER for redraws).
Post 04 Jun 2013, 21:16
View user's profile Send private message Reply with quote
Sasha



Joined: 17 Nov 2011
Posts: 93
Sasha 05 Jun 2013, 12:12
I'm thinking about putting the entire window creation to a separate thread. To have something like CreateWindowAsync.
I think that filtering messages and hwnds is not a good thing.
Post 05 Jun 2013, 12:12
View user's profile Send private message 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.