2010-01-03 158 views
4

我嘗試按照http://edn.embarcadero.com/article/28604重新啓動Interbase。 下面是一些代碼:如何重新啓動Interbase

program IBRestart; 

{$APPTYPE CONSOLE} 

uses 
    SysUtils, winsvc; 

var 
    vManager, vService: SC_Handle; 
    vtmp: TServiceStatus; 
begin 
    vManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS); 
    if vManager > 0 then 
    begin 
    vService := OpenService(vManager, 'InterBaseGuardian', SERVICE_START or SERVICE_STOP); 
    if vService = 0 then   // vService is always 0 here, why ? 
     CloseServiceHandle(vManager) 
    else 
    if ControlService(vService, SERVICE_CONTROL_STOP, vTmp) and 
     QueryServiceStatus(vService, vTmp) and 
     (vTmp.dwCurrentState = SERVICE_STOPPED) then 
    begin 
     WriteLn('Success'); 
    end; 
    end; 
end. 

我注意到,該服務被列爲服務對話「的InterBase 2009衛gds_db」。我已經嘗試過OpenService的參數的不同變體,但沒有成功......任何提示?

編輯: 網啓動列表,的InterBase 2009年衛gds_db服務的InterBase 2009年服務器gds_db

RaiseLastOSError在這兩種情況下,返回此:項目IBRestart.exe引發的異常類EOSError與消息「系統錯誤。代碼:1060.指定的服務不作爲已安裝的服務存在「。

因此,vService在上面的代碼中始終爲0。我甚至試圖阻止另一種服務,如主題,它實際上工作。它可以是字符串中必須特別處理的空格嗎?我試圖關閉IIS Admin,並且它返回與Interbase相同的錯誤消息。

回答

2

這是我使用的重新啓動2007年的InterBase,批處理文件它表明你是在某些機器上的服務名稱在他們一個額外的空間:

rem jpl: 20071015 - on some machines, the guardian service has an extra space 
net stop "InterBase 2007 Guardian gds_db" 
net stop "InterBase 2007 Guardian gds_db " 
net stop "InterBase 2007 Server gds_db" 
net start "InterBase 2007 Guardian gds_db" 
net start "InterBase 2007 Guardian gds_db " 
pause 

注意,我停止和啓動的守護者兩次;有時它在服務停止/開始超時期間內沒有反應。 我也專門停止了InterBase服務;這幾乎不需要,但我有一次監護人服務停止了,但InterBase服務並沒有停止。

- jeroen

+0

優秀。這對我來說就夠了! – 2010-01-04 10:18:35

+0

很高興它爲你工作得那麼好! – 2010-01-04 13:32:31

4

也許服務名稱是錯誤的或者您沒有足夠的權限(需要以管理員身份啓動)?很難說沒有任何提示出了什麼問題。

請檢查是否有任何調用發出錯誤信息(返回代碼= 0),並在此情況下通過調用RaiseLastOSError或SysErrorMessage(GetLastError)來檢查錯誤是什麼。另外請確保檢查其他電話上的錯誤。請使用帶來的任何新信息更新您的問題。

並將您的支票從> 0更改爲<> 0.0信號錯誤,其他任何成功。句柄可以是負數。並添加一些try..finally。並且不要忘記再次啓動服務的代碼:)此外,在調用ControlService之後服務更改狀態可能需要一段時間,因此在實際停止之前,QueryServiceStatus可能會返回SERVICE_STOP_PENDING一段時間。你的代碼應該解釋它。一個例子見here

program IBRestart; 

{$APPTYPE CONSOLE} 

uses 
    SysUtils, winsvc; 

var 
    vManager, vService: SC_Handle; 
    vtmp: TServiceStatus; 
begin 
    vManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS); 
    if vManager <> 0 then 
    begin 
    try 
     vService := OpenService(vManager, 'InterBaseGuardian', SERVICE_START or SERVICE_STOP); 
     if vService = 0 then  // vService is always 0 here, why ? 
     RaiseLastOSError;  // This will give a hint why ! 
     else 
     try 
      Win32Check(ControlService(vService, SERVICE_CONTROL_STOP, vTmp)); 
      Win32Check(QueryServiceStatus(vService, vTmp)); 
      if vTmp.dwCurrentState = SERVICE_STOPPED then // This might also be SERVICE_STOP_PENDING 
      WriteLn('Success') 
      else 
      WriteLn('Failure'); 
     finally 
      CloseServiceHandle(vService); 
     end; 
    finally 
     CloseServiceHandle(vManager); 
    end; 
    end 
    else 
    RaiseLastOSError; 
end. 
+0

+1,all * very * valid points。順便說一句,在shell窗口中運行'net start'會給出一個正在運行的服務列表,並且需要使用正確的名稱將它們標識給服務管理器。 – mghie 2010-01-04 05:26:22

+0

感謝您的代碼。但主要的問題似乎是OpenService無法識別我發送的服務字符串。 – 2010-01-04 08:48:05

+0

我沒有安裝Interbase,但可以在HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services中找到服務名稱,或者使用像psservice這樣的工具(http://technet.microsoft.com/zh-cn/sysinternals/bb897542.aspx) – 2010-01-04 09:31:00

2

Guardian是誰重啓Ibserver.exe當它倒下服務:這是舊OS有用或當您運行ibserver爲應用程序。如果你使用Ibserver作爲服務,你可以直接在服務中管理這個服務(在這種情況下,Guardian是無用的)。

+0

不知道。但我認爲它應該由Interbase安裝程序自動處理。 – 2010-01-04 08:46:16