00001 00007 #include "stdafx.h" 00008 #include <tlhelp32.h> 00009 #include "findproc.h" 00010 00011 #ifdef UNDER_CE 00012 #pragma comment(lib, "toolhelp") 00013 #else 00014 #define CloseToolhelp32Snapshot CloseHandle 00015 #endif 00016 00017 int FindProcessByNames(HANDLE hSnapshot, TCHAR **pszNames, unsigned NameCount, bool RestartSearch, unsigned *pMatchedIndex) 00018 { 00019 if (hSnapshot == INVALID_HANDLE_VALUE) 00020 return 0; 00021 int PID = 0; 00022 PROCESSENTRY32 pe = {0,}; 00023 pe.dwSize = sizeof(PROCESSENTRY32); 00024 if (RestartSearch) 00025 Process32First(hSnapshot,&pe); 00026 else 00027 Process32Next(hSnapshot,&pe); 00028 do 00029 { 00030 for (unsigned i = 0; i < NameCount; i++) 00031 { 00032 if (!_tcsicmp(pe.szExeFile, pszNames[i])) 00033 { 00034 PID = pe.th32ProcessID; 00035 if (pMatchedIndex) 00036 *pMatchedIndex = i; 00037 break; 00038 } 00039 } 00040 } while (Process32Next(hSnapshot,&pe)); 00041 return PID; 00042 } 00043 00044 int FindProcessByName(HANDLE hSnapshot, TCHAR *pszName, bool RestartSearch) 00045 { 00046 return FindProcessByNames(hSnapshot, &pszName, 1, RestartSearch, NULL); 00047 } 00048 00049 00050 int FindProcessByName(TCHAR *pszName) 00051 { 00052 HANDLE hShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); 00053 int PID = FindProcessByName(hShot, pszName, true); 00054 CloseToolhelp32Snapshot(hShot); 00055 return PID; 00056 } 00057 00058 LPVOID GetModuleFromProcess(int PID, TCHAR *pszModuleName) 00059 { 00060 HANDLE hShot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, PID); 00061 if (hShot == INVALID_HANDLE_VALUE) 00062 return NULL; 00063 LPVOID pMod = 0; 00064 00065 MODULEENTRY32 me; 00066 me.dwSize = sizeof(me); 00067 Module32First(hShot, &me); 00068 bool FoundModule = false; 00069 do 00070 { 00071 if (!_tcsicmp(me.szModule, pszModuleName)) 00072 { 00073 pMod = me.modBaseAddr; 00074 break; 00075 } 00076 } while (Module32Next(hShot, &me)); 00077 00078 CloseToolhelp32Snapshot(hShot); 00079 return pMod; 00080 } 00081 00082 int GetFirstThreadID(int PID) 00083 { 00084 HANDLE hShot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, PID); 00085 if (hShot == INVALID_HANDLE_VALUE) 00086 return 0; 00087 LPVOID pMod = 0; 00088 00089 THREADENTRY32 te; 00090 te.dwSize = sizeof(te); 00091 Thread32First(hShot, &te); 00092 int ID = 0; 00093 do 00094 { 00095 if (te.th32OwnerProcessID == PID) 00096 ID = te.th32ThreadID; 00097 } while (Thread32Next(hShot, &te)); 00098 CloseToolhelp32Snapshot(hShot); 00099 return ID; 00100 }