作业问题,附上全部源代码,高手请指教

universee 2006-09-07 02:17:31

#include <windows.h>
//#include <windowsx.h>
#include <process.h> // for _beginthreadex
#include <tchar.h>
#include <stdio.h>

void main()
{
//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);
}


这些函数都找不到,为什么?明明都是在windows中的呀
/------------------

(10) : error C2065: 'CreateJobObject' : undeclared identifier
(10) : 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
(27) : error C2065: 'SetInformationJobObject' : undeclared identifier
(59) : error C2065: 'AssignProcessToJobObject' : undeclared identifier
(91) : error C2373: 'AssignProcessToJobObject' : redefinition; different type modifiers
(96) : error C2373: 'CreateJobObject' : redefinition; different type modifiers
(103) : error C2373: 'SetInformationJobObject' : redefinition; different type modifiers
...全文
234 10 打赏 收藏 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
universee 2006-09-11
  • 打赏
  • 举报
回复
连接问题解决了
hhyytt 2006-09-11
  • 打赏
  • 举报
回复
库文件没有加上。
universee 2006-09-08
  • 打赏
  • 举报
回复
连接问题解决了,但是执行却仍然有问题
universee 2006-09-08
  • 打赏
  • 举报
回复
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <stdio.h>

#define MEMORY_REQUESTED 1024*1024 // request a megabyte

BOOL
LoggedSetLockPagesPrivilege ( HANDLE hProcess,
BOOL bEnable);

void _cdecl main()
{
BOOL bResult; // generic Boolean value
ULONG_PTR NumberOfPages; // number of pages to request
ULONG_PTR NumberOfPagesInitial; // initial number of pages requested
ULONG_PTR *aPFNs; // page info; holds opaque data
PVOID lpMemReserved; // AWE window
SYSTEM_INFO sSysInfo; // useful system information
int PFNArraySize; // memory to request for PFN array

GetSystemInfo(&sSysInfo); // fill the system information structure

printf("This computer has page size %d.\n", sSysInfo.dwPageSize);

// Calculate the number of pages of memory to request.

NumberOfPages = MEMORY_REQUESTED/sSysInfo.dwPageSize;
printf ("Requesting %d pages of memory.\n", NumberOfPages);

// Calculate the size of the user PFN array.

PFNArraySize = NumberOfPages * sizeof (ULONG_PTR);

printf ("Requesting a PFN array of %d bytes.\n", PFNArraySize);

aPFNs = (ULONG_PTR *) HeapAlloc(GetProcessHeap(), 0, PFNArraySize);

if (aPFNs == NULL)
{
printf ("Failed to allocate on heap.\n");
return;
}

// Enable the privilege.

if( ! LoggedSetLockPagesPrivilege( GetCurrentProcess(), TRUE ) )
{
return;
}

// Allocate the physical memory.

NumberOfPagesInitial = NumberOfPages;
bResult = AllocateUserPhysicalPages( GetCurrentProcess(),
&NumberOfPages,
aPFNs );

if( bResult != TRUE )
{
printf("Cannot allocate physical pages (%u)\n", GetLastError() );
return;
}

if( NumberOfPagesInitial != NumberOfPages )
{
printf("Allocated only %p pages.\n", NumberOfPages );
return;
}

// Reserve the virtual memory.

lpMemReserved = VirtualAlloc( NULL,
MEMORY_REQUESTED,
MEM_RESERVE | MEM_PHYSICAL,
PAGE_READWRITE );

if( lpMemReserved == NULL )
{
printf("Cannot reserve memory.\n");
return;
}

// Map the physical memory into the window.

bResult = MapUserPhysicalPages( lpMemReserved,
NumberOfPages,
aPFNs );

if( bResult != TRUE )
{
printf("MapUserPhysicalPages failed (%u)\n", GetLastError() );
return;
}

// unmap

bResult = MapUserPhysicalPages( lpMemReserved,
NumberOfPages,
NULL );

if( bResult != TRUE )
{
printf("MapUserPhysicalPages failed (%u)\n", GetLastError() );
return;
}

// Free the physical pages.

bResult = FreeUserPhysicalPages( GetCurrentProcess(),
&NumberOfPages,
aPFNs );

if( bResult != TRUE )
{
printf("Cannot free physical pages, error %u.\n", GetLastError());
return;
}

// Free virtual memory.

bResult = VirtualFree( lpMemReserved,
0,
MEM_RELEASE );

// Release the aPFNs array.

bResult = HeapFree(GetProcessHeap(), 0, aPFNs);

if( bResult != TRUE )
{
printf("Call to HeapFree has failed (%u)\n", GetLastError() );
}

}

