將處理程序放入一個工作內,以限制此處理程序具體能夠做哪些事情

Post date: 2012/9/19 上午 06:41:33

#include <iostream> #include <Windows.h> #include <tchar.h> #include <Strsafe.h> int main() { BOOL bInJob = FALSE; IsProcessInJob(GetCurrentProcess(),NULL,&bInJob); if(bInJob) { std::cout<<"Process already in a job"<<std::endl; ; } HANDLE hjob = CreateJobObject(NULL,TEXT("Wintellect_RestrictedProcessJob")); JOBOBJECT_BASIC_LIMIT_INFORMATION jobli = { 0 }; jobli.PriorityClass = IDLE_PRIORITY_CLASS; jobli.PerJobUserTimeLimit.QuadPart = 10000; jobli.LimitFlags = JOB_OBJECT_LIMIT_PRIORITY_CLASS | JOB_OBJECT_LIMIT_JOB_TIME; SetInformationJobObject(hjob, JobObjectBasicLimitInformation,&jobli,sizeof(jobli)); JOBOBJECT_BASIC_UI_RESTRICTIONS jobuir; jobuir.UIRestrictionsClass = JOB_OBJECT_UILIMIT_NONE; jobuir.UIRestrictionsClass |= JOB_OBJECT_UILIMIT_EXITWINDOWS; jobuir.UIRestrictionsClass |= JOB_OBJECT_UILIMIT_HANDLES; SetInformationJobObject(hjob,JobObjectBasicUIRestrictions,&jobuir,sizeof(jobuir)); STARTUPINFO si = {sizeof(si) }; PROCESS_INFORMATION pi; TCHAR szCmdLine[8]; _tcscpy_s(szCmdLine, _countof(szCmdLine),TEXT("CMD")); BOOL bResult = CreateProcess( NULL,szCmdLine,NULL,NULL,FALSE,CREATE_SUSPENDED | CREATE_NEW_CONSOLE, NULL, NULL, &si,&pi); AssignProcessToJobObject(hjob,pi.hProcess); ResumeThread(pi.hThread); CloseHandle(pi.hThread); HANDLE h[2]; h[0] = pi.hProcess; h[1] = hjob; DWORD dw = WaitForMultipleObjects(2,h,FALSE,INFINITE); switch (dw - WAIT_OBJECT_0){ case 0: break; case 1: break; } FILETIME CreationTime; FILETIME ExitTime; FILETIME KernelTime; FILETIME UserTime; TCHAR szInfo[MAX_PATH]; GetProcessTimes(pi.hProcess,&CreationTime,&ExitTime,&KernelTime,&UserTime); StringCchPrintf(szInfo, _countof(szInfo),TEXT("Kernel = %u | User = %u\n"), KernelTime.dwLowDateTime / 10000, UserTime.dwLowDateTime / 10000); MessageBox(GetActiveWindow(),szInfo,TEXT("Restricted process times"),MB_ICONINFORMATION | MB_OK); CloseHandle(pi.hProcess); CloseHandle(hjob); system("pause"); return 0; }