Jump to content
Tommi Prami

Handlin the message and returning value to the caller

Recommended Posts

I am bit lost on how I can handle user message and return value to the caller app, which is using SendMessage API.

I've got following code

UM_STATUS = WM_USER + 333;
...
// On form
    procedure WmPrintPreviewStatus(var Msg: TMessage); message UM_PRINT_PREVIEW_STATUS;
// and implementation

 

procedure TMyForm.WmStatus(var Msg: TMessage);
var
  LResult: NativeInt;
begin
  LResult := 1;

  if Assigned(FStatusObject) then
    if FStatusObject.Status = statFinished then
      LResult := 2;

  Msg.Result := LResult;
end;

 

What I am now more than less confused that message structure in MSDN looks pretty different (below) and Will my code work, so that caller can use something like MyResult = SendMessage(HWND,  UM_STATUS...)

 

typedef struct tagMSG {
  HWND   hwnd;
  UINT   message;
  WPARAM wParam;
  LPARAM lParam;
  DWORD  time;
  POINT  pt;
  DWORD  lPrivate;
} MSG, *PMSG, *NPMSG, *LPMSG;

 

Share this post


Link to post

The sender of the message will receive the return value that you set. 

 

Perhaps a bigger problem is that vcl window recreation means that your form's window handle is liable to change during its lifetime. For ipc using windows messages you need to use stable window handles. 

Share this post


Link to post
3 hours ago, David Heffernan said:

The sender of the message will receive the return value that you set. 

 

Perhaps a bigger problem is that vcl window recreation means that your form's window handle is liable to change during its lifetime. For ipc using windows messages you need to use stable window handles. 

We acquire HWND of window first then we try to poll window couple of seconds, when status is set, we close from .

This is GUI testing AutoIT script which should poll that particular form.

Share this post


Link to post
11 hours ago, Tommi Prami said:

What I am now more than less confused that message structure in MSDN looks pretty different (below) and Will my code work, so that caller can use something like MyResult = SendMessage(HWND,  UM_STATUS...)

Yes, that will work fine.  The VCL's internal WndProc that will first receive the message (StdWndProc() in System.Classes.pas) copies the tagMSG values to a local TMessage, then dispatches that TMessage to handlers, and then returns the TMessage.Result value back to the original sender.

  • Thanks 1

Share this post


Link to post
13 hours ago, Remy Lebeau said:

Yes, that will work fine.  The VCL's internal WndProc that will first receive the message (StdWndProc() in System.Classes.pas) copies the tagMSG values to a local TMessage, then dispatches that TMessage to handlers, and then returns the TMessage.Result value back to the original sender.

Thanks man!

Some Windows messages in Delphi guru should write detailed blog post. There was examples but could not find any that would explain all the plumbing and returning values. 😄

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×