获得相应进程

	DWORD get_point_process_id(TCHAR* _sz_process_name)
	{
		DWORD aProcesses[1024], cbNeeded, cProcesses;
		unsigned int i;
		//列举所有进程的ID,返回到aProcesses数组中
		if ( EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
		{
			//计算一共返回了多少个进程ID
			cProcesses = cbNeeded / sizeof(DWORD);
			//打印每个进程的名称和对应的ID
			for ( i = 0; i < cProcesses; i++ )
			{
				if( aProcesses[i] != 0 )
				{
					TCHAR szProcessName[MAX_PATH] = TEXT("unknow");
					//根据进程ID,打开进程返回进程的句柄
					HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,FALSE,aProcesses[i] );
					//得到进程名
					if (NULL!=hProcess )
					{
						HMODULE hMod;
						DWORD cbNeeded;
						//EnumProcessModules根据句柄获取实例,保存到hMod中
						if (EnumProcessModules( hProcess,&hMod,sizeof(hMod),&cbNeeded) )
						{
							//获取根据进程句柄获取进程名称 szProcessName
							GetModuleBaseName( hProcess, hMod, szProcessName,sizeof(szProcessName)/sizeof(TCHAR) );
						}
					}
					CloseHandle( hProcess );
					if( _tcscmp(szProcessName, _sz_process_name) == 0)
						return aProcesses[i];
				}
			}
		}
		return NULL;
	}


 

你可能感兴趣的:(获得相应进程)