【发布时间】:2012-11-29 13:09:32
【问题描述】:
我有一个 StartType = stSystem 的 Windows 服务。
它使用 CreateProcessWithLogonW 执行应用程序。
usr := 'myuser';
dmn := 'mydomain';
pwd := 'thepassword';
cmd := 'c:\myapp.exe -calculate';
wdir := 'c:\';
fillchar(si, sizeof(si), 0);
si.cb := sizeof(si);
if not CreateProcessWithLogon(
PWideChar(usr),
PWideCharOf(dmn),
PWideChar(pwd),
LOGON_WITH_PROFILE,
nil,
PWideChar(cmd),
NORMAL_PRIORITY_CLASS or CREATE_NEW_CONSOLE or CREATE_NEW_PROCESS_GROUP,
nil,
PWideChar(wdir),
si,
pi
)
then
RaiseLastOSError; // raises Code 5: Access Denied
在服务之外运行这段代码,一切正常!
为什么 CreateProcessWithLogon 会引发系统错误代码 5:访问被拒绝?
可能是这个原因吗?
MSDN article on CreateProcessWithLogonW 说:
带有 SP2 和 Windows Server 2003 的 Windows XP:您不能从在 LocalSystem 帐户下运行的进程调用 CreateProcessWithLogonW,因为该函数使用调用者令牌中的登录 SID,而 LocalSystem 帐户的令牌不包含此西德。或者,使用 CreateProcessAsUser 和 LogonUser 函数。
我使用的是 Windows 7 PRO x64
【问题讨论】:
-
您能指定服务运行的用户帐户吗?它可以是 LocalSystem、NetworkService、命名用户等。
-
服务作为 LocalSystem 运行
-
如果不使用 LOGON_WITH_PROFILE 会怎样?
-
我在下面添加了有关检查 Windows 事件日志的注释,因此您可以尝试下一个。
-
您在 LocalSystem 帐户下运行该服务,并且正如文档所述:“您不能从在 LocalSystem 帐户下运行的进程调用 CreateProcessWithLogonW”。因此,要么更改运行服务的用户帐户,要么切换到
CreateProcessAsUser(),如文档所述。
标签: delphi winapi windows-services