简体   繁体   English

如何将当前进程分配给新创建的作业对象?

[英]How can I assign the current process to a newly created job object?

I can't seem to use the AssignProcessToJobObject function to assign the current process to a job object handle given by CreateJobObject . 我似乎无法使用AssignProcessToJobObject函数将当前进程分配给CreateJobObject给出的作业对象句柄。 This has been asked a few times already on StackOverflow, but so far none of the solutions (which usually boil down to embedding an UAC manifest) seem to work for me. 这已经在StackOverflow上已经被问了几次,但到目前为止,没有一个解决方案(通常归结为嵌入UAC清单)似乎对我有用。

I'm using MSVC9 on Windows 7 for this. 我在Windows 7上使用MSVC9。 Here's the source code for my sample application and a small manifest I'm embedding (which supposedly fixes the problem - but not for me): 这是我的示例应用程序的源代码和我嵌入的一个小清单(据说可以修复问题 - 但不适合我):

My sample application ( main.cpp ): 我的示例应用程序( main.cpp ):

#include <windows.h>

static void dumpLastError()
{
    LPVOID lpMsgBuf;
    DWORD dw = GetLastError();

    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_FROM_SYSTEM,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0, NULL );
    OutputDebugStringA( (LPTSTR)lpMsgBuf );

    LocalFree(lpMsgBuf);
}

int main()
{
    HANDLE job = CreateJobObjectA( NULL, "demo job 123" );
    if ( !job ) {
        OutputDebugStringA( "CreateJobObject failed" );
        dumpLastError();
        return 1;
    }

    if ( !AssignProcessToJobObject( job, GetCurrentProcess() ) ) {
        OutputDebugStringA( "AssignProcessToJobObject failed" );
        dumpLastError();
        return 1;
    }

    return 0;
}

The UAC manifest ( main.exe.manifest ): UAC清单( main.exe.manifest ):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
   <ms_asmv3:trustInfo xmlns:ms_asmv3="urn:schemas-microsoft-com:asm.v3">
      <ms_asmv3:security>
         <ms_asmv3:requestedPrivileges>
            <ms_asmv3:requestedExecutionLevel level="requireAdministrator"/>
         </ms_asmv3:requestedPrivileges>
      </ms_asmv3:security>
   </ms_asmv3:trustInfo>
</assembly>

I build this sample by running 我通过运行构建此示例

cl main.cpp
mt -manifest main.exe.manifest -outputresource:main.exe;1

Unfortunately, running my main.exe sample after these steps still yields an 'Access denied' error in debug output when attempting the AssignProcessToJobObject call. 不幸的是,在尝试AssignProcessToJobObject调用时,在AssignProcessToJobObject这些步骤后运行我的main.exe示例仍会在调试输出中产生“拒绝访问”错误。 Does anybody know why that is? 有谁知道那是为什么?

I know this is an old question, but I was recently having the exact same problem. 我知道这是一个老问题,但我最近遇到了完全相同的问题。 As suggested I was using the command-line workaround until a couple of minutes ago I found this post . 正如所建议的,我使用命令行解决方法,直到几分钟前我发现了这篇文章

Since I was creating the process, I just followed the instructions on the article adding CREATE_BREAKAWAY_FROM_JOB to the process creation flags: 自从我创建过程以来,我只是按照文章中的说明将CREATE_BREAKAWAY_FROM_JOB添加到进程创建标志:

CreateProcess(szPath, NULL, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi)

with

CreateProcess(szPath, NULL, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS | CREATE_BREAKAWAY_FROM_JOB, NULL, NULL, &si, &pi)

I tested and it works as expected, the process gets assigned to the job, no UAC manifest, no command-line. 我测试了它并按预期工作,进程被分配给作业,没有UAC清单,没有命令行。

Hope it helps you or anyone else having this problem. 希望它可以帮助您或其他任何有这个问题的人。

I followed a number of discussions at one point relating to job objects and UAC manifests. 我在一个关于工作对象和UAC清单的讨论中进行了一些讨论。 The only piece of information that helped me around the same problem that you're having was that this security feature (introduced in Vista) is apparently not enforced when run from a cmd. 帮助我解决您所遇到的同一问题的唯一信息是,从cmd运行时,此安全功能(在Vista中引入)显然不会强制实施。

I assume you're launching PyCmd from the start menu. 我假设您从开始菜单启动PyCmd。 Try launching it from cmd, and I'll bet the problem will go away there too. 尝试从cmd启动它,我敢打赌问题也会消失。

What I ended up doing (to run mintty with cygwin) was to make a mintty.bat which said 我最终做的事情(与cygwin运行一致)是制作一个mintty.bat说

start mintty.exe

and then a shortcut to mintty.bat which had a property set to run 'minimized' (I forget the exact wording). 然后是mintty.bat的快捷方式,它有一个属性设置为'最小化'(我忘记确切的措辞)。 This allows the shell I wanted to be launched from the start menu and still work as if it had been launched from cmd.exe. 这允许我想从开始菜单启动的shell,并且仍然可以像从cmd.exe启动一样工作。

As a side-note, I'd love it if someone would come along and actually explain the problem at a build-level, and how to fix it. 作为旁注,我会喜欢它,如果有人会来,并在构建级别实际解释问题,以及如何解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM