My laptop has a button to run calculator, but each time I press it, it spawns a new process instead of focusing on an existing one. I've decided to write a small application to focus on calculator's window if it exists or spawn a new one.
#include "pch.h"
#include <iostream>
#include <Windows.h>
#include <WtsApi32.h>
HWND find_top_window(DWORD pid)
{
std::pair<HWND, DWORD> params = { 0, pid };
BOOL bResult = EnumWindows([](HWND hwnd, LPARAM lParam) -> BOOL
{
auto pParams = (std::pair<HWND, DWORD>*)(lParam);
DWORD processId;
if (GetWindowThreadProcessId(hwnd, &processId) && processId == pParams->second)
{
SetLastError(-1);
pParams->first = hwnd;
return FALSE;
}
return TRUE;
}, (LPARAM)¶ms);
if (!bResult && GetLastError() == -1 && params.first)
{
return params.first;
}
return 0;
}
///
int main(int argc, char* argv[])
{
LPCWSTR szProcessName = TEXT("Calculator");
WTS_PROCESS_INFO* pWPIs = NULL;
DWORD dwProcCount = 0;
if (WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE, NULL, 1, &pWPIs, &dwProcCount))
{
for (DWORD i = 0; i < dwProcCount; i++)
{
if (wcsstr(pWPIs[i].pProcessName, szProcessName) != NULL)
{
std::cout << "Found!\npid: " << pWPIs[i].ProcessId << "\n";
HWND hWnd = find_top_window(pWPIs[i].ProcessId);
std::cout << "hwnd:" << hWnd << "\n";
if (hWnd != 0)
{
ShowWindow(hWnd, SW_SHOW);
//ShowWindow(hWnd, SW_RESTORE);
}
break;
}
}
}
if (pWPIs)
{
WTSFreeMemory(pWPIs);
pWPIs = NULL;
}
return 0;
}
For some reason, it fails to ShowWindow
- window is not being shown.
Need help on this one - I do not know why it fails.