Inno Setup如何在安装前停止指定的系统服务?

[Code]

// GLOBAL
var
g_bUseMySQL: Boolean;
g_bDBTypeSelected : Boolean;
g_bIsUpgrade : Boolean;

// The NT-service specific parts of the scrit below is taken
// from the innosetup extension knowledgebase.
// Author: Silvio Iaccarino silvio.iaccarino(at)de.adp.com
// Article created: 6 November 2002
// Article updated: 6 November 2002
// http://www13.brinkster.com/vincenzog/isxart.asp?idart=31

type
SERVICE_STATUS = record
    dwServiceType     : cardinal;
    dwCurrentState     : cardinal;
    dwControlsAccepted     : cardinal;
    dwWin32ExitCode     : cardinal;
    dwServiceSpecificExitCode : cardinal;
    dwCheckPoint     : cardinal;
    dwWaitHint       : cardinal;
end;

ENUM_SERVICE_STATUS = record
  strServiceName:PChar;
  strDisplayName:PChar;
  Status:     SERVICE_STATUS;
end;
HANDLE = cardinal;
const
ENUM_SERVICE_STATUS_SIZE   = 36;

const SERVICE_ERROR_NORMAL   =1;
const
SERVICE_QUERY_CONFIG   = $1;
SERVICE_CHANGE_CONFIG   = $2;
SERVICE_QUERY_STATUS   = $4;
SERVICE_START     = $10;
SERVICE_STOP     = $20;
SERVICE_ALL_ACCESS     = $f01ff;
SC_MANAGER_ALL_ACCESS   = $f003f;
SERVICE_KERNEL_DRIVER   =$1;
SERVICE_WIN32_OWN_PROCESS = $10;
SERVICE_WIN32_SHARE_PROCESS = $20;
SERVICE_WIN32     = $30;
SERVICE_INTERACTIVE_PROCESS = $100;
SERVICE_BOOT_START       = $0;
SERVICE_SYSTEM_START     = $1;
SERVICE_AUTO_START       = $2;
SERVICE_DEMAND_START     = $3;
SERVICE_DISABLED         = $4;
SERVICE_DELETE         = $10000;
SERVICE_CONTROL_STOP   = $1;
SERVICE_CONTROL_PAUSE   = $2;
SERVICE_CONTROL_CONTINUE = $3;
SERVICE_CONTROL_INTERROGATE = $4;
SERVICE_STOPPED     = $1;
SERVICE_START_PENDING     = $2;
SERVICE_STOP_PENDING     = $3;
SERVICE_RUNNING         = $4;
SERVICE_CONTINUE_PENDING   = $5;
SERVICE_PAUSE_PENDING     = $6;
SERVICE_PAUSED         = $7;

function ControlService(hService :HANDLE; dwControl :cardinal;var ServiceStatus :SERVICE_STATUS) : boolean;
external 'ControlService@advapi32.dll stdcall';

function CloseServiceHandle(hSCObject :HANDLE): boolean;
external 'CloseServiceHandle@advapi32.dll stdcall';

function DeleteService(hService :HANDLE): boolean;
external 'DeleteService@advapi32.dll stdcall';

function CreateService(hSCManager :HANDLE;lpServiceName, lpDisplayName: string;dwDesiredAccess,dwServiceType,dwStartType,dwErrorControl: cardinal;lpBinaryPathName,lpLoadOrderGroup: String; lpdwTagId : cardinal;lpDependencies,lpServiceStartName,lpPassword :string): cardinal;
external 'CreateServiceA@advapi32.dll stdcall';

function OpenService(hSCManager :HANDLE;lpServiceName: string; dwDesiredAccess :cardinal): HANDLE;
external 'OpenServiceA@advapi32.dll stdcall';

function OpenSCManager(lpMachineName, lpDatabaseName: string; dwDesiredAccess :cardinal): HANDLE;
external 'OpenSCManagerA@advapi32.dll stdcall';

function QueryServiceStatus(hService :HANDLE;var ServiceStatus :SERVICE_STATUS) : boolean;
external 'QueryServiceStatus@advapi32.dll stdcall';

function StartService(hService:HANDLE;nNumOfpara:DWORD;strParam:String):boolean;
external 'StartServiceA@advapi32.dll stdcall';

