delphi 编写的后台服务如何在探测到文件被修改后启动外部程序?
我想在后台控测某odbc.ini文件的dnsip=192.168.0.1修改后执行外部程序mydns.exe
我用的是winexec调用的。不过当我手动双击启动这个服务程序后,修改dnsip=192.168.0.1它会启动mydns.exe 但如果我使用服务方式来运行服务程序那么修改dnsip=192.168.0.1就不再启动mydns.exe 了,请问应该如何做?

解决方案 »

  1.   

    问题:我想知道一个文件是否被改变,如何监视?一个方法是使用Timer不停监测,这样不精确并且来非CPU时间,那么有没有更好的方法呢?来自Luke有:TFileChangeNotificationStatus=(fnsIdle,fnsError,fnsRunning);   TFileChangeNotification=record    PathName:string;    WatchSubtree:boolean;    NotifyFilter:DWORD;    Status:TFileChangeNotificationStatus;    Changes:DWORD;    wndHandle:HWND;    NotificationMessage:DWORD;  end;    TFileChangesMultipleWatcher = class(TThread)  private    { Private declarations }    FMutex:integer;    FObjectsArray:array[0..(WAITOBJECTSCOUNT-1)]of integer;  protected    procedure Execute; override;  public    Notification:TFileChangeNotification;    constructor Create(Suspended:boolean);    destructor Destroy; override;    procedure Init;    procedure Stop;  end;  constructor TFileChangesMultipleWatcher.Create;var guid:TGUID;begin  inherited;  coCreateGuid(guid);  FMutex:=CreateMutex(nil,False,PChar('TFCMW.'+GuidToString(guid)));  WaitForSingleObject(FMutex,INFINITE);  if FMutex<>0 then FObjectsArray[0]:=FMutex;  FreeOnTerminate:=True;end;// destructor TFileChangesMultipleWatcher.Destroy;begin  inherited;  {$ifdef DEBUGGING_FILECHANGES} writeln(Notification.PathName,' -DESTROY'); {$endif}end;// procedure TFileChangesMultipleWatcher.Init;begin  with Notification do begin    PathName:='';    WatchSubtree:=False;    NotifyFilter:=0;    Status:=fnsIdle;    Changes:=0;    wndHandle:=0;    NotificationMessage:=WM_USER;  end;end;// procedure TFileChangesMultipleWatcher.Stop;begin  Terminate;  ReleaseMutex(FMutex);end;// procedure TFileChangesMultipleWatcher.Execute;var WaitStatus,hChanges:DWORD;    Go:boolean;begin  try while (not Terminated) do begin        with Notification do begin hChanges:=FindFirstChangeNotification(PChar(PathName),WatchSubtree,NotifyFilter);          {$ifdef DEBUGGING_FILECHANGES} writeln('Notification Handle is',hChanges); {$endif}          if (hChanges<>INVALID_HANDLE_VALUE) then try            FObjectsArray[1]:=hChanges;            Status:=fnsRunning;            Go:=True;            while (Go) do begin              {$ifdef DEBUGGING_FILECHANGES} writeln('WaitForSingleObject');{$endif} WaitStatus:=WaitForMultipleObjects(WAITOBJECTSCOUNT,@FObjectsArray,False,INFINITE);              file://if Terminated then exit;              case WaitStatus of                WAIT_ABANDONED:begin                  {$ifdef DEBUGGING_FILECHANGES} writeln('WAIT_ABANDONED');{$endif}                end;                WAIT_OBJECT_0:begin                   exit;                  {$ifdef DEBUGGING_FILECHANGES} writeln('WAIT_OBJECT_0');{$endif}                end;                WAIT_OBJECT_0+1:begin                  inc(Changes);                  if (wndHandle<>0) thenPostMessage(wndHandle,NotificationMessage,Changes,NotificationMessage+Changes);                  {$ifdef DEBUGGING_FILECHANGES} writeln('WAIT_OBJECT_1');{$endif}                end;                WAIT_TIMEOUT:begin                  {$ifdef DEBUGGING_FILECHANGES} writeln('WAIT_TIMEOUT');{$endif}                end;              end;              Sleep(100);              Go:=FindNextChangeNotification(hChanges);            end;          finally            {$ifdef DEBUGGING_FILECHANGES} writeln('FINALLY 1'); {$endif}            FindCloseChangeNotification(hChanges);          end else begin            Status:=fnsError;            {$ifdef DEBUGGING_FILECHANGES} writeln('STATUS ERROR'); {$endif}          end;        end;      end;  finally    {$ifdef DEBUGGING_FILECHANGES} writeln('FINALLY 2'); {$endif}    ReleaseMutex(FMutex);  end;  {$ifdef DEBUGGING_FILECHANGES} writeln('BYE!'); {$endif}end;转自超级猛料
      

  2.   

    问题是在windows服务模式下启动外部程序,让外部程序以登陆的用户名形式启动,而不是以system用户名启动
      

  3.   

    问题是在windows服务模式下启动外部程序,让外部程序以登陆的用户名形式启动,而不是以system用户名启动