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
|
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace Test
{
public static class NativeMethods
{
[DllImport("user32.dll", SetLastError = true)]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
}
class Program
{
static void Main(string[] args)
{
//ouverture de notepad
string lpszParentClass = "Notepad";
string lpszParentWindow = "Untitled - Notepad";
IntPtr ParenthWnd = new IntPtr(0);
//trouve la fenêtre de notepad
ParenthWnd = NativeMethods.FindWindow(lpszParentClass, lpszParentWindow);
if (ParenthWnd.Equals(IntPtr.Zero))
Console.WriteLine("Notepad non trouvé");
else
{
// affiche le chemin complet
Console.WriteLine(GetProcessPathFromWinHandle(ParenthWnd));
}
}
public static string GetProcessPathFromWinHandle(IntPtr hWnd)
{
uint pid;
NativeMethods.GetWindowThreadProcessId(hWnd, out pid);
Process proc = Process.GetProcessById((int)pid);
return proc.MainModule.FileName;
}
}
} |
Partager