Finding a Previously Oriented Application - WinAPI

I answered this in a related question, but it is rather an additional question that I am having problems with and I need more recent answers ...

Basically, I have an application that remains open on the screen, and the user can click a button in my application as soon as they have recorded in one of three third-party applications.

When they click a button in my application, I need to determine which of the three applications they use recently to find out which database to talk to. I followed the search path of GetForeGroundWindow and GetWindow, however the Window handle that I get from GetWindow always refers to the window with the title M. I used the Winternal Explorer tool from the Windows API driven tools and I can find the handle M, which is also "child "the process I will be following, but from this descriptor I cannot get the name of the process.

I made a small application for the example, using simple window shapes, and I understand it, and then I will make Notepad the focus, and then click on the button, and I will get a handle, but if you look at the MainWindowHandle of all processes, it is not specified, but using Winternal Explorer I see that this is a subprocess of the notebook process.

Any suggestions on why I get this subprocess descriptor instead of the actual process descriptor

Sample code below - runs on a computer running Windows XP sp 3

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

namespace TestWindowsAPI
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        public static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            IntPtr thisWindow = GetForegroundWindow();
            IntPtr lastWindow = GetWindow(thisWindow, 2);

            tbThisWindow.Text = thisWindow.ToString();
            tbLastWindow.Text = lastWindow.ToString();
        }
    }
}
+3
source share
3 answers

You can use GetWindowThreadProcessIdto get the process ID from the window handle (under):

uint lastProcess;
GetWindowThreadProcessId(lastWindow, out lastProcess);
+2
source

Pent Ploompuu - it was a spot on - great job! Greetings

- - :

private void button1_Click(object sender, EventArgs e)
    {
        IntPtr thisWindow = GetForegroundWindow();
        IntPtr lastWindow = GetWindow(thisWindow, 2);

        uint processID = 0;
        var parentWindow = GetWindowThreadProcessId(lastWindow, out processID);

        tbThisWindow.Text = thisWindow.ToString();
        tbLastWindow.Text = lastWindow.ToString();
        tbParentWindow.Text = parentWindow.ToString();

        tbLastProcess.Text = processID.ToString();
        var processName = from cp in Process.GetProcesses() where cp.Id == processID select cp.ProcessName;

        tbParentName.Text = processName.FirstOrDefault();
    }
+2

Try to override WndProc (or add IMessageFilter) for each of the programs and return the "application identifier" when sending a specific message. Then just use SendMessage in the window handle to get the application id.

0
source

Source: https://habr.com/ru/post/1729056/


All Articles