/*
Job,just like a container of processes,it means to restrict the action of
the processes in it,such as:the CPU time of the processes occupy,prevent
the computer from be shutdown.HANDLE CreateJobObject( PSECURITY_ATTRIBUTES psa,//
PCTSTR pszName);//the name of the job.
*/
#include<Windows.h>
#include<stdio.h>int main()
{
void StartRestrictedProcess(void); StartRestrictedProcess();
return 0;
}void StartRestrictedProcess(void)
{
//Create a job kernel object.
HANDLE hjob = CreateJobObject(NULL,NULL); //Place some restrictions on processes in the job.
//First,set some basic restrictions.
JOBOBJECT_BASIC_LIMIT_INFORMATION jobli = { 0 }; //The process always runs in the idle priority class.
jobli.PriorityClass = IDLE_PRIORITY_CLASS; //The job cannot use more than 1 second of CPU time.
jobli.PerJobUserTimeLimit.QuadPart = 10000000;
//1 sec in 100-ns intervals //These are the only 2 restrictions I want placed on the job (process).
jobli.LimitFlags = JOB_OBJECT_LIMIT_PRIORITY_CLASS |
JOB_OBJECT_LIMIT_JOB_TIME;
SetInformationJobObject(hjob,
JobObjectBasicLimitInformation,
&jobli, sizeof(jobli)); //Second, set some UI restrictions.
JOBOBJECT_BASIC_UI_RESTRICTIONS jobuir;
jobuir.UIRestrictionsClass = JOB_OBJECT_UILIMIT_NONE;
//A fancy zero //The process can't log off the system.
jobuir.UIRestrictionsClass |= JOB_OBJECT_UILIMIT_EXITWINDOWS; //The process can't access USER objects
//(such as other windows) in the system.
jobuir.UIRestrictionsClass |= JOB_OBJECT_UILIMIT_HANDLES; SetInformationJobObject(hjob,JobObjectBasicUIRestrictions,
&jobuir, sizeof(jobuir)); //Spawn the process that is to be in the job.
//Note: You must first spawn the process and then place the process in
//the job. This means that the process's thread must be initially
//suspended so that it can't execute any code outside
//of the job's restrictions. STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
CreateProcess(NULL, "CMD", NULL, NULL, FALSE,
CREATE_SUSPENDED, NULL, NULL, &si, &pi);
//Place the process in the job.
//Note:if this process spawns any children,the children are
//automatically part of the same job.
AssignProcessToJobObject(hjob,pi.hProcess); //Now we can allow the child process's thread to execute code.
ResumeThread(pi.hThread);
CloseHandle(pi.hThread); //Wait for the process to terminate or for all the job's
//allotted CPU time to be used.
HANDLE h[2];
h[0] = pi.hProcess;
h[1] = hjob;
DWORD dw = WaitForMultipleObjects(2,h,FALSE,INFINITE);
switch( dw-WAIT_OBJECT_0 )
{
case 0:
//The process has terminated...
break;
case 1:
//All of the job's allotted CPU time was used...
break;
} //Clean up properly.
CloseHandle(pi.hProcess);
CloseHandle(hjob);
}————————————————
编译错误:
--------------------Configuration: Restrict_Job - Win32 Debug--------------------
Compiling...
test.cpp
G:\my_pro\Restrict_Job\test.cpp(23) : error C2065: 'CreateJobObject' : undeclared identifier
G:\my_pro\Restrict_Job\test.cpp(23) : error C2440: 'initializing' : cannot convert from 'int' to 'void *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
G:\my_pro\Restrict_Job\test.cpp(40) : error C2065: 'SetInformationJobObject' : undeclared identifier
G:\my_pro\Restrict_Job\test.cpp(68) : error C2664: 'CreateProcessA' : cannot convert parameter 10 from 'struct _PROCESS_INFORMATION' to 'struct _PROCESS_INFORMATION *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
G:\my_pro\Restrict_Job\test.cpp(72) : error C2065: 'AssignProcessToJobObject' : undeclared identifier
执行 cl.exe 时出错.test.obj - 1 error(s), 0 warning(s)
Job,just like a container of processes,it means to restrict the action of
the processes in it,such as:the CPU time of the processes occupy,prevent
the computer from be shutdown.HANDLE CreateJobObject( PSECURITY_ATTRIBUTES psa,//
PCTSTR pszName);//the name of the job.
*/
#include<Windows.h>
#include<stdio.h>int main()
{
void StartRestrictedProcess(void); StartRestrictedProcess();
return 0;
}void StartRestrictedProcess(void)
{
//Create a job kernel object.
HANDLE hjob = CreateJobObject(NULL,NULL); //Place some restrictions on processes in the job.
//First,set some basic restrictions.
JOBOBJECT_BASIC_LIMIT_INFORMATION jobli = { 0 }; //The process always runs in the idle priority class.
jobli.PriorityClass = IDLE_PRIORITY_CLASS; //The job cannot use more than 1 second of CPU time.
jobli.PerJobUserTimeLimit.QuadPart = 10000000;
//1 sec in 100-ns intervals //These are the only 2 restrictions I want placed on the job (process).
jobli.LimitFlags = JOB_OBJECT_LIMIT_PRIORITY_CLASS |
JOB_OBJECT_LIMIT_JOB_TIME;
SetInformationJobObject(hjob,
JobObjectBasicLimitInformation,
&jobli, sizeof(jobli)); //Second, set some UI restrictions.
JOBOBJECT_BASIC_UI_RESTRICTIONS jobuir;
jobuir.UIRestrictionsClass = JOB_OBJECT_UILIMIT_NONE;
//A fancy zero //The process can't log off the system.
jobuir.UIRestrictionsClass |= JOB_OBJECT_UILIMIT_EXITWINDOWS; //The process can't access USER objects
//(such as other windows) in the system.
jobuir.UIRestrictionsClass |= JOB_OBJECT_UILIMIT_HANDLES; SetInformationJobObject(hjob,JobObjectBasicUIRestrictions,
&jobuir, sizeof(jobuir)); //Spawn the process that is to be in the job.
//Note: You must first spawn the process and then place the process in
//the job. This means that the process's thread must be initially
//suspended so that it can't execute any code outside
//of the job's restrictions. STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
CreateProcess(NULL, "CMD", NULL, NULL, FALSE,
CREATE_SUSPENDED, NULL, NULL, &si, &pi);
//Place the process in the job.
//Note:if this process spawns any children,the children are
//automatically part of the same job.
AssignProcessToJobObject(hjob,pi.hProcess); //Now we can allow the child process's thread to execute code.
ResumeThread(pi.hThread);
CloseHandle(pi.hThread); //Wait for the process to terminate or for all the job's
//allotted CPU time to be used.
HANDLE h[2];
h[0] = pi.hProcess;
h[1] = hjob;
DWORD dw = WaitForMultipleObjects(2,h,FALSE,INFINITE);
switch( dw-WAIT_OBJECT_0 )
{
case 0:
//The process has terminated...
break;
case 1:
//All of the job's allotted CPU time was used...
break;
} //Clean up properly.
CloseHandle(pi.hProcess);
CloseHandle(hjob);
}————————————————
编译错误:
--------------------Configuration: Restrict_Job - Win32 Debug--------------------
Compiling...
test.cpp
G:\my_pro\Restrict_Job\test.cpp(23) : error C2065: 'CreateJobObject' : undeclared identifier
G:\my_pro\Restrict_Job\test.cpp(23) : error C2440: 'initializing' : cannot convert from 'int' to 'void *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
G:\my_pro\Restrict_Job\test.cpp(40) : error C2065: 'SetInformationJobObject' : undeclared identifier
G:\my_pro\Restrict_Job\test.cpp(68) : error C2664: 'CreateProcessA' : cannot convert parameter 10 from 'struct _PROCESS_INFORMATION' to 'struct _PROCESS_INFORMATION *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
G:\my_pro\Restrict_Job\test.cpp(72) : error C2065: 'AssignProcessToJobObject' : undeclared identifier
执行 cl.exe 时出错.test.obj - 1 error(s), 0 warning(s)
解决方案 »
- xp下可以实现windows7的透明效果吗?
- 求助:VC win32 applcation 异常处理,谢谢!
- 如何把一个多页tif文件,分成多个文件的一个图像的tif
- 如何让listbox接收VK_DOWN与VK_UP消息??
- UDP服务器做广播的时候,必须指定客户端的端口号吗?
- 【散分】申请大版主成功了,感谢大家支持!!!接分啊
- win7和xp下同一个vs2010重建三维图像项目,xp能显示win7不能
- 大家帮帮我呀!!!急救!!请大家一定要帮我呀!
- 大比例缩小图片时,应该如何取样
- 分割视图里禁止手动改变每个子视图的大小?
- 怎样获得CImageList中ICON的原始大小
- listview内容多的时候 显示很慢 怎么解决?
Client: Included in Windows XP and Windows 2000 Professional.
Server: Included in Windows Server 2003 and Windows 2000 Server.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.
需要设置一下
// 如果您必须使用下列所指定的平台之前的平台,则修改下面的定义。
// 有关不同平台的相应值的最新信息,请参考 MSDN。
#ifndef WINVER // 允许使用 Windows 95 和 Windows NT 4 或更高版本的特定功能。
#define WINVER 0x0500 //为 Windows98 和 Windows 2000 及更新版本改变为适当的值。
#endif#ifndef _WIN32_WINNT // 允许使用 Windows NT 4 或更高版本的特定功能。
#define _WIN32_WINNT 0x0500 //为 Windows98 和 Windows 2000 及更新版本改变为适当的值。
#endif #ifndef _WIN32_WINDOWS // 允许使用 Windows 98 或更高版本的特定功能。
#define _WIN32_WINDOWS 0x0410 //为 Windows Me 及更新版本改变为适当的值。
#endif#ifndef _WIN32_IE // 允许使用 IE 4.0 或更高版本的特定功能。
#define _WIN32_IE 0x0500 //为 IE 5.0 及更新版本改变为适当的值。
#endif
但小弟仍有几点想请教,请大侠不惜赐教:
JOBOBJECT_BASIC_LIMIT_INFORMATION jobli = { 0 };
是否是为,结构体jobli赋初值为全0,或是将第一个成员赋值为0?
STARTUPINFO si = { sizeof(si) };
是将结构体si的cb成员设置为其结构体的大小不?
是将结构体si的cb成员设置为其结构体的大小