Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

GetMessage return values

Posted on 2004-04-10
6
Medium Priority
?
2,472 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 34

Accepted Solution

by:
Slick812 earned 200 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Screencast - Getting to Know the Pipeline
Suggested Courses

886 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