flat assembler
Message board for the users of flat assembler.

Index > Windows > Message loop without window

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
bcdsys



Joined: 04 Aug 2008
Posts: 41
bcdsys 20 Sep 2008, 14:02
howdo I create message loop without windw? I want to make program that handles WM_TIMER and WM_HOTKEY messages, so I need message loop. This program does not have window, so how to register message handler proc? Invisible Window? Is there anyway to determine message passed without using DispatchMessage? What is the code to do that?
Post 20 Sep 2008, 14:02
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 20 Sep 2008, 14:05
Re-read SetTimer documentation, you'll see that it is not always required to have a WndProc.
Post 20 Sep 2008, 14:05
View user's profile Send private message Reply with quote
asmcoder



Joined: 02 Jun 2008
Posts: 784
asmcoder 20 Sep 2008, 17:51
[content deleted]


Last edited by asmcoder on 14 Aug 2009, 14:56; edited 1 time in total
Post 20 Sep 2008, 17:51
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 20 Sep 2008, 17:58
&/%$/&$%$"!!!!!
Post 20 Sep 2008, 17:58
View user's profile Send private message Reply with quote
dxl



Joined: 17 Sep 2005
Posts: 16
dxl 20 Sep 2008, 21:24
a window is a Windows object and it don't need to be visible.
Post 20 Sep 2008, 21:24
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4073
Location: vpcmpistri
bitRAKE 21 Sep 2008, 03:07
I wonder how many of the 1,100+ handles currently allocated are invisible windows? Don't worry about creating another one.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 21 Sep 2008, 03:07
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8359
Location: Kraków, Poland
Tomasz Grysztar 21 Sep 2008, 10:30
Have you ever tried to make a small program going through all the parent-less windows in system and applying WS_VISIBLE to them? Results can be funny. Wink

As long as having to restart your system is not too much not-funny for you.
Post 21 Sep 2008, 10:30
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 21 Sep 2008, 11:47
Tomasz Grysztar wrote:
Have you ever tried to make a small program going through all the parent-less windows in system and applying WS_VISIBLE to them? Results can be funny.
I remember a small utility written for Win95 that would display all the handles (including windows, bitmaps, icons etc.) in a window. It used the arrow keys to cycle through each object one-by-one. I wonder if it could be adapted for WinNT based OS's? I expect it would need to run inside the kernel for it to work properly.
Post 21 Sep 2008, 11:47
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 21 Sep 2008, 13:47
Code:
include 'win32axp.inc'

DELAY = 1000

proc start
local msg: MSG
  xor    edi, edi
  lea    ebx, [msg]

  invoke SetTimer, edi, edi, DELAY, TimerProc
  jmp    .getMessage

.messageLoop:
  invoke DispatchMessage, ebx

.getMessage:
  invoke GetMessage, ebx, edi, edi, edi
  sub    eax, -1
  cmp    eax, 1
  ja    .messageLoop

  invoke ExitProcess, edi
  ; No asmcoder, I won't use ret in place of ExitProcess, don't waste your time posting
endp

proc TimerProc hwnd, uMsg, idEvent, dwTime

  inc    [count]
  cmp    [count], 5
  je     .quit

  invoke MessageBox, 0, "It got called!!", "Windowless timer test", 0
.exit:
  ret

.quit:
  invoke PostQuitMessage, 0
  jmp    .exit
endp

count dd 0

.end start

;Note that the function return value can be TRUE, FALSE, or -1. Thus, you should avoid code like this:

;while (GetMessage( lpMsg, hWnd, 0, 0)) ...  
 

;The possibility of a -1 return value means that such code can lead to fatal application errors.
    


But if you need a window create it then, it is not wrong doing that and in fact you can even create a window that it will be used specifically for message retrieval, but that may not work on Windows versions prior to 2000 (MSDN does not specify if Vista supports this, though: "Windows 2000/XP: To create a message-only window, supply HWND_MESSAGE or a handle to an existing message-only window.").

[edit]Fixed a bug in the GetMessage return address checker


Last edited by LocoDelAssembly on 21 Sep 2008, 15:33; edited 1 time in total
Post 21 Sep 2008, 13:47
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4073
Location: vpcmpistri
bitRAKE 21 Sep 2008, 14:51
Although, maybe not as thorough as another solution, I like to use WinSpy++ - source availible and well maintained. Parent and child windows can be easily navigated and changed.

...I don't think there is any reason to change EAX on return from GetMessage.
Code:
.messageLoop: 
  invoke DispatchMessage, ebx 

.getMessage: 
  invoke GetMessage, ebx, edi, edi, edi 
  test eax,eax ; -1,0,1
  jg .messageLoop
; jne .error
; je .exit    
Svin, has an excellent utility which has helped me greatly with which branch to use. [1][2]
(reading his source code is very educational, too Laughing )

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup


Last edited by bitRAKE on 22 Sep 2008, 01:17; edited 2 times in total
Post 21 Sep 2008, 14:51
View user's profile Send private message Visit poster's website Reply with quote
asmfan



Joined: 11 Aug 2006
Posts: 392
Location: Russian
asmfan 21 Sep 2008, 15:04
RegisterHotKey with 0
Quote:
If this parameter is NULL, WM_HOTKEY messages are posted to the message queue of the calling thread and must be processed in the message loop.

