Pass data via messages



The following web page has good examples of how to pass data via
custom window messages:

http://www.cryer.co.uk/brian/delphi/howto_send_custom_window_message.htm

Here is a sample of the code:

SendMessage(LinkCheckForm.Handle,WM_ANO_MESSAGE,Integer(self),0);

and at the recipient end:

procedure TMyForm.OnAnoMessage(var Msg: TMessage);
var
myObject: TMyClass;
begin
myObject := TMyClass(msg.WParam);

The problem is that I have only been able to get this to work for
simple objects. Here is an example:

type
TLogMsg = record
MessageText: String;
end;

// Sending
LogMsg.MessageText := GetFileName(S);
SendMessage(Application.MainForm.Handle,WM_Log,Integer(LogMsg),0);

// Receiving
procedure TfmMain.OnLog(var Msg: TMessage);
begin
LogMsg := TLogMsg(Msg.WParam);
LogMemo.Lines.Add(LogMsg.MessageText);
end;

This works fine. However if I try to use a more complex object I get
an "Invalid typecast" error in the SendMessage when I try to compile.

TProgressMsg = record
Module: String;
Count: Integer;
end;

// Sending
ProgressMsg.Module := 'Hi There';
ProgressMsg.Cnt := 1;
SendMessage(Application.MainForm.Handle,WM_Progress,Integer(ProgressMsg),0);

Is it possible to pass this more complicated object using WParam and
LParam?

Thanks, Sidney
.



Relevant Pages

  • Re: Pass data via messages
    ... procedure TMyForm.OnAnoMessage(var Msg: TMessage); ... myObject: TMyClass; ... method to the receiving form and then call that method with whatever ...
    (comp.lang.pascal.delphi.misc)
  • Re: Pass data via messages
    ... procedure TMyForm.OnAnoMessage(var Msg: TMessage); ... myObject: TMyClass; ... WParam and LParam are four bytes each today. ...
    (comp.lang.pascal.delphi.misc)
  • Re: String --> Pointer --> LongInt
    ... > procedure TForm1.OnMsg1(var Msg: TMessage); ... If S is an empty string, ... but from your statement that it "worx fine" not what happens. ...
    (alt.comp.lang.borland-delphi)
  • Re: Refresh eines Objektes, aber wie?
    ... procedure WndProc(var MsgRec: TMessage); ... procedure Button1Click(Sender: TObject); ... Msg: TMsg; ...
    (de.comp.lang.delphi.misc)