Connecting UWP apps hosted by ApplicationFrameHost to their real processes

前端 未结 4 1204
暗喜
暗喜 2021-02-08 00:30

I am working on an WPF application to monitor my activities on my computer. I use Process.GetProcesses() and some filtering to get the processes I am interested in

4条回答
  •  不知归路
    2021-02-08 00:58

    I eventually found a way to do this, so I am going answer my own question so maybe someone in the future with the same problem could find it useful.

    This is the class with the WinApiFunctions:

    public class WinAPIFunctions
    {
        //Used to get Handle for Foreground Window
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr GetForegroundWindow();
    
        //Used to get ID of any Window
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
        public delegate bool WindowEnumProc(IntPtr hwnd, IntPtr lparam);
    
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool EnumChildWindows(IntPtr hwnd, WindowEnumProc callback, IntPtr lParam);
    
        public static int GetWindowProcessId(IntPtr hwnd)
        {
            int pid;
            GetWindowThreadProcessId(hwnd, out pid);
            return pid;
        }
    
        public static IntPtr GetforegroundWindow()
        {
            return GetForegroundWindow();
        }
    }
    

    And this is the class I used to test if it would work. I used it in a simple console program that just writes out the name of the process that has current focus:

    class FindHostedProcess
    {
        public Timer MyTimer { get; set; }
        private Process _realProcess;
        public FindHostedProcess()
        {
            MyTimer = new Timer(TimerCallback, null, 0, 1000);
            Console.ReadKey();
        }
    
        private void TimerCallback(object state)
        {
            var foregroundProcess = Process.GetProcessById(WinAPIFunctions.GetWindowProcessId(WinAPIFunctions.GetforegroundWindow()));
            if (foregroundProcess.ProcessName == "ApplicationFrameHost")
            {
                foregroundProcess = GetRealProcess(foregroundProcess);
            }
            Console.WriteLine(foregroundProcess.ProcessName);
        }
    
        private Process GetRealProcess(Process foregroundProcess)
        {
            WinAPIFunctions.EnumChildWindows(foregroundProcess.MainWindowHandle, ChildWindowCallback, IntPtr.Zero);
            return _realProcess;
        }
    
        private bool ChildWindowCallback(IntPtr hwnd, IntPtr lparam)
        {
            var process = Process.GetProcessById(WinAPIFunctions.GetWindowProcessId(hwnd));
            if (process.ProcessName != "ApplicationFrameHost")
            {
                _realProcess = process;
            }
            return true;
        }
    }
    

提交回复
热议问题