/*****************************************************************
LoggedSetLockPagesPrivilege: a function to obtain or
release the privilege of locking physical pages.

Inputs:

HANDLE hProcess: Handle for the process for which the
privilege is needed

BOOL bEnable: Enable (TRUE) or disable?

Return value: TRUE indicates success, FALSE failure.

*****************************************************************/
BOOL
LoggedSetLockPagesPrivilege ( HANDLE hProcess,
BOOL bEnable)
{
struct {
DWORD Count;
LUID_AND_ATTRIBUTES Privilege [1];
} Info;

HANDLE Token;
BOOL Result;

// Open the token.

Result = OpenProcessToken ( hProcess,
TOKEN_ADJUST_PRIVILEGES,
& Token);

if( Result != TRUE )
{
printf( "Cannot open process token.\n" );
return FALSE;
}

// Enable or disable?

Info.Count = 1;
if( bEnable )
{
Info.Privilege[0].Attributes = SE_PRIVILEGE_ENABLED;
}
else
{
Info.Privilege[0].Attributes = 0;
}

// Get the LUID.

Result = LookupPrivilegeValue ( NULL,
SE_LOCK_MEMORY_NAME,
&(Info.Privilege[0].Luid));

if( Result != TRUE )
{
printf( "Cannot get privilege for %s.\n", SE_LOCK_MEMORY_NAME );
return FALSE;
}

// Adjust the privilege.

Result = AdjustTokenPrivileges ( Token, FALSE,
(PTOKEN_PRIVILEGES) &Info,
0, NULL, NULL);

// Check the result.

if( Result != TRUE )
{
printf ("Cannot adjust token privileges (%u)\n", GetLastError() );
return FALSE;
}
else
{
if( GetLastError() != ERROR_SUCCESS )
{
printf ("Cannot enable the SE_LOCK_MEMORY_NAME privilege; ");
printf ("please check the local policy.\n");
return FALSE;
}
}

CloseHandle( Token );

return TRUE;
}


怪了,这个加上也不行,说
Linking...
test3.obj : error LNK2001: unresolved external symbol __imp__FreeUserPhysicalPages@12
test3.obj : error LNK2001: unresolved external symbol __imp__MapUserPhysicalPages@12
test3.obj : error LNK2001: unresolved external symbol __imp__AllocateUserPhysicalPages@12
Debug/test3.exe : fatal error LNK1120: 3 unresolved externals
Error executing link.exe.
universee 2006-09-07
  • 打赏
  • 举报
回复
原来是这样,谢谢,停会结贴
yishow 2006-09-07
  • 打赏
  • 举报
回复
楼上说的没错
加上#define _WIN32_WINNT 0x0500在所有包含头文件之前就可以编译通过了
GoAround 2006-09-07
  • 打赏
  • 举报
回复
define _WIN32_WINNT as 0x0500 or later
universee 2006-09-07
  • 打赏
  • 举报
回复
编译一下这个程序好吗?看是否我的VC6装的有问题
universee 2006-09-07
  • 打赏
  • 举报
回复
头文件都包含着呢
Darter 2006-09-07
  • 打赏
  • 举报
回复
QuickInfo
Windows NT: Requires version 5.0 or later.
Windows: Unsupported.
Windows CE: Unsupported.
Header: Declared in winbase.h.
Import Library: Use kernel32.lib.

16,550

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