1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| class Program
{
#region imporation de dll
[ DllImport( "user32.dll" ) ]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam );
[ DllImport( "user32.dll", SetLastError = true, CharSet = CharSet.Unicode ) ]
private static extern IntPtr FindWindowEx( IntPtr hwndParent,IntPtr hwndChildAfter, string lpszClass, string lpszWindow );
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true)]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
#endregion
private const int EM_GETSEL = 0xB0;
static unsafe void Main(string[] args)
{
ThreadStart delegue = new ThreadStart (thread);
Thread monThread = new Thread (delegue);
Console.Write("Debut du thread dans 5 secondes.\n");
Thread.Sleep(5000);
monThread.Start();
Console.Read();
}
static unsafe void thread()
{
Process saveProcessCurrent = new Process();
IntPtr idFocus;
//on récupère l'id du handler qui à le processus
while (true)
{
idFocus = GetForegroundWindow();
uint pid;
//on récupère l'id du processus qui possede ce handler
GetWindowThreadProcessId(idFocus, out pid);
Console.WriteLine("IdFocus : " + idFocus + " " + "\nPID : " + pid + "\n");
IntPtr hWnd = (IntPtr)(pid);
if (hWnd != IntPtr.Zero)
{
int start;
int end;
SendMessage(hWnd, EM_GETSEL, (IntPtr)(&start), (IntPtr)(&end));
Console.WriteLine("Point : " + start + " ; " + end + "\n");
}
else
Console.WriteLine(" La fonction GetWindowThreadProcessId(idFocus, out pid) a raté.");
Console.Write("\n /************************************/\n\n");
Thread.Sleep(2000);
}
}
} |
Partager