【发布时间】:2017-05-09 14:20:09
【问题描述】:
我需要从 Windows 服务运行命令行应用程序,以另一个 Windows 用户身份执行它(通过提供 domain\user 和 password)。
我以这种方式将 Windows API CreateProcessWithLogonW() 包装在我自己的 RunAppAsAnotherWindowsUser() 函数中:
function CreateProcessWithLogonW(
lpUsername,
lpDomain,
lpPassword: PWideChar;
dwLogonFlags: DWORD;
lpApplicationName: PWideChar;
lpCommandLine: PWideChar;
dwCreationFlags: DWORD;
lpEnvironment: Pointer;
lpCurrentDirectory: PWideChar;
lpStartupInfo: PStartupInfoW;
lpProcessInformation: PProcessInformation
): BOOL; stdcall; external 'advapi32.dll';
function RunAppAsAnotherWindowsUser(const User, Domain, PW, Application, CmdLineParams: WideString): LongWord;
const
LOGON_WITH_PROFILE = $00000001;
implementation
function RunAppAsAnotherWindowsUser(const User, Domain, PW, Application, CmdLineParams: WideString): LongWord;
var
si : TStartupInfoW;
pif : TProcessInformation;
begin
ZeroMemory(@si, sizeof(si));
si.cb := sizeof(si);
si.dwFlags := STARTF_USESHOWWINDOW;
si.wShowWindow := 1;
SetLastError(0);
CreateProcessWithLogonW(PWideChar(User), PWideChar(Domain), PWideChar(PW),
LOGON_WITH_PROFILE, nil, PWideChar(Application+' '+CmdLineParams),
CREATE_DEFAULT_ERROR_MODE, nil, nil, @si, @pif);
Result := GetLastError;
end;
当我从我为测试而创建的 Win32 VCL 应用程序中使用RunAppAsAnotherWindowsUser () 时,它工作正常。当我从 Windows 服务调用它时,它不起作用。
我将this page 读作advised here,但在我的情况下,我没有交互式进程,因为我的命令行应用程序只是执行数据库查询并写入临时文件。
我尝试将服务作为本地系统而不是域管理员运行,但行为是相同的。
该服务是作为服务构建的 IntraWeb(用于 Web 的 VCL)应用程序。作为 exe 构建的相同应用程序可以正常工作。我写这篇文章只是为了解释我工作的背景。
您能帮我找出问题吗?
【问题讨论】:
-
您遇到的实际问题是什么?你收到错误代码了吗?如果有,是哪一个?请更具体一点?
-
服务在隔离会话中运行,您应该在控制台或用户会话中启动该进程。
-
我试图以用户身份运行进程(实际上是作为 pc 管理员),这解决了部分问题实际上 GetLastError(参考上面的代码)为 0(所以没有错误),而之前是 5 点(回复@RemyLebeau)。无论如何,我使用 RunAppAsAnotherWindowsUser 运行的 exe 无法写入文件,即使我对专用文件夹中的每个人都设置了完全访问权限。无论如何,现在只是一个问题或权限。以用户身份运行服务器是一个重要的进步,现在我必须与 windows 文件夹权限作斗争(这真的不那么明显)。
标签: delphi service delphi-10-seattle