程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C編寫進程管理器

C編寫進程管理器

編輯:關於C語言

舉例代碼:

#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;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved