Solved

GetMessage return values

Posted on 2004-04-10
6
2,352 Views
Last Modified: 2012-06-27
Hi,

I have written a message loop below.  However, the compiler gives me an incompatible types error when trying to interpret the return value.

var
  ExitLoop: Boolean;
  B: BOOL;
  lpMsg: TMsg;

ExitLoop := False;
repeat
  B := GetMessage(lpMsg, 0, 0, 0);
  if B = 0 then ExitLoop := True // Incompatible types
  else if B <> -1 then // Incompatible types
  begin
    TranslateMessage(lpMsg);
    DispatchMessage(lpMsg);
  end;
until ExitLoop = True;

According to the GetMessage documentation that comes with Delphi it says:

"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."

Also:

"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."

Therefore, how do I reliably discriminate between the three different return value possibilities of GetMessage in Delphi?

Thanks in Advance,

Richard
0
Comment
Question by:Richard2000
  • 3
  • 2
6 Comments
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 10798101
the -1 and WM_QUIT sort of mean the same thing because if it crashes its going to close anyways.
any nonzero value is false. So the function will return false if there is an invalid handle and if there is a WM_QUIT message.



SO, the code now would be like

var
  ExitLoop: Boolean;
  B:bool;
  lpMsg: TMsg;
begin

ExitLoop := False;
repeat
  B := GetMessage(lpMsg, 0, 0, 0);
  if B = false then  ExitLoop := True
  else
  begin
    TranslateMessage(lpMsg);
    DispatchMessage(lpMsg);
  end;
until ExitLoop = True;


hope this helps, if not let me know
0
 

Author Comment

by:Richard2000
ID: 10798406
Jase-Coder,

Thank you for your comment.

I'm not sure that this would work though.  I assume that Delphi's BOOL would use FALSE for zero and TRUE for any nonzero value.  Therefore, if -1 for an error is returned, this is a nonzero value, so it will be seen as TRUE.  This would then inadvertently cause TranslateMessage and DispatchMessage to be executed after the error, which should be avoided because according to the documentation, this can lead to fatal application errors.

Any ideas?

Richard
0
 
LVL 33

Accepted Solution

by:
Slick812 earned 50 total points
ID: 10799743
helio Richard2000, you should just use a While loop for GetMessage, I know it  says something about -1 in the documentation, but I thick that may be left over from the 16 bit windows days? . . .

anyway, if you look in that same Win32 API help, they give this as being the way to use GetMessage

while( GetMessage( &msg, NULL, 0, 0 ) )
{
    TranslateMessage( &msg );
    DispatchMessage( &msg );
}

of cource this is C-Code, but they have no "Catch" for a -1 there

you should just use

while GetMessage(Msg,0,0,0) do
  begin
  TranslateMessage(Msg);  
  DispatchMessage(Msg);
  end;

the only windows System calls this GetMessage function, and it  knows better than to call getmessage with incorrect data

if you really think this is important, the  BOOL is a Four Byte variable, you might try

while Integer(GetMessage(Msg,0,0,0)) > 0 do

, but I'm not really sure if the  -1  they talk about is an Integer  -1  or a  UNIT  -1 ? ?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:Richard2000
ID: 10800914
Slick812,

Many thanks for your help.  After reading your comment, I have decided to just use GetMessage in a while loop and ignore the -1 error situation.

Richard
0
 
LVL 33

Expert Comment

by:Slick812
ID: 10803440
you might find some info about this at

http://www.angelfire.com/hi5/delphizeus/first1.html
0
 

Author Comment

by:Richard2000
ID: 10816744
Slick812,

Thanks for the link.  I've read the article and it uses GetMessage in a while loop (no mention of -1 for error).  So it looks as though this is the standard way of using GetMessage.

Richard
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now