我有以下代码:
#include <windows.h>
#include <stdio.h>
#include <shlwapi.h>
#pragma comment ( lib, "shlwapi.lib" )
int __cdecl wmain( int argc, PWSTR argv[] )
{
HANDLE Job( CreateJobObject( NULL, NULL ) );
if( !Job )
{
wprintf( L"Could not create job object, error %d\n", GetLastError() );
return 0;
}
HANDLE IOPort( CreateIoCompletionPort( INVALID_HANDLE_VALUE, NULL, 0, 1 ) );
if( !IOPort )
{
wprintf( L"Could not create IO completion port, error %d\n", GetLastError() );
return 0;
}
JOBOBJECT_ASSOCIATE_COMPLETION_PORT Port;
Port.CompletionKey = Job;
Port.CompletionPort = IOPort;
if( !SetInformationJobObject( Job, JobObjectAssociateCompletionPortInformation, &Port, sizeof( Port ) ) )
{
wprintf( L"Could not associate job with IO completion port, error %d\n", GetLastError() );
return 0;
}
PROCESS_INFORMATION ProcessInformation;
STARTUPINFO StartupInfo = { sizeof(StartupInfo) };
PWSTR CommandLine = PathGetArgs(GetCommandLine());
if( !CreateProcess( NULL, CommandLine, NULL, NULL, FALSE, CREATE_NEW_CONSOLE | CREATE_SUSPENDED, NULL, NULL, &StartupInfo, &ProcessInformation ) )
{
wprintf( L"Could not run process, error %d\n", GetLastError() );
return 0;
}
if( !AssignProcessToJobObject( Job, ProcessInformation.hProcess ) )
{
wprintf( L"Could not assign process to job, error %d\n", GetLastError() );
return 0;
}
ResumeThread( ProcessInformation.hThread );
CloseHandle( ProcessInformation.hThread );
CloseHandle( ProcessInformation.hProcess );
DWORD CompletionCode;
ULONG_PTR CompletionKey;
LPOVERLAPPED Overlapped;
int ProcessCount = 0;
while ( GetQueuedCompletionStatus( IOPort, &CompletionCode, &CompletionKey, &Overlapped, INFINITE ) && CompletionCode != JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO )
{
if ( CompletionCode == JOB_OBJECT_MSG_NEW_PROCESS ) ProcessCount++;
if ( ( CompletionCode == JOB_OBJECT_MSG_EXIT_PROCESS ) || ( CompletionCode == JOB_OBJECT_MSG_ABNORMAL_EXIT_PROCESS) ) ProcessCount--;
wprintf( L"Waiting for %d processes to finish...\n", ProcessCount );
}
wprintf( L"All done\n" );
return 0;
}
此代码在 Windows 7 上运行良好,但在 Windows XP 上,AssignProcessToJobObject 失败并出现错误代码 5 (Access Denied)。根据 MSDN:Windows 7、Windows Server 2008 R2、带有 SP3 的 Windows XP、Windows Server 2008、Windows Vista 和 Windows Server 2003:进程必须尚未分配给作业;如果是,则函数失败并显示 ERROR_ACCESS_DENIED。从 Windows 8 和 Windows Server 2012 开始,此行为发生了变化。
有人可以帮我更正这段代码吗?
谢谢!
更新: 我能够找到问题,但我仍然不知道如何解决它:(问题是,我使用标准用户(没有管理员权限)登录 XP 机器,然后打开一个 cmd使用runas(使用具有管理员权限的用户),那么这个cmd将被创建为一个jobobject。在进程资源管理器中你可以看到这个。如果我想从这个cmd启动我的应用程序,那么AssignProcessToJobObject将失败并出现错误访问拒绝,因为thios cmd 已分配给作业。
有没有办法解决我的问题?