怪异的windows服务程序问题。(28分)

  • 主题发起人 主题发起人 ccinflames
  • 开始时间 开始时间
C

ccinflames

Unregistered / Unconfirmed
GUEST, unregistred user!
写了一个windows xp下的服务程序。大体的功能是每隔一段时间执行一个操作。用的是timer控件来实现。有两个比较奇怪的地方。
1,(我先用的notepad做试验),notepad不会显示出来。但是在任务管理器里面可以看到。2,当执行了一段时间以后。确切的说是执行了一定的次数之后,系统就不再继续执行打开notepad程序了。但是日志文件却显示没有问题。两种可能。1)刚执行完就被杀掉了。2)因为什么原因根本没执行。
分数只有这么多了。大家帮忙看看到底是什么问题?代码如下。
unit Main;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,
ExtCtrls, thrd, inifiles;

type
T_ZDKiTestSvc = class(TService)
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
procedure ServiceContinue(Sender: TService; var Continued: Boolean);
procedure ServicePause(Sender: TService; var Paused: Boolean);
procedure ServiceStop(Sender: TService; var Stopped: Boolean);
procedure ServiceStart(Sender: TService; var Started: Boolean);
private
{ Private declarations }
public
f:TextFile;
mythread:TPerformit;
commands:string;
path:string;
function GetServiceController: TServiceController; override;
procedure appendlog(s:String);
{ Public declarations }
end;

var
_ZDKiTestSvc: T_ZDKiTestSvc;

implementation

{$R *.DFM}

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

procedure T_ZDKiTestSvc.appendlog(s:string);
var
f:TextFile;
begin
if not(fileexists(path+'log.log')) then
filecreate(path+'log.log');
AssignFile(f,path+'log.log');
try
writeln(f,s);
finally
closefile(f);
end;
end;

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

procedure T_ZDKiTestSvc.ServiceContinue(Sender: TService;
var Continued: Boolean);
begin
//self.mythread.Resume;
continued:=true;
end;

procedure T_ZDKiTestSvc.ServicePause(Sender: TService; var Paused: Boolean);
begin
//self.mythread.Suspend;
paused:=true;
end;

procedure T_ZDKiTestSvc.ServiceStart(Sender: TService; var Started: Boolean);
var
ini:Tinifile;
milliseconds:integer;
begin
//self.mythread:=Tperformit.create(false);
//showmessage('start');
//self.Timer1Timer(nil);
started:=true;
path:= SysUtils.GetModuleName(HInstance);
Path := ExtractFilePath(Path);
if (path[length(path)]<>'/') then
path:=path+'/';
ini:=tinifile.Create(path+'config.ini');
try
milliseconds:=ini.Readinteger('params','milliseconds',3600000);
commands:=ini.ReadString('params','commands','psftp -l zdki 67.15.72.36 -pw edoconis10# -b C:/LogProcessingTools/script.sh');
self.Timer1.Enabled:=false;
self.Timer1.Interval:=milliseconds;
self.Timer1.Enabled:=true;
finally
ini.Free;
end;
assignfile(f,path+'log.log');
rewrite(f);
end;

procedure T_ZDKiTestSvc.ServiceStop(Sender: TService; var Stopped: Boolean);
begin
//self.mythread.Terminate;
//showmessage('stop');
stopped:=true;
closefile(f);
end;

procedure T_ZDKiTestSvc.Timer1Timer(Sender: TObject);
var
i:integer;
StartInfo:STARTUPINFO;
ProcessInfo:PROCESS_INFORMATION;
begin
i:=0;
GetStartupInfo(StartInfo);
CreateProcessA(nil,PChar(commands),nil,nil,false,0,nil,nil,StartInfo,ProcessInfo);
//this "winexec" crap doesn't seem to work well when running it for lots of times.
//i:=winexec(PChar(commands),SW_SHOWNORMAL);
//here if i>31 then right.else wrong.
writeln(f,'$'+datetimetostr(now())+'__'+inttostr(i));
end;

end.
 
如果 ServiceStartName = 'LocalSystem' 那么 LocalSystem(本地用户)用户启动的
CreateProcessA应该是以用户LocalSystem启动"记事本"的
如果当前用户是Adiminstrator 那么当然就看不到了
 
我登陆的用户是域用户,在任务管理器里面显示的notepad用户是system用户。我的程序里面的servicestartname是空。其实看见看不见窗口倒不是特别严重的问题。

关键的还是为什么执行了11次之后就不在增加了呢?如果杀掉其中一个,他又会自动给补成11个。我需要的是一直执行下去直到系统崩溃才可以啊。
 

Similar threads

后退
顶部