Wisnu
asked on
How do I get Window Title of all opened process?
I need to detect particular opened apps in my desktop to decide further process in my program by getting its window title. For instance, I will search "Untitled - Notepad" to ensure Notepad is opened and will search "Calculator" to ensure Calculator is opened. Please see the following image for what window title is:
The following is the code I have built.
However, I get the title of my Visual Studio only when I run debug on it. I also get the command line title if I run it from windows explorer. The following is the output I obtain:
The question, what's wrong with my code so I cannot get the window title for all active process? Thanks.
The following is the code I have built.
#include "stdafx.h"
#include <windows.h>
#include <tlhelp32.h>
#include <tchar.h>
// Forward declarations:
BOOL GetProcessList();
void printError(TCHAR* msg);
int main(void)
{
GetProcessList();
return 0;
}
BOOL ListProcessThreads(DWORD dwOwnerPID)
{
HANDLE hThreadSnap = INVALID_HANDLE_VALUE;
THREADENTRY32 te32;
GUITHREADINFO guiInfo = { sizeof(guiInfo) };
DWORD dwErr = 0;
LPTSTR app1 = _T("Untitled - Notepad");
LPTSTR app2 = _T("Calculator");
// Take a snapshot of all running threads
hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (hThreadSnap == INVALID_HANDLE_VALUE)
return(FALSE);
// Fill in the size of the structure before using it.
te32.dwSize = sizeof(THREADENTRY32);
// Retrieve information about the first thread,
// and exit if unsuccessful
if (!Thread32First(hThreadSnap, &te32))
{
printError(TEXT("Thread32First")); // Show cause of failure
CloseHandle(hThreadSnap); // Must clean up the snapshot object!
return(FALSE);
}
// Now walk the thread list of the system,
// and display information about each thread
// associated with the specified process
do
{
if (GetGUIThreadInfo(te32.th32OwnerProcessID, &guiInfo))
{
LPTSTR winName = new TCHAR[256];
GetWindowText(guiInfo.hwndActive, winName, 255);
_tprintf(TEXT("\n%s"), winName);
if (winName == app1)
_tprintf(TEXT("\nNotepad found")); // Then do something
else if (winName == app2)
_tprintf(TEXT("\nCalculator found")); // Then do something
delete[] winName;
}
else
{
dwErr = GetLastError();
}
} while (Thread32Next(hThreadSnap, &te32));
_tprintf(TEXT("\n"));
// Don't forget to clean up the snapshot object.
CloseHandle(hThreadSnap);
return(TRUE);
}
BOOL GetProcessList()
{
HANDLE hProcessSnap;
HANDLE hProcess;
PROCESSENTRY32 pe32;
DWORD dwPriorityClass;
// Take a snapshot of all processes in the system.
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE)
{
printError(TEXT("CreateToolhelp32Snapshot (of processes)"));
return(FALSE);
}
// Set the size of the structure before using it.
pe32.dwSize = sizeof(PROCESSENTRY32);
// Retrieve information about the first process,
// and exit if unsuccessful
if (!Process32First(hProcessSnap, &pe32))
{
printError(TEXT("Process32First")); // show cause of failure
CloseHandle(hProcessSnap); // clean the snapshot object
return(FALSE);
}
// Now walk the snapshot of processes, and
// display information about each process in turn
do
{
ListProcessThreads(pe32.th32ProcessID);
} while (Process32Next(hProcessSnap, &pe32));
CloseHandle(hProcessSnap);
return(TRUE);
}
void printError(TCHAR* msg)
{
DWORD eNum;
TCHAR sysMsg[256];
TCHAR* p;
eNum = GetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, eNum,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
sysMsg, 256, NULL);
// Trim the end of the line and terminate it with a null
p = sysMsg;
while ((*p > 31) || (*p == 9))
++p;
do { *p-- = 0; } while ((p >= sysMsg) &&
((*p == '.') || (*p < 33)));
// Display the message
_tprintf(TEXT("\n WARNING: %s failed with error %d (%s)"), msg, eNum, sysMsg);
}
However, I get the title of my Visual Studio only when I run debug on it. I also get the command line title if I run it from windows explorer. The following is the output I obtain:
The question, what's wrong with my code so I cannot get the window title for all active process? Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you, trinitrotoluene. It works like a charm.
ASKER
It works like a charm.
https://msdn.microsoft.com/en-us/library/windows/desktop/ms633498(v=vs.85).aspx