Hence no window needed. Thread will receive thread-only messages via message loop if there are no windows created.

_________________
Any offers?
Post 21 Sep 2008, 15:04
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 21 Sep 2008, 15:09
Excellent bitRAKE!! Very Happy But, can we be sure that it will work always? Look the documentation
Quote:
Return Value

If the function retrieves a message other than WM_QUIT, the return value is nonzero.

If the function retrieves the WM_QUIT message, the return value is zero.

If there is an error, the return value is -1. For example, the function fails if hWnd is an invalid window handle or lpMsg is an invalid pointer. To get extended error information, call GetLastError.


In the "return value is nonzero" part, can we be sure that it will always be greater (signed) than zero (except for ONLY the -1 case)?

Your link [2] doesn't work (says "The requested URL /~files19/wasm.ru/wasm.ru/tools/22/jcc.zip was not found on this server."), and the first one is all in Russian so I don't know what I have to download Embarassed
Post 21 Sep 2008, 15:09
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 21 Sep 2008, 15:11
asmfan wrote:
RegisterHotKey with 0
Quote:
If this parameter is NULL, WM_HOTKEY messages are posted to the message queue of the calling thread and must be processed in the message loop.

Hence no window needed. Thread will receive thread-only messages via message loop if there are no windows created.
But without a process window how can you get keystrokes?
Post 21 Sep 2008, 15:11
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4073
Location: vpcmpistri
bitRAKE 21 Sep 2008, 15:17
LocoDelAssembly wrote:
But, can we be sure that it will work always?
True, it would not work for messages in the range [80000000,FFFFFFFE]. Seems safe for some years.
Code:
0 through WM_USER        Messages reserved for use by the system.
WM_USER through 0x7FFF   Integer messages for use by private window classes.
WM_APP through 0xBFFF    Messages available for use by applications.
0xC000 through 0xFFFF    String messages for use by applications.
Greater than 0xFFFF      Reserved by the system.    

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup


Last edited by bitRAKE on 21 Sep 2008, 15:20; edited 1 time in total
Post 21 Sep 2008, 15:17
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 21 Sep 2008, 15:17
Wouldn't this be more appropriate?
Code:
.messageLoop:
  invoke DispatchMessage, ebx

.getMessage:
  invoke GetMessage, ebx, edi, edi, edi
  cmp    eax, 0
  jz     .wm_quit
  cmp    eax, -1
  jz     .big_problem_display_error_and_quit
  jmp    .messageLoop    
Post 21 Sep 2008, 15:17
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 21 Sep 2008, 15:30
bitRAKE wrote:

True, it would not work for messages in the range [80000000,FFFFFFFE]. Seems safe for some years.

Where the documentation says that GetMessage returns the message id?

revolution, too many branches for my taste, if separate handling is needed for the three conditions I would use this:
Code:
.messageLoop:
  invoke DispatchMessage, ebx 

.getMessage: 
  invoke GetMessage, ebx, edi, edi, edi
  sub    eax, -1
  cmp    eax, 1
  ja     .messageLoop

  test eax, eax
  jz    .big_problem_display_error_and_quit 
.wm_quit:
.
.
.

.big_problem_display_error_and_quit:
.
.
.
    
Post 21 Sep 2008, 15:30
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 21 Sep 2008, 15:38
LocoDelAssembly, too many tests for my taste, if separate handling is needed for the three conditions I would use this:
Code:
.messageLoop:
  invoke DispatchMessage, ebx 

.getMessage: 
  invoke GetMessage, ebx, edi, edi, edi
  sub    eax, -1
  cmp    eax, 1
  ja     .messageLoop
  jb    .big_problem_display_error_and_quit 
.wm_quit:
.
.
.

.big_problem_display_error_and_quit:
.
.
.    
But seriously, sure we are all free to optimise in whatever way we like, no problem there. I was just trying to show that your original submission ignored the error and simply passes it to DispatchMessage, then tries to get another message, and then the next message may also fail for the same reason, thus an infinite loop and program failure!
Post 21 Sep 2008, 15:38
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 21 Sep 2008, 15:39
I just fixed my original code, there was a bug and it was working by miracle*, now this one really halts in the [-1,0] range.

The whole miracle was that the return value was either 1 or 0, not the whole integer range.

[edit]Seems that I need The Svin's tool Razz
Post 21 Sep 2008, 15:39
View user's profile Send private message Reply with quote
asmfan



Joined: 11 Aug 2006
Posts: 392
Location: Russian
asmfan 21 Sep 2008, 15:47
Quote:
When a key is pressed, the system looks for a match against all thread hot keys.
Post 21 Sep 2008, 15:47
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 21 Sep 2008, 15:57
asmfan wrote:
Quote:
When a key is pressed, the system looks for a match against all thread hot keys.
I think you misunderstand that statement. What happens if you register ALT-F4? The key gets sent to the window currently in focus, so any thread associated with the process that owns the window are checked. I don't think it is a global search of all hot keys in every process, else you would get many threads matching the same keystroke and every process will close at the same time!
Post 21 Sep 2008, 15:57
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.