Link to home
Start Free TrialLog in
Avatar of Imbeginner
Imbeginner

asked on

Shut down/Stop/Running/Restarting IIS 5 From Delphi 7 ?

Hi,
Do this work available for our administrators To
Shut down/stop/restart/start +ing  IIS 5 From his application ?

OS:windows 2000 advanced server

Thanks in advance for your Helps.
Avatar of BedouinDN
BedouinDN

OK, you should be able to use the ControlService function as IIS is a service.

I found this code on the web (http://www.elists.org/pipermail/delphi/2003-April/023621.html) and have tested it on 2K and XP and it seems to work quite well. (Used Messenger service as I do not run IIS)
You will need to include WinSvc.pas and the user must have the appropriate priveledges for this to work.

//-----------------------------IsServiceRunning---------------------------------------------------------------
function IsServiceRunning(ServiceName: PChar): Boolean;
var
  ServerStatus  : SERVICE_STATUS;
  SCMHandle     : SC_HANDLE;
  QryResult     : Integer;
begin
  Result := False;
   try
     { Open the Service Control Manager }
     SCMHandle := OpenSCManager(nil, nil, GENERIC_EXECUTE);
     if (SCMHandle <> 0) then
     begin
       { Open and Query the specified Service }
       QryResult := OpenService(SCMHandle,
ServiceName,SERVICE_QUERY_STATUS);
       QueryServiceStatus(QryResult, ServerStatus);
       if (ServerStatus.dwCurrentState = SERVICE_RUNNING) then
         Result := True
       else
         Result := False;
       CloseServiceHandle(QryResult);
       CloseServiceHandle(SCMHandle);
     end
     else
       (*MessageDlg('Error opening Service Manager.', mtError, [mbOK], 0);*)
   except
     (*MessageDlg('Error checking status of service', mtError, [mbOK], 0);*)
   end;
end;

//-------------------------------StartService--------------------------------------------------------------------------------

function ServiceStart(sMachine, sService: String) : Boolean;
var
  schm,
  schs: SC_Handle;
  ss: TServiceStatus;
  psTemp: PChar;
  dwChkP: DWord;
begin
  ss.dwCurrentState := 0;
  schm := OpenSCManager(PChar(sMachine), nil, SC_MANAGER_CONNECT);
  if (schm>0) then
  begin
    schs := OpenService(schm, PChar(sService), SERVICE_START or
      SERVICE_QUERY_STATUS);
    if (schs>0) then
    begin
      psTemp := nil;
      if (StartService(schs, 0, psTemp)) then
        if (QueryServiceStatus(schs, ss)) then
          while (SERVICE_RUNNING<>ss.dwCurrentState) do
          begin
            dwChkP := ss.dwCheckPoint;
            Sleep(ss.dwWaitHint);
            if (not QueryServiceStatus(schs, ss)) then
              Break;
            if (ss.dwCheckPoint < dwChkP) then
              Break;
          end;
      CloseServiceHandle(schs);
    end;
    CloseServiceHandle(schm);
  end;
  Result := SERVICE_RUNNING=ss.dwCurrentState;
end;

//-----------------------------------StopService--------------------------------------------------------------------------------


function ServiceStop(sMachine, sService: String) : Boolean;
var
  schm,
  schs: SC_Handle;
  ss: TServiceStatus;
  dwChkP: DWord;
begin
  schm := OpenSCManager(PChar(sMachine), nil, SC_MANAGER_CONNECT);
  if (schm>0) then
  begin
    schs := OpenService(schm, PChar(sService), SERVICE_STOP or
      SERVICE_QUERY_STATUS);
    if (schs>0) then
    begin
      if (ControlService(schs, SERVICE_CONTROL_STOP, ss)) then
        if (QueryServiceStatus(schs, ss)) then
          while (SERVICE_STOPPED<>ss.dwCurrentState) do
          begin
            dwChkP := ss.dwCheckPoint;
            Sleep(ss.dwWaitHint);
            if (not QueryServiceStatus(schs, ss)) then
              Break;
            if (ss.dwCheckPoint < dwChkP) then
              Break;
          end;
      CloseServiceHandle(schs);
    end;
    CloseServiceHandle(schm);
  end;
  Result := SERVICE_STOPPED=ss.dwCurrentState;
end;

Cheers.
Bedouin..
Avatar of Imbeginner

ASKER

one problem
when you start service, you can t say that your web site is started too.your coding only fire servicing for a special service.
ASKER CERTIFIED SOLUTION
Avatar of Wim ten Brink
Wim ten Brink
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
workshop_alex:
hi, I used delphi7 for my works,
I think this program has some errors in WindowProc  Function
On these lines
1-
line-196
else if (uMsg = WM_DESTROY) then begin
      StopApp := True;  <---- left side cannot be assigned

2-
line-217
 ServiceWndClass.lpfnWndProc := @WindowProc; <---- left side can not be assigned.
___________________

another thing is:
suppose that the IIS was started,sometimes when we openend iis manager in administrator
folder we saw that our default web site was not started(remain in stopped state) ,
do any way existed for starting my web default site or restarting IIS ?
unit ServiceManager;

interface

uses Windows, Sysutils, WinSvc;


{
 this class requires the following access rights :

 SERVICE_CHANGE_CONFIG
 SERVICE_START
 SERVICE_STOP
 SC_MANAGER_CONNECT
 SC_MANAGER_ENUMERATE_SERVICE
 SC_MANAGER_QUERY_LOCK_STATUS

you must be an administrator

 }

type
  TServiceManager = class (TObject)
  private
    FServiceHandle: SC_Handle;
    FServiceManagerHandle: Int64;
    FServiceName: string;
    procedure SetServiceName(Value: string);
  public
    constructor Create;
    destructor Destroy; override;
    procedure SetServiceDescription(description: String);
    procedure StartService;
    procedure StopService;
    property ServiceName: string read FServiceName write SetServiceName;
  end;


  LPSERVICE_DESCRIPTIONA = ^SERVICE_DESCRIPTIONA;
  {$EXTERNALSYM LPSERVICE_DESCRIPTIONA}
  _SERVICE_DESCRIPTIONA = record
    lpDescription: LPSTR;
  end;
  {$EXTERNALSYM _SERVICE_DESCRIPTIONA}
  SERVICE_DESCRIPTIONA = _SERVICE_DESCRIPTIONA;
  {$EXTERNALSYM SERVICE_DESCRIPTIONA}
  TServiceDescriptionA = SERVICE_DESCRIPTIONA;
  PServiceDescriptionA = LPSERVICE_DESCRIPTIONA;

//
// Service description string
//

  LPSERVICE_DESCRIPTIONW = ^SERVICE_DESCRIPTIONW;
  {$EXTERNALSYM LPSERVICE_DESCRIPTIONW}
  _SERVICE_DESCRIPTIONW = record
    lpDescription: LPWSTR;
  end;
  {$EXTERNALSYM _SERVICE_DESCRIPTIONW}
  SERVICE_DESCRIPTIONW = _SERVICE_DESCRIPTIONW;
  {$EXTERNALSYM SERVICE_DESCRIPTIONW}
  TServiceDescriptionW = SERVICE_DESCRIPTIONW;
  PServiceDescriptionW = LPSERVICE_DESCRIPTIONW;

{$IFDEF UNICODE}
  SERVICE_DESCRIPTION = SERVICE_DESCRIPTIONW;
  {$EXTERNALSYM SERVICE_DESCRIPTION}
  LPSERVICE_DESCRIPTION = LPSERVICE_DESCRIPTIONW;
  {$EXTERNALSYM LPSERVICE_DESCRIPTION}
  TServiceDescription = TServiceDescriptionW;
  PServiceDescription = PServiceDescriptionW;
{$ELSE}
  SERVICE_DESCRIPTION = SERVICE_DESCRIPTIONA;
  {$EXTERNALSYM SERVICE_DESCRIPTION}
  LPSERVICE_DESCRIPTION = LPSERVICE_DESCRIPTIONA;
  {$EXTERNALSYM LPSERVICE_DESCRIPTION}
  TServiceDescription = TServiceDescriptionA;
  PServiceDescription = PServiceDescriptionA;
{$ENDIF}


LPVOID = Pointer;
{$EXTERNALSYM LPVOID}

const
  SERVICE_CONFIG_DESCRIPTION     = 1;
  {$EXTERNALSYM SERVICE_CONFIG_DESCRIPTION}


implementation


{
******************************* TServiceManager ********************************
}
constructor TServiceManager.Create;
var
  sMachine: string;
begin
  FServiceName := '';
  FServiceHandle := 0;

  sMachine := ''; // localhost is used when empty
  FServiceManagerHandle := OpenSCManager(PChar(sMachine), Nil, SC_MANAGER_CONNECT);


end;

destructor TServiceManager.Destroy;
begin

  if FServiceHandle <> 0 then
    CloseServiceHandle(FServiceHandle);

  CloseServiceHandle(FServiceManagerHandle);

  inherited;
end;


procedure TServiceManager.SetServiceDescription(description: String);
type
TFnc_ChangeServiceConfig2 = function(hService: SC_HANDLE; dwInfoLevel: DWORD;
                                     lpInfo: LPVOID): BOOL; stdcall;
var
  srvDesc: TServiceDescription;
  csc2: TFnc_ChangeServiceConfig2;
  dllHandle: THandle;
begin

  if FServiceHandle = 0 then begin
    Raise Exception.Create('TServiceManager.SetServiceDescription: Service not selected');
    Exit;
  end;

  srvDesc.lpDescription := pChar(Description);

  { this ChangeServiceConfigA doesn't exist on NT 4 and lover}
  dllHandle := 0;
  try
    dllHandle := LoadLibrary('advapi32');
    @csc2 := GetProcAddress(dllHandle, 'ChangeServiceConfig2A')
  except
    csc2 := nil;
  end;

  if Assigned(csc2) then begin
    try
      csc2(FServiceHandle, SERVICE_CONFIG_DESCRIPTION, @srvDesc);
    except

    end;
  end;

  try
    FreeLibrary(dllHandle);
  except

  end;



end;

procedure TServiceManager.SetServiceName(Value: string);
begin

  if Value = FServiceName then
    Exit;

  FServiceName := Value;

  if FServiceHandle <> 0 then
    CloseServiceHandle(FServiceHandle);

  FServiceHandle := OpenService(FServiceManagerHandle, PChar(FServiceName),
                    SERVICE_CHANGE_CONFIG or SERVICE_START or SERVICE_STOP);

end;

procedure TServiceManager.StartService;
var
  dummy: PChar;
begin

  if FServiceHandle = 0 then begin
    Raise Exception.Create('TServiceManager.StartService: Service not selected');
    Exit;
  end;

  winsvc.StartService(FServiceHandle, 0, dummy);

end;

procedure TServiceManager.StopService;
var
  stat: TServiceStatus;
begin

  if FServiceHandle = 0 then begin
    Raise Exception.Create('TServiceManager.StopService: Service not selected');
    Exit;
  end;

  ControlService(FServiceHandle, SERVICE_CONTROL_STOP, stat);
end;
end.


hope it helped...