繁体   English   中英

CreateProcessAsUser在Windows服务中不起作用

[英]CreateProcessAsUser doesn't work in windows service

我想用C ++制作Windows服务,以便每次用户登录时都以管理员身份启动程序而不会弹出UAC窗口

因为这是我第一次这样做,所以我在这里使用了该项目: https : //code.msdn.microsoft.com/windowsapps/CppWindowsService-cacf4948/view/SourceCode

我在CppWindowsService.cpp中将第74行编辑为:

InstallService(
            SERVICE_NAME,               // Name of service
            SERVICE_DISPLAY_NAME,       // Name to display
            SERVICE_AUTO_START,         // Service start type
            SERVICE_DEPENDENCIES,       // Dependencies
            0,            // Service running account
            SERVICE_PASSWORD            // Password of the account
            );

并在SampleService.cpp第101行的工作线程中添加了一些代码,如下所示:

void CSampleService::ServiceWorkerThread(void)
{
// Periodically check if the service is stopping.
while (!m_fStopping)
{
    // Perform main service function here...

    HANDLE hToken = NULL, dToken;
    WTSQueryUserToken(WTSGetActiveConsoleSessionId(), &hToken);
    DuplicateTokenEx(hToken, MAXIMUM_ALLOWED, 0, SecurityIdentification, TokenPrimary, &dToken);
    STARTUPINFO stinfo = { 0 };
    PROCESS_INFORMATION pinfo = { 0 };
    stinfo.cb = sizeof(stinfo);
    stinfo.lpDesktop = L"winsta0\\default";
    LPVOID pEnv = 0;
    CreateEnvironmentBlock(&pEnv, dToken, 0);
    CreateProcessAsUserW(dToken, L"F:\\test.exe",0, 0, 0, 0,CREATE_NEW_CONSOLE, pEnv, 0, &stinfo, &pinfo);

    ::Sleep(2000);  // Simulate some lengthy operations.
}

// Signal the stopped event.
SetEvent(m_hStoppedEvent);
}

F驱动器中的test.exe显示一个消息框,但是当我尝试从服务运行它时,什么都没发生,尽管我可以轻松地使用CreateProcess从普通程序中执行它。

编辑:我也尝试在CreateProcessAsUser中放置命令行,但什么也没发生

我在函数中使用CREATE_UNICODE_ENVIRONMENT作为创建标志,问题已解决

暂无
暂无

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

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