gpt4 book ai didi

delphi - 服务处的 LogonUser + CreateProcessAsUser = 错误 1314

转载 作者:行者123 更新时间:2023-12-01 21:13:13 26 4
gpt4 key购买 nike

我有一个使用 Delphi 7 创建的 Windows 服务,StartType = stSystem。

现在我需要启动一个应用程序来为我做一些事情。该应用程序具有 MainForm 和其他 GDI 资源。传递给应用程序的参数为某些控件(如编辑和备忘录)分配值,然后单击按钮......

我正在尝试这个:

var
token: cardinal;
si: TStartupInfo;
pi: TProcessInformation;
begin
if not LogonUser('admintest', '', 'secret123', LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, token) then
RaiseLastOSError;

try
if not ImpersonateLoggedOnUser(token) then
RaiseLastOSError;

fillchar(si, sizeof(si), 0);
si.cb := sizeof(si);
si.lpDesktop := PChar('winsta0\default');
if not CreateProcessAsUser(token, nil, '"c:\...\myapp.exe" -doCrazyThings', nil, nil, false, NORMAL_PRIORITY_CLASS or CREATE_NEW_CONSOLE, nil, nil, si, pi) then
RaiseLastOSError;

CloseHandle(pi.hThread);
waitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
finally
CLoseHandle(token);
end;
end;

当我将服务可执行文件作为普通应用程序 (-noservice) 运行时,它作为 Forms.Application 启动,并创建一个带有“开始”按钮的 MainForm。*按钮运行的代码与服务运行的代码相同,但它不起作用,并且在 createprocessasuser 处引发错误代码 1314。*

为什么? SYSTEM服务和管理员启动的普通应用程序有什么区别?

我的环境是 Windows 7 Pro x64

我做错了什么?我该如何解决这个问题?有人可以举个例子吗?

最佳答案

错误 1314 是 ERROR_PRIVILEGE_NOT_HELD,这意味着您的调用线程缺少运行 CreateProcessAsUser() 所需的权限。您不需要也不应该模拟用户 token 来在用户桌面中启动新进程。调用 CreateProcessAsUser() 时,您应该让线程使用服务的凭据,而不是用户的凭据。它将确保新进程在用户帐户和桌面内运行,因此无需调用 ImpersonateLoggedOnUser() 并查看 CreateProcessAsUser() 是否开始工作.

更新:read the documentation :

Typically, the process that calls the CreateProcessAsUser function must have the SE_INCREASE_QUOTA_NAME privilege and may require the SE_ASSIGNPRIMARYTOKEN_NAME privilege if the token is not assignable. If this function fails with ERROR_PRIVILEGE_NOT_HELD (1314), use the CreateProcessWithLogonW function instead. CreateProcessWithLogonW requires no special privileges, but the specified user account must be allowed to log on interactively. Generally, it is best to use CreateProcessWithLogonW to create a process with alternate credentials.

关于delphi - 服务处的 LogonUser + CreateProcessAsUser = 错误 1314,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13145632/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com