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 route of using core api from user32 for findwindow and so on, but then I came across using Process.Getprocesses and was very happy to see it was easy to get all processes and MainWindowTitle - Dim poc() As Process = Process.GetProcesses()
-
For i As Integer = 0 To poc.Length - 1
-
-
Try
-
msgbox poc(i).mainwindowtitle
-
-
Catch ex As Exception
-
MsgBox(poc(i).ProcessName.ToString & " " & ex.Message)
-
-
End Try
-
-
Next
-
-
However... if the software has child windows or multiple windows open it does not get the title of these "sub windows". An example is an application that has 2 windows open from the same exe. Is there anyway to grab all window titles of a process using this same logic?
20 35401
I'm not sure quite how to go about this using .NET's process class. You can however do this using P/Invoke. Code similar to the following should work: - private delegate bool EnumWindowsProc(IntPtr hWnd, int lParam);
-
-
[DllImport("user32.dll", SetLastError=true)]
-
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
-
-
[DllImport("USER32.DLL")]
-
private static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam);
-
-
[DllImport("USER32.DLL")]
-
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
-
-
[DllImport("USER32.DLL")]
-
private static extern int GetWindowTextLength(IntPtr hWnd);
-
-
[DllImport("USER32.DLL")]
-
private static extern bool IsWindowVisible(IntPtr hWnd);
-
-
[DllImport("USER32.DLL")]
-
private static extern IntPtr GetShellWindow();
-
-
public IDictionary<IntPtr, string> GetOpenWindowsFromPID(int processID)
-
{
-
IntPtr hShellWindow = GetShellWindow();
-
Dictionary<IntPtr, string> dictWindows = new Dictionary<IntPtr, string>();
-
-
EnumWindows(delegate(IntPtr hWnd, int lParam)
-
{
-
if (hWnd == hShellWindow) return true;
-
if (!IsWindowVisible(hWnd)) return true;
-
-
int length = GetWindowTextLength(hWnd);
-
if (length == 0) return true;
-
-
uint windowPid;
-
GetWindowThreadProcessId(hWnd, out windowPid);
-
if (windowPid != processID) return true;
-
-
StringBuilder stringBuilder = new StringBuilder(length);
-
GetWindowText(hWnd, stringBuilder, length + 1);
-
dictWindows.Add(hWnd, stringBuilder.ToString());
-
return true;
-
}, 0);
-
-
return dictWindows;
-
}
You can then use the Process class to get the Process ID of the application you are targeting and call the above code to get a list of windows associated it, something along the lines of: - Process[] processes = Process.GetProcessesByName("MyApp");
-
foreach (Process process in processes)
-
{
-
IDictionary<IntPtr, string> windows = this.GetOpenWindowsFromPID(process.Id);
-
foreach (KeyValuePair<IntPtr, string> kvp in windows)
-
{
-
// Do whatever you want here
-
}
-
}
If you want to target hidden windows also you can modify the code slightly.
Hope it helps!
If your goal is simply to determine if a particular program is running or not, then why do you need to find all windows? I'd expect that finding at least one window is sufficient.
I will add also that not finding a window is not sufficient to determine that the program is not running.
Do you have this in VB.NET form? I've been trying to do something similar using GetdesktopWindow Hwnd and then using HWNDNEXT to loop through them all, the problem is I cant get the desktopwindow hwnd for some reason.
I visited the pinvoke web site and found a lot of the findwindow api's and such, maybe im going about it the wrong way?
Also, in response to the last post, yes I do need to know the programs thats are running but I also need to know if those programs are running other windows inside of their app. Problem is .NET's getprocesses just gets the mainwindowtitle and doesnt also provide childwindows... in my personal opinion it's a huge let down on the part of .NET. Everyone is always informing me to try and not use Win32API's directly because it's unmanaged and sloppy but in this instance im forced to use it. Anyway, any help would be appreciated, I'm on my 9th hour of google searching for a simple thing I could do in VB6 in 5 minutes.
As pointed out, not finding the window doesn't mean the pogram is not running.
Shouldn't you be looking for a particular .exe or some other more discriminate property to see if it is running?
Here's what I'm doing:
I'm managing instant message windows or trying to anyway. I need to get all open windows for a specific program. I can find if a application is running, thats not the problem. The problem is finding all open windows for that app. As of right now I can only get the active top most window of that app and thats it, but theres 20 more windows from the same app that are not being found. I understand software can be hidden and just because an open window isn't there doesn;t mean it's not running, understood but it has no bearing on what im trying to accomplish.
Simply put... if you have 10 instant message windows open using AIM, I need to find the title text for all of those windows, not just the mainwindowtitle.
I've been messing around with old school enumwindows but its not working in .NET, most likely im declaring the wrong type. As of now I butchered the code so badly I'm starting from scratch. I liked the first answer posted, makes sense im lost with delegates and need help in the area of vb.net.
Ahh ok, you aren't so much interested in the running process, as what the process is running (all its windows)
Yeah I am afraid you are stuck using win32_api for that unfortunatly
I forgot to thank everyone for their help, I'm just burnt out pounding my head against the wall over this so I apologize for my lack of understanding. But sincerely, thank you for your time in helping me with this, it's much appreciated.
Plater, I was hoping it wouldn't be so! haha... oh well...
Any advice on proper logic to use to go about doing this?
I was trying to start from getting the desktop hwnd and then going hwndnext, I dont know... I'm brainless right now.
Hi Tom,
Sorry, VB.NET really isn't my strong point but still, I've had a go at translating the code sample for you. I hope you can get some use out of it. - Imports System.Runtime.InteropServices
-
Imports System.Text
-
-
Public Class Test
-
-
<DllImport("USER32.DLL")> _
-
Private Shared Function GetShellWindow() As IntPtr
-
End Function
-
-
<DllImport("USER32.DLL")> _
-
Private Shared Function GetWindowText(ByVal hWnd As IntPtr, ByVal lpString As StringBuilder, ByVal nMaxCount As Integer) As Integer
-
End Function
-
-
<DllImport("USER32.DLL")> _
-
Private Shared Function GetWindowTextLength(ByVal hWnd As IntPtr) As Integer
-
End Function
-
-
<DllImport("user32.dll", SetLastError:=True)> _
-
Private Shared Function GetWindowThreadProcessId(ByVal hWnd As IntPtr, <Out()> ByRef lpdwProcessId As UInt32) As UInt32
-
End Function
-
-
<DllImport("USER32.DLL")> _
-
Private Shared Function IsWindowVisible(ByVal hWnd As IntPtr) As Boolean
-
End Function
-
-
Private Delegate Function EnumWindowsProc(ByVal hWnd As IntPtr, ByVal lParam As Integer) As Boolean
-
-
<DllImport("USER32.DLL")> _
-
Private Shared Function EnumWindows(ByVal enumFunc As EnumWindowsProc, ByVal lParam As Integer) As Boolean
-
End Function
-
-
Private hShellWindow As IntPtr = GetShellWindow()
-
Private dictWindows As New Dictionary(Of IntPtr, String)
-
Private currentProcessID As Integer
-
-
-
Public Function GetOpenWindowsFromPID(ByVal processID As Integer) As IDictionary(Of IntPtr, String)
-
dictWindows.Clear()
-
currentProcessID = processID
-
EnumWindows(AddressOf enumWindowsInternal, 0)
-
Return dictWindows
-
End Function
-
-
Private Function enumWindowsInternal(ByVal hWnd As IntPtr, ByVal lParam As Integer) As Boolean
-
If (hWnd <> hShellWindow) Then
-
Dim windowPid As UInt32
-
If Not IsWindowVisible(hWnd) Then
-
Return True
-
End If
-
Dim length As Integer = GetWindowTextLength(hWnd)
-
If (length = 0) Then
-
Return True
-
End If
-
GetWindowThreadProcessId(hWnd, windowPid)
-
If (windowPid <> currentProcessID) Then
-
Return True
-
End If
-
Dim stringBuilder As New StringBuilder(length)
-
GetWindowText(hWnd, stringBuilder, (length + 1))
-
dictWindows.Add(hWnd, stringBuilder.ToString)
-
End If
-
Return True
-
End Function
-
End Class
You should be able to call it in a similar fashion, although I haven't tested this even though it does compile, i.e: -
Dim processes As Process() = Process.GetProcessesByName("MyApp")
-
For Each process As Process In processes
-
Dim windows As IDictionary(Of IntPtr, String) = GetOpenWindowsFromPID(process.Id)
-
For Each kvp As KeyValuePair(Of IntPtr, String) In windows
-
' Do whatever you want here
-
Next
-
Next
NUKEFUSION!!!! THANK YOU!!
Works perfectly! I CANNOT say Thank you enough for this!!
I combined your code with the code I had for cycling through the open processes, then I ran your code to evaluate each process individually and it works like a charm to gather each and every sub windows title!
You seriously saved me from going insane over this issue, now I understand how to tackle things of this nature. Keep up the excellent coding, simply brilliant!
Your welcome. Glad you have managed to resolve your issue!
@nukefusion
Nukefusion,
I managed to modify the code as below -
Imports System.Runtime.InteropServices
-
Imports System.Text
-
-
Public Class Form1
-
-
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
-
For Each poc In Process.GetProcesses
-
If poc.MainWindowTitle.Length > 1 Then
-
-
Dim windows As IDictionary(Of IntPtr, String) = GetOpenWindowsFromPID(poc.Id)
-
For Each kvp As KeyValuePair(Of IntPtr, String) In windows
-
poc.EnableRaisingEvents = True
-
-
-
Try
-
ListBox1.Items.Add(" " & kvp.ToString)
-
Catch ex As Exception
-
MsgBox(poc.ProcessName.ToString & " " & ex.Message)
-
End Try
-
Next
-
-
End If
-
AddHandler poc.Exited, AddressOf OnProcessExit
-
Next
-
-
End Sub
-
<DllImport("USER32.DLL")> _
-
Private Shared Function GetShellWindow() As IntPtr
-
End Function
-
-
<DllImport("USER32.DLL")> _
-
Private Shared Function GetWindowText(ByVal hWnd As IntPtr, ByVal lpString As StringBuilder, ByVal nMaxCount As Integer) As Integer
-
End Function
-
-
<DllImport("USER32.DLL")> _
-
Private Shared Function GetWindowTextLength(ByVal hWnd As IntPtr) As Integer
-
End Function
-
-
<DllImport("user32.dll", SetLastError:=True)> _
-
Private Shared Function GetWindowThreadProcessId(ByVal hWnd As IntPtr, <Out()> ByRef lpdwProcessId As UInt32) As UInt32
-
End Function
-
-
<DllImport("USER32.DLL")> _
-
Private Shared Function IsWindowVisible(ByVal hWnd As IntPtr) As Boolean
-
End Function
-
-
Private Delegate Function EnumWindowsProc(ByVal hWnd As IntPtr, ByVal lParam As Integer) As Boolean
-
-
<DllImport("USER32.DLL")> _
-
Private Shared Function EnumWindows(ByVal enumFunc As EnumWindowsProc, ByVal lParam As Integer) As Boolean
-
End Function
-
-
Private hShellWindow As IntPtr = GetShellWindow()
-
Private dictWindows As New Dictionary(Of IntPtr, String)
-
Private currentProcessID As Integer
-
-
-
Public Function GetOpenWindowsFromPID(ByVal processID As Integer) As IDictionary(Of IntPtr, String)
-
dictWindows.Clear()
-
currentProcessID = processID
-
EnumWindows(AddressOf enumWindowsInternal, 0)
-
Return dictWindows
-
End Function
-
-
Private Function enumWindowsInternal(ByVal hWnd As IntPtr, ByVal lParam As Integer) As Boolean
-
If (hWnd <> hShellWindow) Then
-
Dim windowPid As UInt32
-
If Not IsWindowVisible(hWnd) Then
-
Return True
-
End If
-
Dim length As Integer = GetWindowTextLength(hWnd)
-
If (length = 0) Then
-
Return True
-
End If
-
GetWindowThreadProcessId(hWnd, windowPid)
-
If (windowPid <> currentProcessID) Then
-
Return True
-
End If
-
Dim stringBuilder As New StringBuilder(length)
-
GetWindowText(hWnd, stringBuilder, (length + 1))
-
dictWindows.Add(hWnd, stringBuilder.ToString)
-
End If
-
Return True
-
End Function
-
Private Sub OnProcessExit(ByVal sender As Object, ByVal e As EventArgs)
-
' Avoid late binding and Cast the Object to a Process.
-
Dim poc As Process = CType(sender, Process)
-
' Write the ExitCode
-
'ListBox4.Items.Add(p.ExitCode)
-
' Write the ExitTime
-
MsgBox(poc.ExitTime.ToString)
-
End Sub
-
End Class
-
-
As you can see, I've added the EnableRaisingEvents and an eventhandler to pop up the exittime whenever a window closes.
The problem now is, the exittime popup appears only when I close all child programs of a program.
For example, if I have 3 IEs open, the exittime pops up after i close all 3 IEs.
My goal is to get the exittime on each of the IE. Any idea how I can achieve that?
Thanks a bunch =)
You'll have to find out when the window handle is destroyed right?
@Plater
Yes exactly. I can't seem to get the idea on how I can enableraisingevents on each kvp.value.
Any other work-around? thanks :)
Unless you can find something in the WMI stuff, you'll probably just have to poll it and look for when the number of windows changes?
@Plater
oh my.. that means a revamp of the above code .. im just so close. the exit time pops up but its for each poc.id
i need it to be for each kvp.value. DAMNNNNNN
@Plater
thinking abt it, even so, how do i make it pop up the exit time? i cant enable raising events on windows.
All I can come up with is polling. Every X amount of time you check to see how many windows are open? Doesn't seem like a very good solution though.
@Plater
*sigh* this is bad. thanks anyway =)
Nukefusion,
This did exactly what I needed it to do. Very helpful.
Being that most people say to avoid the Win32 API's from .NET, it'd be helpful if there was a way to do this within .NET itself - but either way, this was exactly what I was looking for.
Thank you!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: sshuangw |
last post by:
Hello:
I am encountering a very weird issue with MDI child, Overriden WndProc
function and hidden form.
Basically, the application has two forms, Form1(parent form),
Form2(Child form),...
|
by: Bonj |
last post by:
Further to my last post, I have managed to get a child window to display. But its messages are routed to the same WNDPROC that the main window's messages are routed to - what is the way of...
|
by: Charles Law |
last post by:
For some reason, when I click the X to close my MDI parent form, the action
appears to be re-directed to one of the MDI child forms, and the parent
remains open. I am then unable to close the...
|
by: Darrell Wesley |
last post by:
I have a program that some else wrote but it's my duty to try and maintain
and in there are a couple of places where it makes a call to the
System.Diagnostics.Process.GetProcess in order to find...
|
by: Sheikko |
last post by:
Hi all,
i have an application and i wan to call another program into it, like
notepad, so when i minimize the notepad, or maximize, it must rest in
my application.
thank you
|
by: dan.jakubiec |
last post by:
I'm trying to write a Python app which accepts a socket connection and
then spawns another Python process to handle it. I need it to run
under both Linux and Windows. I have it working under...
|
by: ranju |
last post by:
I am trying to spawn a process (say an exe file) with different user
crendentials than that of the current user.
1) Called LogonUserEx() to logon the user and recieve a handle to the
token that...
|
by: geoffbache |
last post by:
Hi,
As part of my efforts to write a test tool that copes with GUIs
nicely, I'm trying to establish how I can start a GUI process on
Windows that will not bring up the window. So I try to hide...
|
by: Quill_Patricia |
last post by:
I have a Python script which is used to load data into a database. Up to
now this script has been run by customers from the Windows command
prompt using "python edg_loader.pyc". Any error messages...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
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...
|
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...
| |