如何从Delphi服务中调用另一个应用程序?

时间:2013-02-10 19:14:52

标签: delphi

我用Delphi做过服务。每次我在该服务中调用另一个应用程序时,应用程序都不会运行。怎么了?

BTW我用过shellexecute,shellopen或用cmd调用它。这些方法都不起作用。

这是我的代码:

    program roro_serv;

uses
  SvcMgr,
  Unit1 in 'Unit1.pas' {Service1: TService},
  ping in 'ping.pas';

{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TService1, Service1);
  Application.Run;
end.

    unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,
  ExtCtrls, DB, MemDS, DBAccess, MyAccess, Menus, forms, IniFiles,
  ComCtrls, wininet, Variants, shellapi,
  FileCtrl, ExtActns, StdCtrls, ShellCtrls;

type
  TService1 = class(TService)
    Timer1: TTimer;
    procedure Timer1Timer(Sender: TObject);
    procedure ServiceExecute(Sender: TService);
    procedure ServiceStop(Sender: TService; var Stopped: Boolean);
    procedure ServiceStart(Sender: TService; var Started: Boolean);
  private
    { Private declarations }
  public
    function GetServiceController: TServiceController; override;
    { Public declarations }
    procedure run_procedure;
    procedure log(text_file, atext : string );
    procedure loginfo(text : string);
    function  CheckUrl(url: string): boolean;
    procedure execCMD(CommandLine, Work:  string);
    function  DoDownload(FromUrl, ToFile: String): boolean;
  end;

var
  Service1: TService1;
  iTime : integer;
  limit_time : integer = 2;
  myini : TiniFile;
  default_exe_path : string = '';
  default_log_path : string = '';
  appdir : String = '';

implementation

{$R *.DFM}

uses ping;

function TService1.CheckUrl(url: string): boolean;
var 
hSession, hfile, hRequest: hInternet;
dwindex,dwcodelen :dword; 
dwcode:array[1..20] of char; 
res : pchar; 
begin 
if pos('http://',lowercase(url))=0 then 
url := 'http://'+url; 
Result := false; 
hSession := InternetOpen('InetURL:/1.0', 
INTERNET_OPEN_TYPE_PRECONFIG,nil, nil, 0);
if assigned(hsession) then
begin 
hfile := InternetOpenUrl(
hsession, 
pchar(url), 
nil, 
0, 
INTERNET_FLAG_RELOAD, 
0); 
dwIndex := 0; 
dwCodeLen := 10; 
HttpQueryInfo(hfile, HTTP_QUERY_STATUS_CODE, 
@dwcode, dwcodeLen, dwIndex); 
res := pchar(@dwcode); 
result:= (res ='200') or (res ='302'); 
if assigned(hfile) then 
InternetCloseHandle(hfile); 
InternetCloseHandle(hsession); 
end;
end;

procedure ServiceController(CtrlCode: DWord); stdcall;
begin
  Service1.Controller(CtrlCode);
end;

function TService1.GetServiceController: TServiceController;
begin
  Result := ServiceController;
end;

procedure TService1.Timer1Timer(Sender: TObject);
begin
iTime:=iTime+1;
if iTime=15 then // (limit_time*60) then
  begin
      itime:=1;
      run_procedure;
  end;
// loginfo('Defaultlog : '+default_log_path+'; exe : '+default_exe_path);
end;

procedure TService1.ServiceExecute(Sender: TService);
begin
Timer1.Enabled := True;
while not Terminated do
ServiceThread.ProcessRequests(True);
Timer1.Enabled := False;
end;

procedure TService1.run_procedure;
var
i : integer;
sUrl, sLogFile, sAction, sAct_param : String;
begin
for i:=0 to 20 do
   begin
   sLogFile:=default_log_path+myini.ReadString('logs', 'log_file'+intTostr(i), '');
   if fileexists(slogfile) then
      begin
      loginfo(slogfile+' tersedia');
      sAction:=myini.ReadString('logs', 'action'+intTostr(i), '');
           if ((trim(sAction)<>'') and (fileexists(default_exe_path+sAction))) then
              begin
                   // this line is don't work in servcie
                   ShellExecute(Application.Handle, 'open', 'c:\Windows\notepad.exe', nil, nil, SW_SHOWNORMAL);
                   sAct_param:=myini.ReadString('logs', 'action_prm'+intTostr(i), '');
                   // this line is don't work in servcie
                   execCMD(sAction+' '+sAct_param, default_exe_path);
                   loginfo(sAction+' '+sAct_param+' defpath : '+default_exe_path);
                   // this loginfo works
              end;
      end else
      begin

      end;

   end;
end;

procedure TService1.log(text_file, atext: string);
var
logFile : TextFile;
begin
AssignFile(LogFile, text_file);
if FileExists(text_file) then
Append(LogFile) else rewrite(LogFile);
WriteLn(logFile, aText);
CloseFile(LogFile);
end;

