Solved

PostMessage with more then 2 parameters

Posted on 2004-10-05
8
274 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
[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
  • 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
Industry Leaders: 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!

 
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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to debug For loops? 3 63
LAN or WAN ? 11 106
Performance of SQL statement 37 137
how can i delete one control based on its tagstring ? 6 37
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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…

733 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