473,396 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Getting titles of all openning windows process

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
Sep 30 '05 #1
5 4631
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" wrote:
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

Sep 30 '05 #2
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" wrote:
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" wrote:
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

Sep 30 '05 #3
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" wrote:
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" wrote:
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" wrote:
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

Sep 30 '05 #4
On Thu, 29 Sep 2005 20:26:04 -0700, "Tedmond"
<Te*****@discussions.microsoft.com> wrote:
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/de...classtopic.asp
Oct 2 '05 #5
That would give you all running processes - not just the ones with a visible
Window.

Regards, Jakob.
--
http://www.dotninjas.dk
http://www.powerbytes.dk
"Emmet Gray" wrote:
On Thu, 29 Sep 2005 20:26:04 -0700, "Tedmond"
<Te*****@discussions.microsoft.com> wrote:
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/de...classtopic.asp

Oct 3 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ohad Young | last post by:
Hi, I need to open (launch) an external application from my winform application. The application is not a dot.net application, for example the windows calculator. I'm using the...
8
by: Rod | last post by:
I have been working with ASP.NET 1.1 for quite a while now. For some reason, opening some ASP.NET applications we wrote is producing the following error message: "The Web server reported...
2
by: Henrik | last post by:
Hi all, I'm working with a win app in vb that needs access to actual process id of the office-applications Word, Excel and PowerPoint. I know I can loop the processes and check for processes of...
5
by: Tedmond | last post by:
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...
0
by: Sally Whitfield | last post by:
8Zcc ----uMyItidGBFcIxNvn7Ubo Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html><head><style type=3Dtext/css>.eyebrow { FONT-WEIGHT: bold; FONT-SIZE= : 10px;...
2
by: MSK | last post by:
Hi, Continued to my earlier post regaring "Breakpoints are not getting hit" , I have comeup with more input this time.. Kindly give me some idea. I am a newbie to .NET, recently I installed...
1
by: Pankajmani das | last post by:
I have developed a program which is need to run automatically when windows/computer start. I have kept my exe file in startup and task folder manually and it work also. Please let me know the code...
0
by: Tobias Maier | last post by:
hello I can't retrieve the windows captions / -titles from processes on Windows Server 2003. On Windows XP all works fine. I use the following: - win32 api FindWindow (by title)
20
by: TomLasky | last post by:
Hi everyone, I ran into a road block and cant seem to find anything on google. I need to check all open windows/processes to see if certain software is running on the system. I was going to go the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.