1

I have the following code:

The using statements are

using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;

Before the Main I have

// import the function in your 
[DllImport("User32.dll")]
static extern int SetForegroundWindow(IntPtr point);

[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);

[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
static extern IntPtr SetActiveWindow(IntPtr hWnd);

[STAThreadAttribute]
static void **Main**(string[] args)

Within the Main I have

// Get the Processes
Process[] p = Process.GetProcesses();
string processName = "";
// Loop through the processes list
for (int i = 0; i < p.Length; i++)
{
    // Find myProcessName
    if (p[i].ProcessName == "myProcessName")
    {
        Console.WriteLine("PID: " + p[i].Id);
        processName = p[i].ProcessName;
    }
}

// Set the process/window to the foreground
Process p = Process.GetProcessesByName(processName)[0];
if (p != null)
{
    // Get the process handle
    IntPtr h = p.MainWindowHandle;
    // Set the window to the foreground
    SetForegroundWindow(h);
    // Wait to get idle
    p.WaitForInputIdle();
    // Set the Active window
    SetActiveWindow(h);
    // Wait to get idle
    p.WaitForInputIdle();
}

// Select the Text
MouseOperations.SetCursorPosition(1269, 218);
MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.LeftDown);
MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.LeftUp);

MouseOperations.SetCursorPosition(123, 310);
MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.LeftDown);
MouseOperations.SetCursorPosition(133, 502);

// Send the Ctrl+C
SendKeys.SendWait("^c");
// Application.DoEvents();
// Print the clipboard
if (Clipboard.ContainsText())
{
    Console.WriteLine("##########################################");
    Console.WriteLine(Clipboard.GetText());
    Console.WriteLine("##########################################");
}

// Done, lift the left mouse key
MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.LeftUp);

The MouseOperations was taken from here (https://stackoverflow.com/a/7121314/1933657)

I run the program from within VisualStudio 2019, the CommandWindow shows up, the program I want comes up above the CommandWindow, the Text is selected, but nothing is ever copied to the Clipboard.

Any ideas what I am missing here?

KingsInnerSoul
  • 1,373
  • 4
  • 20
  • 49
  • 1
    Is the other application yours ? if not asked the company that made it if they protected it against sendkeys. – Franck Oct 22 '20 at 16:33
  • The application is not mine, is running withing the Citrix Wordspace, I have no idea if the keystrokes are protected - but if I use my keyboard for CTRL+C it does copy. – KingsInnerSoul Oct 22 '20 at 16:36
  • 1
    Looks ok, except the first loop in `Main` appears to just be equivalent to `string processName = "myProcessName"`, since the next thing you do is find the process by name again... You can get the `Process.Id` at that time, and eliminate the initial loop. In terms of copy-paste, you're keeping the left mouse button pressed *while copying* - I'm not sure if that works in Windows; I'd swap the order of those two commands. – CoolBots Oct 22 '20 at 16:37
  • @CoolBots, I cleaned up the code, I had some extra stuff within those code areas. Sorry about the bad cleanup. – KingsInnerSoul Oct 22 '20 at 16:38
  • 1
    @CoolBots raising the left mouse click as you suggested, before the copy, did not change the outcome. – KingsInnerSoul Oct 22 '20 at 16:43
  • 1
    @Franck My understanding is SendKeys is indistinguishable from keyboard key presses - do you know of a way to "protect against SendKeys“? Would you mind posting a link to such resource, or a quick explanation? Thanks! – CoolBots Oct 22 '20 at 16:48
  • 1
    Can you check if your code works on a different application, say Notepad? – CoolBots Oct 22 '20 at 16:50
  • @CoolBots I did try notepad just now, and it does not copy. Either with or without lifting the left mouse key before the sendkeys – KingsInnerSoul Oct 22 '20 at 16:56
  • @CoolBots You can simply detect the time between window change focus vs any keypress and cancel them. you will get a focus to your application instant before the send key. As a secondary option you can simply hook to the keyboard itself and detect if the key match the keyboard key that were recently pressed. Very easy when you know how. – Franck Oct 22 '20 at 17:33

0 Answers0