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#?

[411 byte] By [Ravashi] at [2007-12-24]
# 1

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

TaylorMichaelL at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...
# 2

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 :)

Ravashi at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...
# 3
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

TaylorMichaelL at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...
# 4

[DllImport("user32")]

privatestaticexternUInt32 GetWindowThreadProcessId(

Int32 hWnd, outInt32 lpdwProcessId

privateInt32 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);

cablehead at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...
# 5

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
{
publicpartialclassForm1 : Form
{

[DllImport("user32.dll")]
staticexternint GetForegroundWindow();

[DllImport("user32")]
privatestaticexternUInt32 GetWindowThreadProcessId(Int32 hWnd, outInt32 lpdwProcessId);

privateint teller = 0;

public Form1()
{
InitializeComponent();
}

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

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

privatevoid 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;
}
}
}

Ravashi at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...
# 6

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...

cablehead at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...
# 7
Why? this works
Ravashi at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...
# 8
Because your just running a timer...Why re-do the process info if its the same window?
cablehead at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...