1

I want to send a barcode to TEdit called edtCodigo inside a window called TfrmGuiaMovimiento, I am using the following code:

var
  h: HWND;
  codigo: string;
  i: Integer;
begin
  h := FindWindow('TfrmGuiaMovimiento',nil);
  if h > 0 then
  begin
    codigo := '7665009887781';
    h := FindWindowEx(h, 0, 'edtCodigo', nil);
    if h > 0 then
    begin
      for i := 1 to codigo.Length do SendMessage(h, WM_CHAR, Ord(codigo[i]), 0);
      PostMessage(h, WM_KEYDOWN, VK_RETURN, 0);
    end;
  end;
 end;

But I have not got it. But sending the Handle directly works.

  • 5
    You can't send messages from a service to a desktop application. They are both running in a different session *(and Window Station mentioned [here](https://stackoverflow.com/questions/12976140/how-to-pass-a-message-from-windows-service-to-windows-desktop-application-using/12979450#12979450))*. The crux of the linked answer is to use another mechanism to pass information: named pipes, sockets, ... – Lieven Keersmaekers Aug 14 '17 at 05:41
  • 1
    Or, have the service spawn a new process in the same desktop session as the `TEdit` window using `CreateProcessAsUser()`, and then that process can set the `TEdit` text using `WM_SETTEXT` – Remy Lebeau Aug 14 '17 at 06:36
  • 6
    Will this ever end? [You can’t simulate keyboard input with PostMessage](https://blogs.msdn.microsoft.com/oldnewthing/20050530-11/?p=35513). – IInspectable Aug 14 '17 at 12:18
  • 1
    @IInspectable: "Will this ever end?" - not likely, because there are too many bad examples floating around that do it, and newbies find them and think they are ok to use, not knowing any better. – Remy Lebeau Aug 14 '17 at 21:30

1 Answers1

-2
procedure TMainForm.SendMsgToTele;
var
    h : HWND;
    hchild : HWND;
    aTemp      : array[0..200] of Char;
    sClassName : String;
    //tmpI : Integer;
Begin
    h := FindWindow(nil, 'MT5SENDER');
    if IsWindow(h) then
    Begin
        hchild := FindWindowEx(h, 0, 'TEdit', nil);
        if hchild > 0  then
        Begin
            DateTimeToString(sClassName, 'd.m.yyyy hh:nn:ss.zzz', Now);
            sClassName := sClassName ;
            StrPCopy(aTemp,sClassName);
            SendMessage(hchild,WM_SETTEXT,SizeOf(aTemp),Integer(@aTemp));
            SendMessage(h, WM_USER+1, 123, 520);
        End;
    End;
End;