如何以编程方式确定进程是否在系统上运行? [英] How do I programmatically determine if a process is running on a system?

查看:86
本文介绍了如何以编程方式确定进程是否在系统上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Win32 API,并且有一个应用程序可以控制硬件并在开始时对其进行初始化。硬件驱动程序正在运行,偶尔调用其中一个SDK函数永远不会返回...并且整个应用程序只是挂起。该过程仍然可以在Windows任务管理器的列表中看到,并且必须在重新启动应用程序之前手动结束。

我正在寻找从我的程序中执行此操作的方法。 br />
谢谢。



评论版本的问题:

我如何在我的应用程序启动期间以编程方式确定它的另一个实例是否已在系统上运行,并且最好如何终止该进程?

I am using Win32 API and have an application that controls a piece of hardware and initializes it upon the start. The hardware driver is acting up and once in a while the call to one of the SDK functions never returns... and the entire application just hangs up. The process is still visible in the list in Windows Task Manager, and it has to be manually ended before the application can be restarted.
I am looking for a way to do this from inside my program.
Thank you.

Reviewed version of the question:
How do I programmatically determine during the start of my application if another instance of it is already running on a system and, preferably, how do I terminate that process?

推荐答案

尝试

如何检查应用程序已经在运行 [ ^ ]

http://forums.devshed.com/progr amming-42 / checking-process-running-including-psapi-dll-640352.html [ ^ ]


1)枚举过程:

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)使用超时的线程



2) Using a thread with timeout

DAQ_Device* Device;


DWORD DevInitProc(LPBYTE lpData) {
DAQ_Device* Dev = (DAQ_Device*) lpData;
return Dev->HardwareInit();		// returns TRUE on success
}


// In the application main window process:
case WM_CREATE: {
	//.................
	Device = new DAQ_Device(hWnd);	// The constructor just allocates memory and zeroes the variables

	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: {	// Normal thread termination
			DWORD dwExitCode;
			// What did DevInitProc() return?
			if(!GetExitCodeThread(hSSIThread, &dwExitCode)) { ErrorReport(MODULE, __LINE__); return -1; }
			if(!dwExitCode) {	// DevInitProc() returned FALSE
				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;	// DevInitProc returned TRUE, continue with WM_CREATE
			}
		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;
	}	// WM_CREATE


这篇关于如何以编程方式确定进程是否在系统上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