在服务中用管理员权限创建一个进程

HANDLE hToken;
    HDESK hdesk;
    HWINSTA hwinsta;
    PROCESS_INFORMATION pi;
    PSID psid;
    STARTUPINFO si;

    //
    // obtain an access token for the user fester
    //
    if (!LogonUser(
        strUser, //用户名“administrator"
        NULL,
        strPwd,  //密码“password”
        LOGON32_LOGON_INTERACTIVE,
        LOGON32_PROVIDER_DEFAULT,
        &hToken))
    {
        goto end;
    }
    //
    // obtain a handle to the interactive windowstation
    //
    hwinsta = OpenWindowStation(
        "winsta0",
        FALSE,
        READ_CONTROL | WRITE_DAC
        );
    if (hwinsta == NULL)
        goto end;

    HWINSTA hwinstaold = GetProcessWindowStation();

    //
    // set the windowstation to winsta0 so that you obtain the
    // correct default desktop
    //
    if (!SetProcessWindowStation(hwinsta))
        goto end;

    //
    // obtain a handle to the "default" desktop
    //
    hdesk = OpenDesktop(
        "default",
        0,
        FALSE,
        READ_CONTROL | WRITE_DAC |
        DESKTOP_WRITEOBJECTS | DESKTOP_READOBJECTS
        );
    if (hdesk == NULL)
        goto end;
    //
    // obtain the logon sid of the user fester
    //
    if (!ObtainSid(hToken, &psid))
        goto end;

    //
    // add the user to interactive windowstation
    //
    if (!AddTheAceWindowStation(hwinsta, psid))
        goto end;

    //
    // add user to "default" desktop
    //
    if (!AddTheAceDesktop(hdesk, psid))
        goto end;

    //
    // free the buffer for the logon sid
    //
    RemoveSid(&psid);

    //
    // close the handles to the interactive windowstation and desktop
    //
    CloseWindowStation(hwinsta);

    CloseDesktop(hdesk);

    //
    // initialize STARTUPINFO structure
    //
    ZeroMemory(&si, sizeof(STARTUPINFO));
    si.cb = sizeof(STARTUPINFO);
    si.lpDesktop = "winsta0\\default";


    //
    // start the process
    //
    if (!CreateProcessAsUser(
        hToken,
        NULL,
        (LPSTR)(LPCTSTR)strcmd,
        NULL,
        NULL,
        FALSE,
        NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE,
        //CREATE_NO_WINDOW|NORMAL_PRIORITY_CLASS,
        NULL,
        NULL,
        &si,
        &pi
        ))
    {
        goto end;
    }

    SetProcessWindowStation(hwinstaold); //set it back

    //
    // close the handles
    //
    CloseHandle(pi.hProcess);

    CloseHandle(pi.hThread);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值