Getting titles of all openning windows process

G

Guest

Dear all,

How can I get all the titles of all openning windows? I found a API in
win32 that called EnumWindows() but it returns only the windows handles, not
titles. How can I get the list just like the Applications tab in Windows Task
Manager?

Thanks,

Tedmond
 
G

Guest

I wrote the following sample to get you started:

delegate int EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr
lParam);
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, [Out] StringBuilder
lpString, int nMaxCount);
[DllImport("user32.dll")]
static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool IsWindowVisible(IntPtr hWnd);

private static int PrintText(IntPtr hWnd, IntPtr lParam)
{
if (IsWindowVisible(hWnd))
{
int length = GetWindowTextLength(hWnd);
StringBuilder sb = new StringBuilder(length + 1);
int res = GetWindowText(hWnd, sb, sb.Capacity);
if (res > 0)
Console.WriteLine(sb.ToString());
}
return 1;
}

public static void Main(string[] args)
{
EnumWindowsProc cb = new EnumWindowsProc(PrintText);
EnumWindows(cb, IntPtr.Zero);
}

HTH, Jakob.
 
G

Guest

Thanks Jakob, it works. I have one more question. After I found the
application by title, I want to kill it. However, I used
TerminateProcess(hWnd) but could not kill the window. Do you have any idea?

Thanks for any help.

Tedmond

Jakob Christensen said:
I wrote the following sample to get you started:

delegate int EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr
lParam);
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, [Out] StringBuilder
lpString, int nMaxCount);
[DllImport("user32.dll")]
static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool IsWindowVisible(IntPtr hWnd);

private static int PrintText(IntPtr hWnd, IntPtr lParam)
{
if (IsWindowVisible(hWnd))
{
int length = GetWindowTextLength(hWnd);
StringBuilder sb = new StringBuilder(length + 1);
int res = GetWindowText(hWnd, sb, sb.Capacity);
if (res > 0)
Console.WriteLine(sb.ToString());
}
return 1;
}

public static void Main(string[] args)
{
EnumWindowsProc cb = new EnumWindowsProc(PrintText);
EnumWindows(cb, IntPtr.Zero);
}

HTH, Jakob.

--
http://www.dotninjas.dk
http://www.powerbytes.dk


Tedmond said:
Dear all,

How can I get all the titles of all openning windows? I found a API in
win32 that called EnumWindows() but it returns only the windows handles, not
titles. How can I get the list just like the Applications tab in Windows Task
Manager?

Thanks,

Tedmond
 
G

Guest

Use the Win32 API function GetWindowThreadProcessId to get the id of the
process that started your window. You can then use the managed class
System.Diagnostics.Process to kill the process using either Process.Kill or
Process.CloseMainWindow:

[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int
lpdwProcessId);

// ...
int processId;
GetWindowThreadProcessId(hWnd, out processId);
Process proc = Process.GetProcessById(processId);
// Request a close (use Kill() to force a close)
proc.CloseMainWindow();

HTH, Jakob.

--
http://www.dotninjas.dk
http://www.powerbytes.dk


Tedmond said:
Thanks Jakob, it works. I have one more question. After I found the
application by title, I want to kill it. However, I used
TerminateProcess(hWnd) but could not kill the window. Do you have any idea?

Thanks for any help.

Tedmond

Jakob Christensen said:
I wrote the following sample to get you started:

delegate int EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr
lParam);
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, [Out] StringBuilder
lpString, int nMaxCount);
[DllImport("user32.dll")]
static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool IsWindowVisible(IntPtr hWnd);

private static int PrintText(IntPtr hWnd, IntPtr lParam)
{
if (IsWindowVisible(hWnd))
{
int length = GetWindowTextLength(hWnd);
StringBuilder sb = new StringBuilder(length + 1);
int res = GetWindowText(hWnd, sb, sb.Capacity);
if (res > 0)
Console.WriteLine(sb.ToString());
}
return 1;
}

public static void Main(string[] args)
{
EnumWindowsProc cb = new EnumWindowsProc(PrintText);
EnumWindows(cb, IntPtr.Zero);
}

HTH, Jakob.

--
http://www.dotninjas.dk
http://www.powerbytes.dk


Tedmond said:
Dear all,

How can I get all the titles of all openning windows? I found a API in
win32 that called EnumWindows() but it returns only the windows handles, not
titles. How can I get the list just like the Applications tab in Windows Task
Manager?

Thanks,

Tedmond
 
E

Emmet Gray

Dear all,

How can I get all the titles of all openning windows? I found a API in
win32 that called EnumWindows() but it returns only the windows handles, not
titles. How can I get the list just like the Applications tab in Windows Task
Manager?

Thanks,

Tedmond

....or a much simpler approach would be to use the Process class built
into the .Net Framework.

You can enumerate all running processes, you can look at the Title
Text, and you can kill the process.

... all without using any pinvoke to APIs!

http://msdn.microsoft.com/library/d...l/frlrfsystemdiagnosticsprocessclasstopic.asp
 
G

Guest

That would give you all running processes - not just the ones with a visible
Window.

Regards, Jakob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top