How to get the active application

I have the code to get the active window (for example notepad) and the capiton of the program (untitled - Notepad)

But now I want to get the application, cause if I save a file in notepad the capiton will change. And I need something unique, like the program name. I mean I can start notepad a zillion times, it will always be called notepad.

So does anyone know how to do this in C#



Answer this question

How to get the active application

  • soni_ace

    Thanks! that works :D

    My total code for anybody else that needs it:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Diagnostics;
    namespace WindowsApplication1
    {
    public partial class Form1 : Form
    {

    [DllImport("user32.dll")]
    static extern int GetForegroundWindow();

    [DllImport("user32")]
    private static extern UInt32 GetWindowThreadProcessId(Int32 hWnd, out Int32 lpdwProcessId);

    private int teller = 0;

    public Form1()
    {
    InitializeComponent();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
    if (teller == 1)
    {
    setTextje();
    }
    teller++;
    }

    private Int32 GetWindowProcessID(Int32 hwnd)
    {
    Int32 pid = 1;
    GetWindowThreadProcessId(hwnd,
    out pid);
    return pid;
    }

    private void setTextje()
    {
    Int32 hwnd = 0;
    hwnd = GetForegroundWindow();
    string appProcessName = Process.GetProcessById(GetWindowProcessID(hwnd)).ProcessName;
    string appExePath = Process.GetProcessById(GetWindowProcessID(hwnd)).MainModule.FileName;
    string appExeName = appExePath.Substring(appExePath.LastIndexOf(@"\") + 1);
    textBox1.Text = appProcessName +
    " | " + appExePath + " | " + appExeName;
    }
    }
    }


  • djshades2004

    Hi all Smile

    I'm trying to understand the above code... but when i run the program, the textbox still blank Sad

    What i need to know to make it work

  • spattewar

    I get it using the GetForegroundWindow() method. this gives me the handle which I use to get the caption with GetWindowText() method.
    I got that piece of code from a topic here at the forum: http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=474529&SiteID=1 lowest code.
    I tried working with the processes but that won't get me something unique.
    Thanks anyway :)


  • nick5454

    You should keep a local var lastHwnd, or whatever.

    int currentHwnd = GetForeGroundWindow();

    if(currentHwnd != lastHwnd)

    {

    lastHwnd = currentHwnd;

    ///do the process stuff

    }

    Save on the process thrashing...



  • grprk

    How are you retrieving the active window and caption If you are using the Process class then you can use MainModule to get the executable of the process and then FileName to get the full path or ModuleName to get just the name. Be aware however that a module name is not guaranteed unique. Actually even the file name is not guaranteed unique so it really depends on what you want to do with the information. For example if you want to be able to kill an arbitrary process you can't use the module name as there could be several versions of the program running. In that case you'd use the Id property as it is guaranteed unique while the process is running (but could be reused by another process later on).

    Michael Taylor - 9/28/06


  • Danny Tuppeny

    Once you get the process id using the code given in the other post you can use GetProcessById to get the Process object. From there you have access to everything that is exposed by the Win32 API. For uniqueness the Id is the only valid indicator. Once a process closes there is nothing that makes it unique (not even the full path). What are you trying to do with the process once you get it

    Michael Taylor - 9/28/06

  • Hallio

    Why this works
  • Xi0N

    [DllImport("user32")]

    private static extern UInt32 GetWindowThreadProcessId(

    Int32 hWnd, out Int32 lpdwProcessId

    private Int32 GetWindowProcessID(Int32 hwnd)

    {

    Int32 pid = 1;

    GetWindowThreadProcessId(hwnd, out pid);

    return pid;

    }

    string appProcessName = Process.GetProcessById(GetWindowProcessID(hwnd)).ProcessName;

    string appExePath = Process.GetProcessById(GetWindowProcessID(hwnd)).MainModule.FileName;

    string appExeName = appExePath.Substring(appExePath.LastIndexOf(@"\") + 1);



  • ranger28

    Because your just running a timer...Why re-do the process info if its the same window

  • How to get the active application