CToolHelp
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#pragma once
//Header file for CToolHelp
#include
#pragma warning(disable:4244)
#pragma warning(disable:4312)
class CToolHelp
{
private:
HANDLE m_hSnapshot;
public:
CToolHelp(DWORD dwFlags=0,DWORD dwProcessID=0);
~CToolHelp();
BOOL CreateSnapshot(DWORD dwFlags,DWORD dwProcessID=0);
BOOL ProcessFirst(LPPROCESSENTRY32 ppe) const;
BOOL ProcessNext(LPPROCESSENTRY32 ppe) const;
BOOL ProcessFind(DWORD dwProcessID,LPPROCESSENTRY32 ppe) const;
BOOL ModuleFirst(PMODULEENTRY32 pme) const;
BOOL ModuleNext(PMODULEENTRY32 pme) const;
BOOL ModuleFind(PVOID pvBaseAddr,PMODULEENTRY32 pme) const;
BOOL ModuleFind(PTSTR pszModName,PMODULEENTRY32 pme) const;
BOOL ThreadFirst(PTHREADENTRY32 pte) const;
BOOL ThreadNext(PTHREADENTRY32 pte) const;
BOOL HeapListFirst(PHEAPLIST32 phl) const;
BOOL HeapListNext(PHEAPLIST32 phl) const;
int HowManyHeaps() const;
BOOL HeapFirst(PHEAPENTRY32 phe,DWORD dwProcessID,DWORD dwHeapID) const;
BOOL HeapNext(PHEAPENTRY32 phe) const;
int HowManyBlocksInHeap(DWORD dwProcessID,DWORD dwHeapID) const;
BOOL IsAHeap(HANDLE hProcess,PVOID pvBlock,PDWORD pdwFlags) const;
public:
static BOOL EnableDebugPrivilege(BOOL fEnable=TRUE);
static BOOL ReadProcessMemory(DWORD dwProcessID,LPCVOID lpBaseAddress,LPVOID pvBuffer,DWORD cbSize,PDWORD pdwNumberOfBytesRead=NULL);
static PVOID GetModulePreferredBaseAddr(DWORD dwProcessID,PVOID pvModuleRemote);
};
inline CToolHelp::CToolHelp(DWORD dwFlags/* =0 */,DWORD dwProcessID/* =0 */){
m_hSnapshot = INVALID_HANDLE_VALUE;
CreateSnapshot(dwFlags,dwProcessID);
}
inline CToolHelp::~CToolHelp(){
CloseHandle(m_hSnapshot);
}
inline BOOL CToolHelp::CreateSnapshot(DWORD dwFlags,DWORD dwProcessID/* =0 */){
SAFE_CLOSE_HANDLE(m_hSnapshot);
if(dwFlags==0){
m_hSnapshot = INVALID_HANDLE_VALUE;
}else{
m_hSnapshot = CreateToolhelp32Snapshot(dwFlags,dwProcessID);
}
return m_hSnapshot!=INVALID_HANDLE_VALUE;
}
inline BOOL CToolHelp::EnableDebugPrivilege(BOOL fEnable/* =TRUE */){
BOOL fOK = FALSE;
HANDLE hToken = NULL;
if(OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&hToken)){
TOKEN_PRIVILEGES tp;
tp.PrivilegeCount =1;
LookupPrivilegeValue(NULL,SE_DEBUG_NAME,&tp.Privileges[0].Luid);
tp.Privileges[0].Attributes = fEnable ? SE_PRIVILEGE_ENABLED : 0;
AdjustTokenPrivileges(hToken,FALSE,&tp,sizeof(tp),NULL,NULL);
fOK = (GetLastError()==ERROR_SUCCESS);
CloseHandle(hToken);
}
return fOK;
}
inline BOOL CToolHelp::ReadProcessMemory(DWORD dwProcessID,LPCVOID lpBaseAddress,LPVOID pvBuffer,DWORD cbSize,PDWORD pdwNumberOfBytesRead/* =NULL */){
return Toolhelp32ReadProcessMemory(dwProcessID,lpBaseAddress,pvBuffer,cbSize,pdwNumberOfBytesRead);
}
inli
ne BOOL CToolHelp::ProcessFirst(LPPROCESSENTRY32 ppe)const{
BOOL fOK = Process32First(m_hSnapshot,ppe);
if(fOK && (ppe-> th32ProcessID==0))
fOK = ProcessNext(ppe);
return fOK;
}
inline BOOL CToolHelp::ProcessNext(LPPROCESSENTRY32 ppe)const{
BOOL fOK = Process32Next(m_hSnapshot,ppe);
if(fOK && (ppe-> th32ProcessID==0))
fOK = ProcessNext(ppe);
return fOK;
}
inline BOOL CToolHelp::ProcessFind(DWORD dwProcessID,LPPROCESSENTRY32 ppe)const{
BOOL bFound = FALSE;
for(BOOL fOK=ProcessFirst(ppe);fOK;fOK=ProcessNext(ppe)){
bFound =(ppe-> th32ProcessID==dwProcessID);
if(bFound)break;
}
return bFound;
}
inline BOOL CToolHelp::ModuleFirst(PMODULEENTRY32 pme)const{
return Module32First(m_hSnapshot,pme);
}
inline BOOL CToolHelp::ModuleNext(PMODULEENTRY32 pme)const{
return Module32Next(m_hSnapshot,pme);
}
inline BOOL CToolHelp::ModuleFind(PVOID pvBaseAddr,PMODULEENTRY32 pme) const{
BOOL bFound = FALSE;
for(BOOL fOK=ModuleFirst(pme);fOK;fOK=ModuleNext(pme)){
bFound =(pme-> modBaseAddr==pvBaseAddr);
if(bFound)break;
}
return bFound;
}
inline BOOL CToolHelp::ModuleFind(PTSTR pszModName,PMODULEENTRY32 pme)const{
BOOL bFound = FALSE;
for(BOOL fOK=ModuleFirst(pme);fOK;fOK=ModuleNext(pme)){
bFound =(lstrcmpi(pme-> szModule,pszModName)==0) || (lstrcmpi(pme-> szExePath,pszModName)==0);
if(bFound)break;
}
return bFound;
}
inline BOOL CToolHelp::ThreadFirst(PTHREADENTRY32 pte)const{
return Thread32First(m_hSnapshot,pte);
}
inline BOOL CToolHelp::ThreadNext(PTHREADENTRY32 pte)const{
return Thread32Next(m_hSnapshot,pte);
}
inline int CToolHelp::HowManyHeaps() const{
int nHowManyHeaps=0;
HEAPLIST32 hl={sizeof(hl)};
for(BOOL fOK=HeapListFirst(&hl);fOK;fOK=HeapListNext(&hl))
++nHowManyHeaps;
return nHowManyHeaps;
}
inline int CToolHelp::HowManyBlocksInHeap(DWORD dwProcessID,DWORD dwHeapID) const{
int nHowManyBlocksInHeap=0;
HEAPENTRY32 he={sizeof(he)};
for(BOOL fOK=HeapFirst(&he,dwProcessID,dwHeapID);fOK;fOK=HeapNext(&he))
++nHowManyBlocksInHeap;
return nHowManyBlocksInHeap;
}
inline BOOL CToolHelp::HeapListFirst(PHEAPLIST32 phl) const{
return Heap32ListFirst(m_hSnapshot,phl);
}
inline BOOL CToolHelp::HeapListNext(PHEAPLIST32 phl) const{
return Heap32ListNext(m_hSnapshot,phl);
}
inline BOOL CToolHelp::HeapFirst(PHEAPENTRY32 phe,DWORD dwProcessID,DWORD dwHeapID) const{
return Heap32First(phe,dwProcessID,dwHeapID);
}
inline BOOL CToolHelp::HeapNext(PHEAPENTRY32 phe)const{
return Heap32Next(phe);
}
inline BOOL CToolHelp::IsAHeap(HANDLE hProcess,PVOID pvBlock,PDWORD pdwFlags)const{
HEAPLIST32 hl={sizeof(hl)};
for(BOOL fOK=HeapListFirst(&hl);fOK;HeapListNext(&hl)){
HEAPENTRY32 he={sizeof(he)};
for(BO
OL fOk=HeapFirst(&he,hl.th32ProcessID,hl.th32HeapID);fOk;fOk=HeapNext(&he)){
MEMORY_BASIC_INFORMATION mbi;
VirtualQueryEx(hProcess,(PVOID)he.dwAddress,&mbi,sizeof(mbi));
if(PBYTE(pvBlock)> =PBYTE(mbi.AllocationBase) && PBYTE(pvBlock) <=(PBYTE(mbi.AllocationBase)+mbi.RegionSize)){
*pdwFlags=hl.dwFlags;
}
}
return FALSE;
}
}
inline PVOID CToolHelp::GetModulePreferredBaseAddr(DWORD dwProcessID,PVOID pvModuleRemote){
PVOID pvModulePreferredBaseAddr=NULL;
IMAGE_DOS_HEADER idh;
IMAGE_NT_HEADERS inth;
Toolhelp32ReadProcessMemory(dwProcessID,pvModuleRemote,&idh,sizeof(idh),NULL);
if(idh.e_magic==IMAGE_DOS_SIGNATURE){
Toolhelp32ReadProcessMemory(dwProcessID,(PBYTE)pvModuleRemote+idh.e_lfanew,&inth,sizeof(inth),NULL);
if(inth.Signature==IMAGE_NT_SIGNATURE){
pvModulePreferredBaseAddr=PVOID(inth.OptionalHeader.ImageBase);
}
}
return pvModulePreferredBaseAddr;
}