【发布时间】:2012-10-31 22:20:36
【问题描述】:
我有一个使用 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
我做错了什么? 我该如何解决这个问题? 有人可以发一个例子吗?
【问题讨论】:
-
Read the documentation。您用于在 noservice 模式下运行应用程序的用户帐户可能没有必要的权限,但在服务模式下运行应用程序时,SYSTEM 帐户确实具有权限。
-
如果你正在运行询问 LOCALSYSTEM 那么你不需要登录用户和明文密码。如果你在桌面上运行,你可以简单地调用 CreateProcess。
标签: delphi