Announcement

Collapse
No announcement yet.

GetWindowHandle + Ctrl V

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • GetWindowHandle + Ctrl V

    Hello Lifepower!

    How are you?

    A little bit of off topic here....

    Code:
    Code:
    procedure TForm1.Button3Click(Sender: TObject);
    var
      H : HWnd;
     begin
    
          H := FindWindow(nil, pchar('Kaspersky Password Manager'));
          if H <> 0 then
          begin
         // Try to bring target window to foreground
         ShowWindow(H,SW_SHOW);
         SetForegroundWindow(H);
    ​
    Clipboard.AsText := 'mypass';
      Keybd_event(VK_CONTROL, 0, 0, 0);
      Keybd_event(Byte('V'), 0, 0, 0);
      Keybd_event(Byte('V'), 0, KEYEVENTF_KEYUP, 0);
      Keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
    
      Keybd_event(VK_RETURN, 0, 0, 0);
      Keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
    ​
    With this code below, I'm able to locate a window, and paste a password on it... using Delphi 7 all is running fine inside the debugger, when I compile .exe and run the app the code dont works...

    I just need to select other application and paste a text on it using a handle...

    Do you have suggestion how to fix it?

    Thanks

  • #2
    What OS are you trying to run this code on? You have to make sure that the target application is running with the same privileges (e.g. administrator) as yours and that title matches exactly. I'm not sure if "SetForegroundWindow" will work on application that is not currently active. You can also use "SendInput" instead of "Keybd_event":

    Code:
    procedure TForm1.Button3Click(Sender: TObject);
    var
      H: HWND;
      Input: TInput;
    begin
      H := FindWindow(nil, PChar('Kaspersky Password Manager'));
      if H = 0 then
      begin
        ShowMessage('Target window not found.');
        Exit;
      end;
    
      ShowWindow(H, SW_SHOW);
      SetForegroundWindow(H);
    
      Clipboard.AsText := 'mypass';
    
      FillChar(Input, SizeOf(TInput), 0);
      Input.Itype := INPUT_KEYBOARD;
      Input.ki.wVk := VK_CONTROL;
      SendInput(1, Input, SizeOf(TInput));
      Input.ki.wVk := Ord('V');
      SendInput(1, Input, SizeOf(TInput));
      Input.ki.dwFlags := KEYEVENTF_KEYUP;
      SendInput(1, Input, SizeOf(TInput));
      Input.ki.wVk := VK_CONTROL;
      Input.ki.dwFlags := 0;
      SendInput(1, Input, SizeOf(TInput));
    
      FillChar(Input, SizeOf(TInput), 0); 
      Input.Itype := INPUT_KEYBOARD;
      Input.ki.wVk := VK_RETURN;
      SendInput(1, Input, SizeOf(TInput));
      Input.ki.dwFlags := KEYEVENTF_KEYUP;
      SendInput(1, Input, SizeOf(TInput));
    end;

    Comment


    • #3
      Originally posted by lifepower View Post
      What OS are you trying to run this code on? You have to make sure that the target application is running with the same privileges (e.g. administrator) as yours and that title matches exactly. I'm not sure if "SetForegroundWindow" will work on application that is not currently active. You can also use "SendInput" instead of "Keybd_event":

      Code:
      procedure TForm1.Button3Click(Sender: TObject);
      var
      H: HWND;
      Input: TInput;
      begin
      H := FindWindow(nil, PChar('Kaspersky Password Manager'));
      if H = 0 then
      begin
      ShowMessage('Target window not found.');
      Exit;
      end;
      
      ShowWindow(H, SW_SHOW);
      SetForegroundWindow(H);
      
      Clipboard.AsText := 'mypass';
      
      FillChar(Input, SizeOf(TInput), 0);
      Input.Itype := INPUT_KEYBOARD;
      Input.ki.wVk := VK_CONTROL;
      SendInput(1, Input, SizeOf(TInput));
      Input.ki.wVk := Ord('V');
      SendInput(1, Input, SizeOf(TInput));
      Input.ki.dwFlags := KEYEVENTF_KEYUP;
      SendInput(1, Input, SizeOf(TInput));
      Input.ki.wVk := VK_CONTROL;
      Input.ki.dwFlags := 0;
      SendInput(1, Input, SizeOf(TInput));
      
      FillChar(Input, SizeOf(TInput), 0);
      Input.Itype := INPUT_KEYBOARD;
      Input.ki.wVk := VK_RETURN;
      SendInput(1, Input, SizeOf(TInput));
      Input.ki.dwFlags := KEYEVENTF_KEYUP;
      SendInput(1, Input, SizeOf(TInput));
      end;
      Windows 11 and Delphi 7

      Comment


      • #4
        Originally posted by lifepower View Post
        What OS are you trying to run this code on? You have to make sure that the target application is running with the same privileges (e.g. administrator) as yours and that title matches exactly. I'm not sure if "SetForegroundWindow" will work on application that is not currently active. You can also use "SendInput" instead of "Keybd_event":

        Code:
        procedure TForm1.Button3Click(Sender: TObject);
        var
        H: HWND;
        Input: TInput;
        begin
        H := FindWindow(nil, PChar('Kaspersky Password Manager'));
        if H = 0 then
        begin
        ShowMessage('Target window not found.');
        Exit;
        end;
        
        ShowWindow(H, SW_SHOW);
        SetForegroundWindow(H);
        
        Clipboard.AsText := 'mypass';
        
        FillChar(Input, SizeOf(TInput), 0);
        Input.Itype := INPUT_KEYBOARD;
        Input.ki.wVk := VK_CONTROL;
        SendInput(1, Input, SizeOf(TInput));
        Input.ki.wVk := Ord('V');
        SendInput(1, Input, SizeOf(TInput));
        Input.ki.dwFlags := KEYEVENTF_KEYUP;
        SendInput(1, Input, SizeOf(TInput));
        Input.ki.wVk := VK_CONTROL;
        Input.ki.dwFlags := 0;
        SendInput(1, Input, SizeOf(TInput));
        
        FillChar(Input, SizeOf(TInput), 0);
        Input.Itype := INPUT_KEYBOARD;
        Input.ki.wVk := VK_RETURN;
        SendInput(1, Input, SizeOf(TInput));
        Input.ki.dwFlags := KEYEVENTF_KEYUP;
        SendInput(1, Input, SizeOf(TInput));
        end;
        Changing the permission of .exe file to admin its working Lifepower, thank you!

        Comment

        Working...
        X