1) Enumerating the processes:
DWORD processes[1024], processes_size, i, n;
char buffer[256];
HANDLE ph;
memset(processes, 0, 1024 * sizeof(DWORD));
FILE* fp = fopen("results.txt", "w");
if(EnumProcesses(processes, sizeof(processes), &processes_size))
for(i=0; i<processes_size; i++) {
if(processes[i] == 0) continue;
ph = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, processes[i]);
n = GetModuleBaseNameA(ph, 0, buffer, sizeof(buffer));
CloseHandle(ph);
if(n==0) continue;
fprintf(fp, "%d\t%d\t%s\n", i, n, buffer);
}
fclose(fp);
2) Using a thread with timeout
DAQ_Device* Device;
DWORD DevInitProc(LPBYTE lpData) {
DAQ_Device* Dev = (DAQ_Device*) lpData;
return Dev->HardwareInit(); }
case WM_CREATE: {
Device = new DAQ_Device(hWnd);
DWORD dwThreadID;
HANDLE hSSIThread = CreateThread((LPSECURITY_ATTRIBUTES) NULL, 0, (LPTHREAD_START_ROUTINE) DevInitProc, (LPVOID) Device, 0, &dwThreadID);
if(hSSIThread == NULL) { ErrorReport(MODULE, __LINE__); return -1; }
DWORD Status = WaitForSingleObject(hSSIThread, 500);
switch (Status) {
case WAIT_FAILED: { ErrorReport(MODULE, __LINE__); return -1; }
case WAIT_OBJECT_0: { DWORD dwExitCode;
if(!GetExitCodeThread(hSSIThread, &dwExitCode)) { ErrorReport(MODULE, __LINE__); return -1; }
if(!dwExitCode) { MessageBox(hWnd, "Instrument not found.\nCheck the power switch.", " Attention:", MB_OK | MB_ICONEXCLAMATION);
return -1;
}
if(!CloseHandle(hSSIThread)) { ErrorReport(MODULE, __LINE__); return -1; }
break; }
case WAIT_TIMEOUT: {
MessageBox(hWnd, "A hardware initialization problem detected.\nPlease call for instructions.", " Attention:", MB_OK | MB_ICONEXCLAMATION);
TerminateThread(hSSIThread, 0);
CloseHandle(hSSIThread);
return -1;
}
}
break;
}