Solved

PostMessage with more then 2 parameters

Posted on 2004-10-05
8
271 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
ID: 12233433
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
ID: 12235036
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
ID: 12236186
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 12

Expert Comment

by:Ivanov_G
ID: 12236417
// 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
 
LVL 12

Expert Comment

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

Expert Comment

by:vadim_ti
ID: 12237891
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
ID: 12343284
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
ID: 12343305
yes, you need to free object when you do not need it more.
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

803 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