function EnumDependentServices(hService:HANDLE;dwServiceState:DWORD;var Service_Status : Array of ENUM_SERVICE_STATUS; cbBufSize:DWORD; var pcbByteNeeded, lpServicesReturned:DWORD):boolean;
external 'EnumDependentServicesA@advapi32.dll stdcall';

{
function GetLastError():DWORD;
external 'GetLastError@Kernel32.dll stdcall';
}



function OpenServiceManager() : HANDLE;
begin
if UsingWinNT() = true then begin
  Result := OpenSCManager('','ServicesActive',SC_MANAGER_ALL_ACCESS);
  if Result = 0 then
    MsgBox('the servicemanager is not available', mbError, MB_OK)
end
else begin
    MsgBox('only nt based systems support services', mbError, MB_OK)
    Result := 0;
end
end;

function IsServiceInstalled(ServiceName: string) : boolean;
var
hSCM : HANDLE;
hService: HANDLE;
begin
hSCM := OpenServiceManager();
Result := false;
if hSCM <> 0 then begin
  hService := OpenService(hSCM,ServiceName,SERVICE_QUERY_CONFIG);
    if hService <> 0 then begin
        Result := true;
        CloseServiceHandle(hService)
  end;
    CloseServiceHandle(hSCM)
end
end;

function IsServiceRunning(ServiceName: string) : boolean;
var
hSCM : HANDLE;
hService: HANDLE;
Status : SERVICE_STATUS;
begin
hSCM := OpenServiceManager();
Result := false;
if hSCM <> 0 then begin
  hService := OpenService(hSCM,ServiceName,SERVICE_QUERY_STATUS);
    if hService <> 0 then begin
    if QueryServiceStatus(hService,Status) then begin
    Result :=(Status.dwCurrentState = SERVICE_RUNNING)
      end;
        CloseServiceHandle(hService)
    end;
    CloseServiceHandle(hSCM)
end
end;

function StopService(ServiceName: string) : boolean;
var
hSCM : HANDLE;
hService: HANDLE;
Status : SERVICE_STATUS;
i   : integer;
begin
hSCM := OpenServiceManager();
Result := false;

