Processing WM_CopyData message fails lazarus / 31-LCL ---------------------------------------------------------------------- Krzychu · 2015-08-31 07:03 Hi! I try to create a plugin in Lazarus which is based on existing open-source code. I use Windows and Messages classes.  WM_COPYDATA message should be used to communicate with the main programme and TCopyDataStruct should collect the data. However, I found out that the messages between them are lost. After research I think it might be connected with Processing non-user messages in your window (http://wiki.freepascal.org/Win32/64_Interface#Processing_non- user_messages_in_your_window) Now I try to implement the WndCallBack function, but I stuck with an error. I don't know what parameter should I pass to the procedure WMCopyData inside of the function WndCallback. I have already tried with: - (var Msg: TCopyDataStruct) as in the procedure declaration. It raises an error: <> - (Msg) - Error <> -() - Error <> Could you tell me how to make it work? The code of my procedure: Code: [Select] procedure TForm1.WMCopyData(var Msg: TCopyDataStruct); var   sText: array[0..199] of Char;   s, nr, kod, x, y, z : string;   xx, yy : Double;   err, n : Integer; begin     if Msg.dwData = 1 then          Memo1.Clear;          pkt := Msg.lpData;          for n := 0 to 5000 do           begin            if pkt^[n].nr = '' then Break;            Memo1.Lines.Add(pkt^[n].nr+' '+pkt^[n].k+' '+tostr(pkt^[n].x,2)+' '+tostr(pkt^[n].y,2)+' '+tostr(pkt^[n].z,3));           end;        end  end;  The WndCallBack function Code: [Select]function WndCallback(Ahwnd: HWND; uMsg: UINT; wParam: WParam; lParam: LParam):LRESULT; stdcall;  begin    case uMsg of WM_COPYDATA:      begin        Result := TForm1.WMCopyData();        exit;        end;       else       Result := CallWindowProc(PrevWndProc, Ahwnd, uMsg, WParam, LParam);      end;  end;                 Declared with: Code: [Select]procedure WMCopyData(var Msg:TCopyDataStruct); message WM_COPYDATA; ---------------------------------------------------------------------- derek.john.evans · 2015-08-31 07:54 This is the method i'm using. This unit is hacked from one of my projects. Would be nice to have something like this which is portable. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Wim; {$MODE objfpc}{$H+} interface uses  SysUtils, Windows; type  TWimCallBack = procedure(const AMessage: String); procedure WimSetCallBack(const ASender: HWND; const ACallBack: TWimCallBack);function WimSendMessage(const ASender: HWND; const AReceiver, AMessage: String): Boolean; implementation function WimSendMessage(const ASender: HWND; const AReceiver, AMessage: String): Boolean;var  LHandle: HWND;  LCopyDataStruct: TCopyDataStruct;begin  LHandle := FindWindow(nil, pchar(AReceiver));  Result := LHandle <> 0;  if Result then begin    LCopyDataStruct.dwData := 0;    LCopyDataStruct.cbData := Length(AMessage) + 1;    LCopyDataStruct.lpData := pchar(AMessage);  SendMessage(LHandle, WM_COPYDATA, WPARAM(ASender), LPARAM(@LCopyDataStruct));  end;end; var   GWndProc: WNDPROC;  GWimCallBack: TWimCallBack; function WimProc(AHwnd: HWND; AMessage: UINT; AWParam: WParam; ALParam: LParam): LRESULT; stdcall;var  LCopyDataStruct: TCopyDataStruct;begin  if Assigned(GWimCallBack) and (AMessage = WM_COPYDATA) then begin    LCopyDataStruct := PCopyDataStruct(ALParam)^;    if LCopyDataStruct.dwData = 0 then begin      GWimCallBack(pchar(LCopyDataStruct.lpData));    end;    Result := S_OK;  end else begin    Result := CallWindowProc(GWndProc, AHwnd, AMessage, AWParam, ALParam);  end;end; procedure WimSetCallBack(const ASender: HWND; const ACallBack: TWimCallBack);begin  GWimCallBack := ACallBack;  if GWndProc = nil then begin    GWndProc := Windows.WNDPROC(SetWindowLongPtr(ASender, GWL_WNDPROC, PtrInt(@WimProc)));  end;end; end.  To use, include unit wim, and: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // This is your message receiver.procedure GMyMessageCallBack(const AMessage: String);begin  ShowMessage(AMessage);end; procedure TForm1.FormCreate(Sender: TObject);begin  // Set the receiver callback.  WimSetCallBack(Handle, @GMyMessageCallBack);  // Set the caption. (I will explain why its here below)  Caption := 'MyCaption';end;  // Send a message like this.procedure TForm1.Button1Click(Sender: TObject);begin  WimSendMessage(Handle, 'MyCaption', 'Hello There');end;   The way I use this is for single instances applications. I use WimSendMessage to send the ParamStr(1) (or, EmptyStr if there are no paramstr's) to a known window caption. If that succeeds, I halt the application, since another instance will pick up the filename and load it. If it fails, then I must be the first instance, in which cause I handle the file loading. The reason why Caption must be set after the WinSendMessage, is so I dont pick up my own messages. Anyway, if you have problems with the code, sing out. I guess I like this feature in a seperate unit, because, it makes porting the code easy. On other platforms, I just ignor WinSendMessage/WimSetCallBack ---------------------------------------------------------------------- Krzychu · 2015-08-31 09:07 I have reproduced your code as you provided and I worked. Now I will try to use in the original project. I'll give you feedback soon Thanks a lot for your help! ---------------------------------------------------------------------- Krzychu · 2015-09-01 07:56 I managed to rewrite those codes and make it work in my project. So the problem was solved. Your hint was very useful. Thanks!