Solved

GetMessage return values

Posted on 2004-04-10
6
2,375 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

763 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