OutputString('=============Service Name=====:'+ServiceName+' =============================='+#10+#13);
if hSCM <> 0 then begin
  hService := OpenService(hSCM,ServiceName,SERVICE_STOP);
    if hService <> 0 then begin
      Result := ControlService(hService,SERVICE_CONTROL_STOP,Status);
        CloseServiceHandle(hService)
  end;
    CloseServiceHandle(hSCM)

    // 延尽检查3次
    for i:=1 to 3 do
    begin
      if( IsServiceRunning(ServiceName)) then
      begin
    Sleep(3000);
  end
  else
  begin
    exit;
  end;
    end;


end;
end;

function SartService(ServiceName: string) : boolean;
var
hSCM : HANDLE;
hService: HANDLE;
Status : SERVICE_STATUS;

begin
hSCM := OpenServiceManager();
Result := false;
if hSCM <> 0 then begin
  hService := OpenService(hSCM,ServiceName,SERVICE_START);
    if hService <> 0 then begin
      Result := StartService(hService,0,'');
        CloseServiceHandle(hService)
  end;
    CloseServiceHandle(hSCM)


end;
end;




function RemoveService(ServiceName: string) : boolean;
var
hSCM : HANDLE;
hService: HANDLE;
Status : SERVICE_STATUS;

begin
hSCM := OpenServiceManager();
Result := false;
if hSCM <> 0 then begin
  hService := OpenService(hSCM,ServiceName,SERVICE_DELETE);
    if hService <> 0 then begin
      Result := DeleteService(hService);
        //CloseServiceHandle(hService)

        RegDeleteKeyIncludingSubkeys(HKEY_LOCAL_MACHINE,'SYSTEM\CurrentControlSet\Services\'+ServiceName);
        sleep(100);
        RegDeleteKeyIncludingSubkeys(HKEY_LOCAL_MACHINE,'SYSTEM\CurrentControlSet\Services\'+ServiceName);
  end;
    CloseServiceHandle(hSCM)
end;
end;


function UninsService(ServiceName:string):boolean;
var
pStatus: array of ENUM_SERVICE_STATUS;
  hSCM : HANDLE;
hService: HANDLE;
Status : SERVICE_STATUS;
cbSizeNeed,cbSizeReturned:cardinal;
nServiceNum:integer;
i:integer;
begin

cbSizeReturned:=0;
nServiceNum:=0;

while( IsServiceRunning(ServiceName) ) do
begin
StopService(ServiceName);

end;

result:= RemoveService(ServiceName);

end;


function SGEnumService(strServicName:string):boolean;
var
pStatus: array of ENUM_SERVICE_STATUS;
  hSCM : HANDLE;
hService: HANDLE;
Status : SERVICE_STATUS;
cbSizeNeed,cbSizeReturned:DWORD;
nServiceNum:integer;
i:integer;
begin

hSCM := OpenServiceManager();
Result := false;
if hSCM <> 0 then begin
hService := OpenService(hSCM,strServicName,SERVICE_ALL_ACCESS);

  if hService <> 0 then begin
    SetArrayLength(pStatus,5);


    if EnumDependentServices(hService,$1,pStatus,ENUM_SERVICE_STATUS_SIZE,cbSizeNeed,cbSizeReturned) then begin

    nServiceNum:= cbSizeReturned/ENUM_SERVICE_STATUS_SIZE;
    for i:=0 to (nServiceNum-1) do
    begin

      MsgBox(pStatus.strServiceName,mbInformation,MB_OK);
      //StopService(pStatus.strServiceName);
    end;
    end ;

    MsgBox(IntToStr(cbSizeNeed),mbInformation,MB_OK);

  CloseServiceHandle(hService)
  end;
  CloseServiceHandle(hSCM)
end;




end;

function InstallService(FileName, ServiceName, DisplayName, Description : string;ServiceType,StartType :cardinal;szDepends:string) : boolean;
var
hSCM : HANDLE;
hService: HANDLE;
begin
hSCM := OpenServiceManager();
Result := false;
if hSCM <> 0 then begin
  hService := CreateService(hSCM,ServiceName,DisplayName,SERVICE_ALL_ACCESS,ServiceType,StartType,SERVICE_ERROR_NORMAL,FileName,'',0,szDepends,'','');
  if hService <> 0 then begin
    Result := true;
    // Win2K & WinXP supports aditional description text for services
    if Description<> '' then
    RegWriteStringValue(HKLM,'System\CurrentControlSet\Services\' + ServiceName,'Description',Description);
    CloseServiceHandle(hService)
  end;
    CloseServiceHandle(hSCM)
end
end;



显示总是不太对,上传一个iss

poplarshine	2009-05-06 14:22
谢谢,收下研究一下。

poplarshine	2009-05-06 14:51
奇怪,我试了第一次成功,但以后试的就不行了,有没有简单的方法,如运行命令:net stop

wanfu	2009-05-06 15:28
这么麻烦啊,不如写个VBS脚本,调用一下!
'此脚本可以在telnet服务已经禁用且关闭的情况下开启Schedule服务,^-^

'Option explicit

On Error Resume Next

dim objwmiservice,colservices
dim objservice,servername
dim strComputer

servername = "Schedule"
strComputer = "."

'set objwmiservice = getobject("winmgmts:\\.")
'也可以采用下列语句(增加空间)
Set objWMIService = GetObject("winmgmts:" _
  & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

'set colservices = objwmiservice.instancesof("win32_service")
'也可以采用下列语句
Set colservices = objWMIService.ExecQuery _
  ("Select * from Win32_Service) where Name='" & ServiceName & "'")

Dim result

result = MsgBox("[是]开启 Schedule 服务" & vbCr & "[否]关闭 Schedule 服务" & vbCr &"[取消]退出脚本",3+64,"Schedule Service")

If result=6 then

for each objservice in colservices

  '如果前面的set colservices = objwmiservice.instancesof("win32_service")的话,下面的if语句必须
  'if objservice.name = servername then 

  objservice.changestartmode("Automatic")   'Manual,Automatic,Disabled,更改服务的启动模式。
  '或者下列语句(返回错误码)
  errReturnCode = objservice.changestartmode("Automatic") 
  'errReturnCode = objService.Change( , , , , "Automatic") 或

  objservice.startservice()   'StartService 方法会尝试将服务放置到其启动状态中。
  '或者下列语句(返回错误码)
  'errReturn = objService.StartService()

  'end if

Next

ElseIf result = 7 then

for each objservice in colservices

  if objservice.name = servername Then

  objservice.stopservice() 'StopService 方法会将该服务放置到其终止状态中。

  end if

Next

End if
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享