HANDLE WINAPI CreateJob()
{
BOOL IsInJob = FALSE; DWORD ErrorCode = NULL;
IsProcessInJob(GetCurrentProcess(), NULL, &IsInJob);
if (IsInJob == TRUE)
{
ErrorCode = GetLastError();
return (HANDLE)ErrorCode;
}
HANDLE MyJob = CreateJobObject(NULL, TEXT("MyProcessJob"));
JOBOBJECT_BASIC_LIMIT_INFORMATION BasicLimit = { 0 };
BasicLimit.LimitFlags = JOB_OBJECT_LIMIT_JOB_TIME | JOB_OBJECT_LIMIT_PRIORITY_CLASS;
BasicLimit.PerJobUserTimeLimit.QuadPart = 10000; BasicLimit.PriorityClass = IDLE_PRIORITY_CLASS;
SetInformationJobObject(MyJob, JobObjectBasicLimitInformation, &BasicLimit, sizeof(BasicLimit));
JOBOBJECT_BASIC_UI_RESTRICTIONS UIBasicLimit = { 0 };
UIBasicLimit.UIRestrictionsClass = JOB_OBJECT_UILIMIT_DISPLAYSETTINGS | JOB_OBJECT_UILIMIT_EXITWINDOWS;
SetInformationJobObject(MyJob, JobObjectBasicUIRestrictions, &UIBasicLimit, sizeof(UIBasicLimit));
return MyJob;
}
#include <Windows.h>
#include <iostream>
#include <strsafe.h>
#include <tchar.h>
using namespace std;
HANDLE WINAPI CreateJob();
DWORD WINAPI ProcessTask();
VOID WINAPI ErrorCodeTransformation(DWORD ErrorCode);
int main(int argc, char *argv[])
{
ProcessTask();
return 0;
}
DWORD WINAPI ProcessTask()
{
HANDLE MyJob = CreateJob();
TCHAR CmdLine[8] = L"CMD.EXE"; STARTUPINFO StartInfo = { sizeof(StartInfo) }; PROCESS_INFORMATION ProcessInfo = { 0 };
CreateProcess(NULL, CmdLine, NULL, NULL, FALSE, CREATE_NEW_CONSOLE | CREATE_SUSPENDED, NULL, NULL, &StartInfo, &ProcessInfo);
AssignProcessToJobObject(MyJob, ProcessInfo.hProcess);
ResumeThread(ProcessInfo.hThread); CloseHandle(ProcessInfo.hThread);
CloseHandle(MyJob); CloseHandle(ProcessInfo.hProcess);
return TRUE;
}
#include <Windows.h>
#include <iostream>
#include <strsafe.h>
#include <tchar.h>
using namespace std;
HANDLE WINAPI CreateJob();
HANDLE WINAPI ProcessTask();
VOID WINAPI ErrorCodeTransformation(DWORD ErrorCode);
int main(int argc, char *argv[])
{
DWORD ErrorCode = NULL;
HANDLE MyJob = ProcessTask();
JOBOBJECT_BASIC_ACCOUNTING_INFORMATION BasicAccountInfo = { 0 }; DWORD ResSize = NULL;
if (!QueryInformationJobObject(MyJob, JobObjectBasicAccountingInformation, &BasicAccountInfo, sizeof(JOBOBJECT_BASIC_ACCOUNTING_INFORMATION), &ResSize))
{
ErrorCode = GetLastError();
ErrorCodeTransformation(ErrorCode);
return 0;
}
cout << "
- 作业内进程的基本信息 -
" << endl;
cout << "[*] 作业关联的所有活动进程的用户模式执行时间总量: " << BasicAccountInfo.TotalUserTime.LowPart / 10000 << endl;
cout << "[*] 作业关联的所有活动进程的内核模式执行时间总量: " << BasicAccountInfo.TotalKernelTime.LowPart / 10000 << endl;
cout << "[*] 作业关联的所有进程的用户模式执行时间总量: " << BasicAccountInfo.ThisPeriodTotalUserTime.LowPart / 10000 << endl;
cout << "[*] 作业关联的所有进程的内核模式执行时间总量: " << BasicAccountInfo.ThisPeriodTotalKernelTime.LowPart / 10000 << endl;
cout << "[*] 作业关联的所有活动进程遇到的页面错误总数: " << BasicAccountInfo.TotalPageFaultCount << endl;
cout << "[*] 作业在其生命周期内关联的进程总数: " << BasicAccountInfo.TotalProcesses << endl;
cout << "[*] 当前与作业关联的进程总数: " << BasicAccountInfo.ActiveProcesses << endl;
cout << "[*] 由于违反限制而终止的进程总数: " << BasicAccountInfo.TotalTerminatedProcesses << endl;
JOBOBJECT_BASIC_AND_IO_ACCOUNTING_INFORMATION IOBasicInfo = { 0 }; ResSize = NULL;
if (!QueryInformationJobObject(MyJob, JobObjectBasicAndIoAccountingInformation, &IOBasicInfo, sizeof(JOBOBJECT_BASIC_AND_IO_ACCOUNTING_INFORMATION), &ResSize))
{
ErrorCode = GetLastError();
ErrorCodeTransformation(ErrorCode);
return 0;
}
cout << "
- 作业内进程 IO 信息 -
" << endl;
cout << "[*] 由作业中的进程读取 IO 的次数: " << IOBasicInfo.IoInfo.ReadOperationCount << endl;
cout << "[*] 由作业中的进程未读取 IO 的次数: " << IOBasicInfo.IoInfo.ReadTransferCount << endl;
cout << "[*] 由作业中的进程写入 IO 的次数: " << IOBasicInfo.IoInfo.WriteOperationCount << endl;
cout << "[*] 由作业中的进程未写入 IO 的次数: " << IOBasicInfo.IoInfo.WriteTransferCount << endl;
#define MAX_PROCESS_IDS 10
ResSize = sizeof(JOBOBJECT_BASIC_PROCESS_ID_LIST) + (MAX_PROCESS_IDS - 1) * sizeof(DWORD);
PJOBOBJECT_BASIC_PROCESS_ID_LIST ProcessID = (PJOBOBJECT_BASIC_PROCESS_ID_LIST)_alloca(ResSize);
ProcessID->NumberOfAssignedProcesses = MAX_PROCESS_IDS;
if (!QueryInformationJobObject(MyJob, JobObjectBasicProcessIdList, ProcessID, ResSize, &ResSize))
{
ErrorCode = GetLastError();
ErrorCodeTransformation(ErrorCode);
return 0;
}
cout << "
- 作业内进程列表 -
" << endl;
for (DWORD i = 0; i < ProcessID->NumberOfProcessIdsInList; i++)
{
cout << "[*] 作业中第 " << i << " 个进程 ID: " << ProcessID->ProcessIdList[i] << endl;
}
cout << "
- 父进程 -
" << endl;
cout << "[*] 父进程 ID: " << GetProcessId(GetCurrentProcess()) << endl;
CloseHandle(MyJob);
return 0;
}
HANDLE WINAPI ProcessTask()
{
HANDLE MyJob = CreateJob();
TCHAR CmdLine[8] = L"CMD.EXE"; STARTUPINFO StartInfo = { sizeof(StartInfo) }; PROCESS_INFORMATION ProcessInfo = { 0 };
CreateProcess(NULL, CmdLine, NULL, NULL, FALSE, CREATE_NEW_CONSOLE | CREATE_SUSPENDED, NULL, NULL, &StartInfo, &ProcessInfo);
AssignProcessToJobObject(MyJob, ProcessInfo.hProcess);
ResumeThread(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
return MyJob;
}
HANDLE WINAPI CreateJob()
{
BOOL IsInJob = FALSE; DWORD ErrorCode = NULL;
IsProcessInJob(GetCurrentProcess(), NULL, &IsInJob);
if (IsInJob == TRUE)
{
ErrorCode = GetLastError();
return (HANDLE)ErrorCode;
}
HANDLE MyJob = CreateJobObject(NULL, TEXT("MyProcessJob"));
JOBOBJECT_BASIC_LIMIT_INFORMATION BasicLimit = { 0 };
BasicLimit.LimitFlags = JOB_OBJECT_LIMIT_JOB_TIME | JOB_OBJECT_LIMIT_PRIORITY_CLASS;
BasicLimit.PerJobUserTimeLimit.QuadPart = 10000; BasicLimit.PriorityClass = IDLE_PRIORITY_CLASS;
SetInformationJobObject(MyJob, JobObjectBasicLimitInformation, &BasicLimit, sizeof(BasicLimit));
JOBOBJECT_BASIC_UI_RESTRICTIONS UIBasicLimit = { 0 };
UIBasicLimit.UIRestrictionsClass = JOB_OBJECT_UILIMIT_DISPLAYSETTINGS | JOB_OBJECT_UILIMIT_EXITWINDOWS;
SetInformationJobObject(MyJob, JobObjectBasicUIRestrictions, &UIBasicLimit, sizeof(UIBasicLimit));
return MyJob;
}
VOID WINAPI ErrorCodeTransformation(DWORD ErrorCode)
{
LPVOID lpMsgBuf; LPVOID lpDisplayBuf; DWORD dw = ErrorCode;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL
);
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, (lstrlen((LPCTSTR)lpMsgBuf) + 40) * sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf, LocalSize(lpDisplayBuf), TEXT("错误代码 %d : %s"), dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
LocalFree(lpMsgBuf); LocalFree(lpDisplayBuf); ExitProcess(dw);
}