激活application.exe 窗口标题1/窗口标题2/窗口标题3中的一个,先找到先激活。
调用ShowBondMainWindow()即可
#include "showmainwindow.h"
#include <Windows.h>
#include <Psapi.h>
#pragma comment (lib, "Psapi.lib")
//define members
#define MAX_WINDOW_COUNT 10 //support possible main window = 5
HWND wnd_handle_; //main window handle
int actual_window_count_ = 0; //max window count
wchar_t *window_title_[MAX_WINDOW_COUNT]={nullptr};//title of each main window
bool showProcessMainWindow(const wchar_t* process_name, const wchar_t* require_process_path, const wchar_t* titles)
{
DWORD process_id = 0;
if (!isExistingProcess(process_name, require_process_path, process_id))
{
return false;
}
//set window_title_ for enumWindowsProc method
splitString(titles);
EnumWindows((WNDENUMPROC)enumWindowsProc,process_id);
//delete the titles
releaseTitles();
ShowWindow(wnd_handle_, SW_SHOWNORMAL);
SetForegroundWindow(wnd_handle_);
return true;
}
bool isExistingProcess(const wchar_t* process_name,const wchar_t* require_process_path, DWORD &process_id )
{
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded ))
return false;
cProcesses = cbNeeded / sizeof(DWORD);
DWORD current_process = GetCurrentProcessId();
for ( i = 0; i < cProcesses; i++ )
{
if( aProcesses[i]!=0 && aProcesses[i]!=current_process )
{
if(matchProcessNameAndID(aProcesses[i], process_name, require_process_path))
{
process_id = aProcesses[i];
return true;
}
}
}
return false;
}
bool matchProcessNameAndID(DWORD process_id, const wchar_t * process_name, const wchar_t *require_process_path /*= nullptr*/)
{
wchar_t cur_process_name[MAX_PATH] = L"unknown";
bool ret = false;
HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION|PROCESS_VM_READ, FALSE, process_id );
if (nullptr == hProcess )
{
return false;
}
HMODULE hMod;
DWORD cbNeeded;
if (EnumProcessModules( hProcess, &hMod, sizeof(hMod), &cbNeeded) )
{
if (GetModuleBaseName(hProcess, hMod, cur_process_name,sizeof(cur_process_name)/sizeof(wchar_t)))
{
if (!wcscmp(cur_process_name, process_name))
{
if(nullptr != require_process_path)
{
//required process path
wchar_t *process_path = new wchar_t[MAX_PATH*2];
//get process path
if(getProcessPath(process_id, process_path))
{
if(isSamePath(require_process_path, process_path))
{
ret = true;
}
}
delete []process_path;
}
else
{
ret = true;
}
}
}
}
CloseHandle(hProcess);
return ret;
}
BOOL CALLBACK enumWindowsProc(HWND hwnd, DWORD lParam)
{
DWORD mpid;
GetWindowThreadProcessId(hwnd,&mpid);
if (mpid == lParam)
{
int i = GetWindowTextLength(hwnd);
wchar_t current_title[255];
GetWindowTextW(hwnd, current_title, i+1);
for(int j=0; j<actual_window_count_; j++)
{
//search for the first occurrence of window_title_[j] in current_title
if (wcsstr(current_title, window_title_[j]))
{
wnd_handle_ = hwnd;
return false;
}
}
}
return true;
}
void splitString(const wchar_t *source)
{
wchar_t *src = new wchar_t[1024];
wcscpy(src, source);
//delete titles
releaseTitles();
//new titles
wchar_t *delimit = wcstok(src, L";");
while (delimit != nullptr && actual_window_count_< MAX_WINDOW_COUNT ) {
int len = wcslen(delimit);
window_title_[actual_window_count_] = new wchar_t[len+1];
wcscpy(window_title_[actual_window_count_],delimit);
delimit = wcstok(nullptr, L";");
actual_window_count_++;
}
delete []src;
}
void releaseTitles()
{
for(int i = 0; i<actual_window_count_; i++)
{
delete[] window_title_[i];
window_title_[i] = nullptr;
}
actual_window_count_ = 0;
}
bool getProcessPath(DWORD process_id, wchar_t *file_path)
{
if(nullptr == file_path)
{
return false;
}
bool ret = false;
HANDLE processHandle = nullptr;
processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, process_id);
if (processHandle != nullptr) {
if (!GetModuleFileNameExW(processHandle, NULL, file_path, MAX_PATH))
{
wprintf(L"Failed to get module filename.\n");
}
else
{
wprintf(L"Module filename is: %s\n", file_path);
ret = true;
}
CloseHandle(processHandle);
} else {
wprintf(L"Failed to open process.\n");
}
return ret;
}
bool isSamePath(const wchar_t *src, const wchar_t *des)
{
bool ret = false;
while(*src && *des)
{
if(*src != *des)
{
if (!(*src == L'\\' || *src == L'/') &&(*des == L'\\' || *des == L'/'))
{
return false;
}
}
src++;
des++;
}
if(*src == 0 || *src == 0)
{
ret = true;
}
return ret;
}
void ShowAppMainWindow()
{
showProcessMainWindow(L"application.exe", nullptr, L"窗口标题1;窗口标题2;窗口标题3");
}