锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
// ProcessHelper.h: interface for the ProcessHelper class.
//
/**///////////////////////////////////////////////////////////////////////
#if !defined(AFX_PROCESSHELPER_H__EA2A87A6_5E54_4610_8EDD_C5F8119D2976__INCLUDED_)
#define AFX_PROCESSHELPER_H__EA2A87A6_5E54_4610_8EDD_C5F8119D2976__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <Tlhelp32.h>
#include <Psapi.h>
#define ProcessBasicInformation 0
typedef struct

{
DWORD ExitStatus;
DWORD PebBaseAddress;
DWORD AffinityMask;
DWORD BasePriority;
ULONG UniqueProcessId;
ULONG InheritedFromUniqueProcessId;
} PROCESS_BASIC_INFORMATION;
// ntdll!NtQueryInformationProcess (NT specific!)
//
// The function copies the process information of the
// specified type into a buffer
//
// NTSYSAPI
// NTSTATUS
// NTAPI
// NtQueryInformationProcess(
// IN HANDLE ProcessHandle, // handle to process
// IN PROCESSINFOCLASS InformationClass, // information type
// OUT PVOID ProcessInformation, // pointer to buffer
// IN ULONG ProcessInformationLength, // buffer size in bytes
// OUT PULONG ReturnLength OPTIONAL // pointer to a 32-bit
// // variable that receives
// // the number of bytes
// // written to the buffer
// );
typedef LONG (WINAPI *PROCNTQSIP)(HANDLE,UINT,PVOID,ULONG,PULONG);
class ProcessHelper 

{
public:
ProcessHelper();
virtual ~ProcessHelper();
DWORD GetParentProcessID(DWORD dwId);
DWORD GetProcessFileName( DWORD dwId,LPTSTR lpImageFileName);
private:
PROCNTQSIP NtQueryInformationProcess;

};

ProcessHelper::ProcessHelper()

{
NtQueryInformationProcess = (PROCNTQSIP)GetProcAddress(
GetModuleHandle("ntdll"),
"NtQueryInformationProcess"
);
}
ProcessHelper::~ProcessHelper()

{
}
DWORD ProcessHelper::GetProcessFileName( DWORD dwId,LPTSTR lpImageFileName)

{
HANDLE hSnapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,dwId);
if (hSnapshot == INVALID_HANDLE_VALUE)
return (FALSE); 

MODULEENTRY32 me32 =
{0};
me32.dwSize = sizeof(MODULEENTRY32); 
if(! Module32First(hSnapshot,&me32))
{
CloseHandle(hSnapshot);
return (DWORD) -1;
}
strcpy(lpImageFileName,me32.szModule);
CloseHandle(hSnapshot);
return (DWORD)0;
}
DWORD ProcessHelper::GetParentProcessID(DWORD dwId)

{
if (!NtQueryInformationProcess) return -1;
LONG status;
DWORD dwParentPID = (DWORD)-1;
HANDLE hProcess;
PROCESS_BASIC_INFORMATION pbi;
// Get process handle
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION,FALSE,dwId);
if (!hProcess)
return (DWORD)-1;
// Retrieve information
status = NtQueryInformationProcess( hProcess,
ProcessBasicInformation,
(PVOID)&pbi,
sizeof(PROCESS_BASIC_INFORMATION),
NULL
);
// Copy parent Id on success
if (!status)
dwParentPID = pbi.InheritedFromUniqueProcessId;
CloseHandle (hProcess);
return dwParentPID;
}

#endif
#include聽<Tlhelp32.h>
//鍙栧緱鍐呭瓨涓殑瀹炰緥鏁?/SPAN>
int聽GetMemoryProcessCount(LPCSTR聽peFileName)

{
聽聽聽聽int聽result聽=聽0;
聽聽聽聽HANDLE聽hSnapshot;
聽聽聽聽PROCESSENTRY32聽pe;
聽聽聽聽pe.dwSize=sizeof(pe);
聽聽聽聽BOOL聽blExist=FALSE;
聽聽聽聽char聽sPath[MAX_PATH]聽=聽"";
聽聽聽聽strcpy(sPath,peFileName);
聽聽聽聽size_t聽len聽=聽strlen(sPath);聽聽聽聽
聽聽聽聽hSnapshot=::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
聽聽聽聽if(hSnapshot<0)聽goto聽L1;
聽聽聽聽if(::Process32First(hSnapshot,&pe)==FALSE)
聽聽聽聽
{
聽聽聽聽聽聽聽聽::CloseHandle(hSnapshot);聽goto聽L1;
聽聽聽聽}
聽聽聽聽if(_strnicmp(sPath,pe.szExeFile,len)==0)
聽聽聽聽
{聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽++聽result;
聽聽聽聽}
聽聽聽聽while(::Process32Next(hSnapshot,&pe))
聽聽聽聽
{
聽聽聽聽聽聽聽聽if(_strnicmp(sPath,pe.szExeFile,len)==0)
聽聽聽聽聽聽聽聽
{
聽聽聽聽聽聽聽聽聽聽聽聽++聽result;
聽聽聽聽聽聽聽聽}聽
聽聽聽聽}
L1:聽
聽聽聽聽::CloseHandle(hSnapshot);
聽聽聽聽return聽result;
}