Solved

PostMessage with more then 2 parameters

Posted on 2004-10-05
8
260 Views
Last Modified: 2010-04-05
I need to use the PostMessage command, but I also have 3 parameter to pass...
What would be the best solution in such case...

Thanks for your help!
0
Comment
Question by:PaulP
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 13

Expert Comment

by:BlackTigerX
Comment Utility
first describe how you are planning on using this message, from application to application?

you could just send the pointer to an structure where you have the 3 or whatever fields, that then would become "parameters"
0
 
LVL 12

Expert Comment

by:Ivanov_G
Comment Utility
type
   Params = record
     Param1 : Integer;
     Param2 : String;
     Param3 : Real;
   end;

  PostMessage(Handle, WM_CUSTOMMESSAGE, @Params, 0);

  inside you WM_CUSTOMMESSAGE definition, you declare one Params variables and take the address of it from Msg.wParam
0
 

Author Comment

by:PaulP
Comment Utility
Thanks Ivanov!
You seem to be on the right track to help me...
However, I cannot make it work...

I have two window, one with several buttons that call procedures to the other window..

So I have this...
type
 TActionParams = record
     ParamW : Integer;
     ParamL : Integer;
     Param1 : Integer;
     Param2 : Integer;
     Param3 : Integer;
     Param4 : Integer;
   end;
......

procedure callAction(A,B,C);
Var ActionParams: TActionParams;
Begin
   ActionParams.Param1:=A;
   ActionParams.Param2:=B;
   ActionParams.Param3:=C;
   PostMessage(Self.Handle,UM_CallActionFromButton,@ActionParams,0);
End;

.....

procedure TLJManagerMainForm.AppMessage(var Msg: TMsg; var Handled: Boolean);
Var ActionParams: TActionParams;
Begin

     Case msg.message of
       UM_CallActionFromButton: begin
        ActionParams:=msg.wParam;
        CallActionItems(ActionParams.ParamW,ActionParams.ParamL,ActionParams.Param1,0,0,0,1);
      End;
End;

Buit this doesn't work as there's something I don;t get...  
this line
 PostMessage(Self.Handle,UM_CallActionFromButton,@ActionParams,0);
Give me an error as compile time... "Incompatible Types: Integer and Pointer
0
 
LVL 12

Expert Comment

by:Ivanov_G
Comment Utility
// Here it is ... nice example

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

const
  WM_CALLACTIONFROMBUTTON = WM_USER + 1;

type

 TActionParams = record
   ParamW : Integer;
   ParamL : Integer;
   Param1 : Integer;
   Param2 : Integer;
   Param3 : Integer;
   Param4 : Integer;
 end;


  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    procedure OnMyNewMessage(var Msg: TMessage); message WM_CALLACTIONFROMBUTTON;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TForm1 }

procedure TForm1.OnMyNewMessage(var Msg: TMessage);
var
  params   : ^TActionParams;
  addr     : Pointer;
begin
  // get the address of Params structure
  addr := Ptr(Msg.wParam);
  params := addr;
  if params <> nil then
    // params fetched
    ShowMessage(IntToStr(params.Param3))
  else
    ShowMessage('Error');
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Params    : TActionParams;
begin
  // initialize the parameters
  with Params do
    begin
      ParamW := 0;
      ParamL := 1;
      Param1 := 2;
      Param2 := 3;
      Param3 := 4;
      Param4 := 5;
    end;
  // send to application itself
  SendMessage(Handle, WM_CALLACTIONFROMBUTTON, Integer(@Params), 0);
end;

end.
0
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.

 
LVL 12

Expert Comment

by:Ivanov_G
Comment Utility
I used SendMessage, but now I tried it with PostMessage - the same result - it is working :))
0
 
LVL 6

Expert Comment

by:vadim_ti
Comment Utility
a little another way:

TActionParams = class(TObject)
   ParamW : Integer;
   ParamL : Integer;
   Param1 : Integer;
   Param2 : Integer;
   Param3 : Integer;
   Param4 : Integer;
 end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Params    : TActionParams;
begin
  // initialize the parameters
 Params := TActionParams.Create;
  with Params do
    begin
      ParamW := 0;
      ParamL := 1;
      Param1 := 2;
      Param2 := 3;
      Param3 := 4;
      Param4 := 5;
    end;
  // send to application itself
  PostMessage(Handle, WM_CALLACTIONFROMBUTTON, Integer(Params), 0);
end;

procedure TForm1.OnMyNewMessage(var Msg: TMessage);
begin
  // get the address of Params structure
  with TActionParams(Msg.wParam) do begin
    ShowMessage(IntToStr(Param3))
  end;
end;
0
 

Author Comment

by:PaulP
Comment Utility
Ok, Sorry for the delay...

I used Vadim method (simpler) and it works... Thanks!

But this seems to have a problem with memeory leaks...
Maybe it's just me doing the wrong thing..

So anyway, I made a small modification to the message handling procedure

Here's what I'm doing:

//The calling procedure
procedure TFormTouchPanel.callActionDown(A: word);
var
  ItemB: pButtonSetup;
  ItemA: pActionCommand;
  I:Integer;
  ActionParams: TActionParams;
begin
    ItemB := TouchButtonList[A];
    ActionParams := TActionParams.Create;
    With ItemB^.DownAction do begin
      With ActionParams do begin
        CmdNo:=action[0];
        CmdType:=action[1];
        Param1:=action[2];
        Param2:=action[3];
        Param3:=action[4];
        Origin:=OriginTouchButton;
      End;
    End;
    PostMessage(MainForm.handle,UM_CallAction,Integer(ActionParams),0);
End;
-------------------------------------

procedure TMainForm.CallActionMessage(var Msg: TMessage);
begin
      With TActionParams(Msg.WParam) do begin
        CallActionItems(CmdNo,CmdType,Param1,Param2,Param3,Origin);
           FreeAndNil(TActionParams(msg.WParam));
      End;
End;

I free/nil the ActionParams after... is this the right way to do it?

Thanks!
0
 
LVL 6

Accepted Solution

by:
vadim_ti earned 250 total points
Comment Utility
yes, you need to free object when you do not need it more.
0

Featured Post

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

Join & Write a Comment

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

17 Experts available now in Live!

Get 1:1 Help Now