procedure TService1.loginfo(text: string);
begin
log(ChangeFileExt(application.exename, '.log'), formatdateTime('dd-mm-yyyy hh:nn:ss ', now)+
text);
end;

procedure TService1.ServiceStop(Sender: TService; var Stopped: Boolean);
begin
myini.Free;
end;

procedure TService1.execCMD(CommandLine, Work: string);
var
SA: TSecurityAttributes;
SI: TStartupInfo;
PI: TProcessInformation;
StdOutPipeRead, StdOutPipeWrite: THandle;
WorkDir: string;
begin
with SA do begin
nLength := SizeOf(SA);
bInheritHandle := True;
lpSecurityDescriptor := nil;
end;
CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SA, 0);
try
with SI do
begin
FillChar(SI, SizeOf(SI), 0);
cb := SizeOf(SI);
dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
wShowWindow := SW_HIDE;
hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect stdin
hStdOutput := StdOutPipeWrite;
hStdError := StdOutPipeWrite;
end;
WorkDir := Work;
CreateProcess(nil, PChar('cmd.exe /C ' + CommandLine),
nil, nil, True, 0, nil,
PChar(WorkDir), SI, PI);
CloseHandle(StdOutPipeWrite);
finally
CloseHandle(StdOutPipeRead);
end;
end;

procedure TService1.ServiceStart(Sender: TService; var Started: Boolean);
begin
appdir:=ExtractFileDir(Application.ExeName);
myini:=TiniFile.Create(ExtractFileDir(application.ExeName)+'\setting.ini');
limit_time:=myini.ReadInteger('setting', 'limit_time', 0);
default_exe_path:=myini.ReadString('setting', 'default_exe_path','');
if trim(default_exe_path)='' then default_exe_path:=appdir+'\';

default_log_path:=myini.ReadString('setting', 'default_log_path','');
if trim(default_log_path)='' then default_log_path:=appdir+'\logs\';

end;

function TService1.DoDownload(FromUrl, ToFile: String): boolean;
begin
 {  with TDownloadURL.Create(self) do
   try
     URL:=FromUrl;
     FileName := ToFile;
     ExecuteTarget(nil) ;
   finally
     Free;
   end;    }
end;

end.

请参阅run_procedure代码行;

简单地说:如何从我的服务中调用另一个应用程序?

3 个答案:

答案 0 :(得分:9)

ShellExecute/Ex()CreateProcess()在与调用进程相同的会话中运行指定的文件/ app。服务始终在会话0中运行。

在XP及更早版本中,第一个登录用户也在会话0中运行,因此服务可以运行交互式进程并使其可供该交互式用户查看,但前提是该服务被标记为交互式({{ 1}}属性是真的)。如果多个用户登录,则它们在会话1+中运行,因此无法看到服务运行的交互式进程。

Windows Vista引入了一项名为"Session 0 Isolation"的新功能。交互式用户根本不再在会话0中运行,而是始终在会话1+中运行,而会话0根本不是交互式的(TService.Interactive属性不再有任何影响)。但是,为了帮助迁移旧服务,如果服务运行尝试在会话0上显示GUI的交互式进程,Windows会提示当前登录用户(如果有)切换到临时使GUI可见的单独桌面。在Windows 7以后,该遗留支持现已消失。

在2000年以后的Windows上的所有版本中,从服务运行交互式进程并让交互式用户可以查看的正确方法是使用CreateProcessAsUser()在指定用户的会话中运行新进程和桌面。在MSDN,StackOverflow和整个Web上都有很多详细的例子,所以我不打算在这里重申它们。

答案 1 :(得分:5)

服务在与交互式用户不同的会话中运行。服务在会话0中运行。会话0进程无权访问交互式桌面。这意味着任何在会话0中显示交互式进程的尝试都注定要失败。您正在尝试创建一个交互式的记事本进程。

有一些方法可以从会话中在交互式桌面上启动进程:Launching an interactive process from Windows Service in Windows Vista and later。正如您在阅读该文章后所理解的那样,您试图做的事情并非易事。

答案 2 :(得分:0)

该解决方案旨在在服务中使用,我想将这段代码粘贴到这里,因为这是我获得服务以当前登录用户身份运行应用程序的方式。

function WTSQueryUserToken(SessionId: ULONG; var phToken: THandle): BOOL; stdcall; external 'Wtsapi32.dll';

procedure runApp(appName: String);
var
  hToken: THandle;
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
  res: boolean;

  begin
    GetStartupInfo(StartupInfo);
   if WTSQueryUserToken(WtsGetActiveConsoleSessionID, hToken) then
   begin
     res := CreateProcessAsUser(hToken, PWideChar(appName), nil, nil, nil, False, CREATE_NEW_CONSOLE, nil, nil, StartupInfo, ProcessInfo);
     if res then
      WaitForSingleObject(ProcessInfo.hProcess,INFINITE);
   end;
end;

///您的服务或应用中的任何地方

RunApp ('notepad.exe');