Solved

GetMessage return values

Posted on 2004-04-10
6
2,385 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 34

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 34

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

734 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