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();
}
}
}
source
share