Solved

PostMessage with more then 2 parameters

Posted on 2004-10-05
8
276 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
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!

 
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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

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…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

696 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