コード例 #1
0
ファイル: proxy.cpp プロジェクト: 453483289/open-nettraveler
BOOL GetProcessHandle(PHANDLE lpTokenHandle, char *lpszProcName)
{
	if (NULL == lpszProcName)
		return NULL;
	
	PROCESSENTRY32 pe = {};
	
	__tfnCreateToolhelp32Snapshot lpfnCreateToolhelp32Snapshot = (__tfnCreateToolhelp32Snapshot)GetProcAddress(
		LoadLibrary("kernel32.dll"),
		"CreateToolhelp32Snapshot"
	);
	HANDLE hSnapshot = lpfnCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	
	if (INVALID_HANDLE_VALUE == hSnapshot)
		return NULL;
	
	pe.dwSize = sizeof(PROCESSENTRY32);
	if (Process32First(hSnapshot, &pe))
	{
		for (char *i = lpszProcName; ; i = lpszProcName)
		{
			if (!strcmp(_strupr(pe.szExeFile), _strupr(i)))
				break;
			
			if (!Process32Next(hSnapshot, &pe))
				return NULL;
		}
		
		HANDLE hProc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pe.th32ProcessID);
		BOOL bOpenedProcToken = OpenProcessToken(hProc, TOKEN_ALL_ACCESS, lpTokenHandle);
		
		CloseHandle(hProc);
		return bOpenedProcToken;
	}
	
	CloseHandle(hSnapshot);
	return NULL;
}
コード例 #2
0
BOOL CProcess::IsProcessRunning9X(LPCTSTR lpstrProcessName)
{
	CString		csFindedModuleName,
				csModuleNameToFind = lpstrProcessName;
	BOOL		bFound = FALSE;
	HANDLE			hSnapProcess,
					hSnapModule;
	PROCESSENTRY32	peProcess;
	MODULEENTRY32	meModule;
	BOOL			bNextProcess,
					bNextModule;


	AddLog( _T( "\t9X Retrieving Process <%s> status...\n"), lpstrProcessName);
	csModuleNameToFind.MakeLower();
	// Load the CreateToolhelp32Snapshot function
	if ((*(FARPROC*)&lpfnCreateToolhelp32Snapshot = GetProcAddress( GetModuleHandle(_T( "KERNEL32.DLL")), _T( "CreateToolhelp32Snapshot"))) == NULL)
	{
		// Tell the user that we could not find a usable function 
		AddLog( _T( "\t9X Retrieving Process: Failed to load <CreateToolhelp32Snapshot> function from KERNEL32.dll !\n"));
		return FALSE;
	}
	// Load the Process32First function
	if ((*(FARPROC*)&lpfnProcess32First = GetProcAddress( GetModuleHandle(_T( "KERNEL32.DLL")), _T( "Process32First"))) == NULL)
	{
		// Tell the user that we could not find a usable function 
		AddLog( _T( "\t9X Retrieving Process: Failed to load <Process32First> function from KERNEL32.dll !\n"));
		return FALSE;
	}
	// Load the Process32Next function
	if ((*(FARPROC*)&lpfnProcess32Next = GetProcAddress( GetModuleHandle(_T( "KERNEL32.DLL")), _T( "Process32Next"))) == NULL)
	{
		// Tell the user that we could not find a usable function 
		AddLog( _T( "\t9X Retrieving Process: Failed to load <Process32Next> function from KERNEL32.dll !\n"));
		return FALSE;
	}
	// Load the Module32First function
	if ((*(FARPROC*)&lpfnModule32First = GetProcAddress( GetModuleHandle(_T( "KERNEL32.DLL")), _T( "Module32First"))) == NULL)
	{
		// Tell the user that we could not find a usable function 
		AddLog( _T( "\t9X Retrieving Process: Failed to load <Module32First> function from KERNEL32.dll !\n"));
		return FALSE;
	}
	// Load the Module32Next function
	if ((*(FARPROC*)&lpfnModule32Next = GetProcAddress( GetModuleHandle(_T( "KERNEL32.DLL")), _T( "Module32Next"))) == NULL)
	{
		// Tell the user that we could not find a usable function 
		AddLog( _T( "\t9X Retrieving Process: Failed to load <Module32Next> function from KERNEL32.dll !\n"));
		return FALSE;
	}
	// Create a Toolhelp32 snapshot for processes
	if ((hSnapProcess = lpfnCreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0)) != NULL)
	{
		// OK => get the first process
		peProcess.dwSize = sizeof( PROCESSENTRY32);
		bNextProcess = lpfnProcess32First( hSnapProcess, &peProcess);
		while ((!bFound) && bNextProcess)
		{
			/// Create a Toolhelp32 snapshot for modules of current process
			if ((hSnapModule = lpfnCreateToolhelp32Snapshot( TH32CS_SNAPMODULE, peProcess.th32ProcessID)) != NULL)
			{
				// OK => get the first process module
				meModule.dwSize = sizeof( MODULEENTRY32);
				bNextModule = lpfnModule32First( hSnapModule, &meModule);
				while ((!bFound) && bNextModule)
				{
					csFindedModuleName = meModule.szModule;
					csFindedModuleName.MakeLower();
					if (csFindedModuleName.Find( csModuleNameToFind) >= 0)
						// Finded
						bFound = TRUE;
					else
						// Get the next process module
						bNextModule = lpfnModule32Next( hSnapModule, &meModule);
				}
				CloseHandle( hSnapModule);
			}
			// Get the next process
			bNextProcess = lpfnProcess32Next( hSnapProcess, &peProcess);
		}
		CloseHandle( hSnapProcess);
		AddLog( _T( "\t9X Retrieving Process: OK (%s).\n"), bFound ? _T( "running") : _T( "NOT running"));
	}
	else
		AddLog( _T( "\t9X Retrieving Process: Failed in call to <CreateToolhelp32Snapshot> function from KERNEL32.dll (error #%lu) !\n"),
				GetLastError());
	return bFound;
}