舉例代碼:
#include
#include
#include
int main()
{
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(pe32);
HANDLE hProcessSnap;
BOOL bMore;
int count = 0;
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(hProcessSnap == INVALID_HANDLE_VALUE)
{
printf("CreateToolhelp 函數調用失敗
");
return 0;
}
bMore = Process32First(hProcessSnap,&pe32);
printf("%20s %10s
","進程名","PID");
printf("======================================
");
while(bMore)
{
count++;
printf("%20s %10d
",pe32.szExeFile,pe32.th32ProcessID);
bMore = Process32Next(hProcessSnap,&pe32);
}
CloseHandle(hProcessSnap);
return 0;
}
-----------------------EnumProcess函數
舉例代碼:
#include
#include
#include
#pragma comment(lib,"psapi.lib");
BOOL UpdateProcessPrivilege(HANDLE hProcess,LPCTSTR lpPrivilegeName = SE_DEBUG_NAME);
void main()
{
UpdateProcessPrivilege(GetCurrentProcess());
DWORD processcount;
DWORD cbNeeded;
DWORD ProcessId[1024];
EnumProcesses(ProcessId, sizeof(ProcessId), &cbNeeded);
processcount=cbNeeded/sizeof(DWORD);
HMODULE hModule;
char szPath[MAX_PATH];
for (DWORD i=0;i {
//打開進程
HANDLE hProcess=OpenProcess(
PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,
FALSE, ProcessId[i]);
memset(szPath,0,sizeof(szPath));
if (hProcess)
{
EnumProcessModules(hProcess,
&hModule, sizeof(hModule), &cbNeeded);
GetModuleFileNameExA(hProcess,
hModule, szPath, sizeof(szPath));
printf("ProcessID: %d (%s)
",ProcessId[i],szPath);
}
else
printf("Failed!!!
");
CloseHandle(hProcess);
}
getchar(); // 暫停.
}
BOOL UpdateProcessPrivilege( HANDLE hProcess, LPCTSTR lpPrivilegeName)
{
HANDLE hToken;
int iResult;
TOKEN_PRIVILEGES TokenPrivileges;
if (OpenProcessToken( hProcess, TOKEN_ALL_ACCESS, &hToken ) )
{
LUID destLuid;
if (LookupPrivilegeValue( NULL, lpPrivilegeName, &destLuid ) )
{
TokenPrivileges.PrivilegeCount = 1;
TokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
TokenPrivileges.Privileges[0].Luid = destLuid;
if ( iResult =AdjustTokenPrivileges( hToken, FALSE,
&TokenPrivileges, 0, NULL, NULL )) {
return TRUE;
}
}
}
return FALSE;
}
2.進程關閉
ExitProcess
TerminateProcess
3.枚舉進程模塊
代碼示例:
int Modlist(DWORD Pid)
{
HANDLE SnapP;
struct tagMODULEENTRY32 modsnap;
DebugPrivilege(SE_DEBUG_NAME,TRUE);
SnapP = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,Pid);
if(SnapP == (HANDLE)-1)
{
sprintf(Temp,"Fail To CreateToolhelp32Snapshot
");
SendMessage(Socket,Temp);
return 1;
}
modsnap.dwSize = sizeof(tagMODULEENTRY32);
if(Module32First(SnapP,&modsnap))
{
sprintf(Temp,"The Process[%d] Module Infomation:
ModuleName ModulePath
",Pid);
strcat(Temp,"-------------------------------------------------------------------------------
");
printf("%s",Temp);
do
{
sprintf(Temp,"%-21s%s
",modsnap.szModule,modsnap.szExePath);
printf("%s",Temp);
}
while(Module32Next(SnapP,&modsnap));
sprintf(Temp,"
List Process Module Compeleted
");
}
else
sprintf(Temp,"Fail To Process32First
");
printf("%s",Temp);
DebugPrivilege(SE_DEBUG_NAME,FALSE);
CloseHandle(SnapP);
return 0;
}