我用Delphi做过服务.每次我在该服务中调用另一个应用程序时,应用程序都不会运行.哪里不对?
顺便说一下,我使用了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,0); if assigned(hsession) then begin hfile := InternetOpenUrl( hsession,pchar(url),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',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),True,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代码行;
简单地说:如何从我的服务中调用另一个应用程序?
解决方法
ShellExecute / Ex()和CreateProcess()在与调用进程相同的会话中运行指定的文件/ app.服务始终在会话0中运行.
在XP及更早版本中,第一个登录用户也在会话0中运行,因此服务可以运行交互式进程并使其可供该交互式用户查看,但前提是该服务被标记为交互式(TService.Interactive属性为真正).如果多个用户登录,则它们在会话1中运行,因此无法查看服务运行的交互式进程.
Windows Vista引入了一个名为“Session 0 Isolation”的新功能.交互式用户根本不再在会话0中运行,它们总是在会话1中运行,而会话0根本不是交互式的(TService.Interactive属性不再有任何影响).但是,为了帮助迁移旧服务,如果服务运行尝试在会话0上显示GUI的交互式进程,Windows会提示当前登录用户(如果有)切换到临时使GUI可见的单独桌面.在Windows 7以后,这种传统支持现在已经消失.
在从2000年开始的Windows上的所有版本中,从服务运行交互式进程并让交互式用户可以查看的正确方法是使用CreateProcessAsUser()
在指定用户的会话和桌面中运行新进程.在MSDN,StackOverflow和整个Web上都有很多详细的例子,所以我不打算在这里重申它们.