Solved

GetMessage return values

Posted on 2004-04-10
6
2,319 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Richard2000
Comment Utility
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
Comment Utility
you might find some info about this at

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

Author Comment

by:Richard2000
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 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

11 Experts available now in Live!

Get 1:1 Help Now