Page 1 of 1

Trap WM_COPYDATA from component.

PostPosted: February 1st, 2012, 2:22 pm
by Gome206
Hi!

I'm trying to write a component, to send string messages between applications by WM_COPYDATA.
I'd like trap the WM_COPYDATA, but isn't work like this:


TMyMessage = class(TComponent)
private
{ Private declarations }

protected
{ Protected declarations }

procedure WMCopyData(var Msg : TMessage); message WM_COPYDATA;

end;

Searching Google a lot, found some reference using wndproc. I tried it, but isn't working too.

TMyMessage = class(TComponent)

protected
{ Protected declarations }

procedure WMCopyData(var Msg : TMessage); message WM_COPYDATA;
procedure WndProc(var Msg: TMessage);

end;

procedure TMyMessage.WndProc(var Msg: TMessage);
var
p: PCopyDataStruct;
begin
//inherited;
if Msg.Msg = WM_COPYDATA then
WMCopyData(Msg);
end;

Please help, what is wrong? ](*,)

Göme206

Re: Trap WM_COPYDATA from component.

PostPosted: February 1st, 2012, 10:30 pm
by lexdean
thier is a project on torry pages on your subject
except windows is not good at handling delphi objects

Lex Dean

Re: Trap WM_COPYDATA from component.

PostPosted: February 4th, 2012, 9:30 am
by Kambiz
Do not override WndProc. Delphi automatically calls WMCopyData, it doesn't need to be called manually inside the WndProc.

Re: Trap WM_COPYDATA from component.

PostPosted: July 18th, 2012, 11:12 pm
by lexdean
I made my self such a Component
its has a server /terminal arrangement.
The component removes most the code from the form's code.

It transports a ID integer and a reference with ether:-
a string and integer for data
or a stream and transport pictures between programs

The ID integer is to remove broadcast copy messages from being received

I have made a execute and wait for the EXE to be installed

It’s on Tory pages.

Re: Trap WM_COPYDATA from component.

PostPosted: July 19th, 2012, 3:05 pm
by arhangelsoft
WM_ messages could be handled only by windows.
Your component need to create a window and organize message cycle for it and fork with it.

See my example:
Code: Select all
uses windows, messages;

var   
  wndClass: TWndClassEx;
  wndMain: HWND;
  wndMC: TMsg; //Message for my windows messages cycle


//Message handler
function WindowProc(wnd:HWND; Msg : Integer; Wparam:Wparam; Lparam:Lparam):Lresult;
 stdcall;
Begin

case msg of
wm_destroy :
  Begin
   PostQuitMessage(0); Exit;
   Result:=0;
  End;

  else Result:=DefWindowProc(wnd,msg,wparam,lparam);
end;

End;

var xPos,yPos,nWidth,nHeight : Integer;

begin
wndClass.cbSize:=sizeof(TWndClassEx);
wndClass.style:=cs_hredraw or cs_vredraw;
wndClass.lpfnWndProc:=@WindowProc;
wndClass.cbClsExtra:=0;
wndClass.cbWndExtra:=0;
wndClass.hInstance:=HInstance;
wndClass.hIcon:=LoadIcon(0,idi_application);
wndClass.hCursor:=LoadCursor(0,idc_arrow);
wndClass.hbrBackground:=COLOR_BTNFACE+1;
wndClass.lpszMenuName:=nil;
wndClass.lpszClassName:='TMP: Window';

RegisterClassEx(wndClass);


xPos:=100;
yPos:=150;
nWidth:=400;
nHeight:=250;


wndMain:=CreateWindowEx(0,'TMP: Window','Window',ws_overlappedwindow,xPos,yPos,nWidth,nHeight,0, 0,Hinstance,nil);
ShowWindow(MwndMain,CmdShow);

//message cycle
While GetMessage(wndMC,0,0,0) do
begin
  TranslateMessage(wndMC);
  DispatchMessage(wndMC);
end;

end.


Only what is need.
Description of used functions you can found on MSDN WinAPI reference

Why you don't use Memory Mapped Files? Window doesn't need it.

Re: Trap WM_COPYDATA from component.

PostPosted: July 29th, 2012, 10:39 pm
by lexdean
go to about.com WM_COPYDATA
and start from scrach

Re: Trap WM_COPYDATA from component.

PostPosted: May 8th, 2013, 10:51 pm
by dejavu.cucud
simply, you can use SendMessage to send string into any destination window (on same application or different application). fill the wParam with any TEdit.Handle or another Text Control's handle.

const
{ User-defined message }
WM_EXCHANGE_TEXT = WM_USER + 2;

SendMessage(hWndDestination, Edit1.Handle, 0);

on the destination window, create a custom message method to catch the message. use GetWindowText() function, and fill HWND/handle parameter with wParam.

private
procedure ExchangeText(var Msg: TMessage); message WM_EXCHANGE_TEXT;

procedure TFormDest.ExchangeText(var Msg: TMessage);
var data: PChar;
begin
GetMem(data, 1024);
GetWindowText(Msg.wParam, data, 1024);
ShowMessage(StrPas(data));
FreeMem(data);
end;