thanks
based on your cooperation and my investigations the following code was resulted. i would expect such an answer.
good luck! :)
struct handle_data
{
unsigned long process_id;
HWND best_handle;
};
BOOL is_main_window(HWND handle)
{
return GetWindow(handle, GW_OWNER) == (HWND)0 && IsWindowVisible(handle);
}
BOOL CALLBACK enum_windows_callback(HWND handle, LPARAM lParam)
{
handle_data& data = *(handle_data*)lParam;
unsigned long process_id = 0;
GetWindowThreadProcessId(handle, &process_id);
if (data.process_id != process_id || !is_main_window(handle))
return TRUE;
data.best_handle = handle;
return FALSE;
}
HWND find_main_window(unsigned long process_id)
{
handle_data data;
data.process_id = process_id;
data.best_handle = 0;
EnumWindows(enum_windows_callback, (LPARAM)&data);
return data.best_handle;
}
CString file_name(LPCSTR path)
{
int len = strlen(path);
for (int i = len - 1; i >= 0; i--)
if (path[i] == '\\')
return path + i + 1;
return "";
}
bool is_running(LPCSTR prog)
{
void *buf = NULL;
int estimatedRequiredBufSize = 300;
DWORD requiredBufSize;
while (true)
{
buf = realloc(buf, estimatedRequiredBufSize);
VERIFY(EnumProcesses((DWORD *) buf, estimatedRequiredBufSize, &requiredBufSize));
if (estimatedRequiredBufSize == requiredBufSize)
estimatedRequiredBufSize *= 2;
else
break;
}
DWORD *pProcessIds = (DWORD *) buf;
int numProcesses = requiredBufSize / sizeof(DWORD);
for (int i=0; i<numprocesses;> {
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pProcessIds[i]);
if (hProcess)
{
char path[_MAX_PATH];
DWORD size = sizeof(path) - 1, wholeSize = size;
VERIFY(QueryFullProcessImageName(hProcess, 0, path, &size));
ASSERT(size < wholeSize);
if (file_name(path) == prog)
{
HWND hWnd = find_main_window(pProcessIds[i]);
VERIFY(SetForegroundWindow(hWnd));
free(pProcessIds);
return true;
}
}
}
free(pProcessIds);
return false;
}