Solved

Stop and start IIS with Delphi?

Posted on 2001-09-13
5
435 Views
Last Modified: 2010-04-06
I need to stop and start IIS (Windows 2000)with a delphi application.
Thanks
0
Comment
Question by:faustomen
5 Comments
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6480669
take a look at this link, it's for delphi and IIS
http://www.fulgan.com/delphi
0
 
LVL 10

Expert Comment

by:Jacco
ID: 6480703
On the local machine or on a remote one?

Regards Jacco
0
 

Author Comment

by:faustomen
ID: 6481034
On a remote server.
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 6482060
you done any work with services ?
if so ... then ti won't be a problem with this
just connect to the service manager on the remote machine
then open the IIS service  .. dunno the services name (not display name)
control it like any other service
i've done this but not for remote machines and not IIS
but a service is a service :)
but if you're not farmiliar with services I can post the answer with code
0
 
LVL 1

Accepted Solution

by:
pede earned 100 total points
ID: 6482203
Pretty long - I know - but it does the job. Not made by me, but by a co-worker.


procedure TMainForm.SysErr(Stage: string; error:integer);
var
  buf:array[0..1024] of char;
begin
  if (Error <> 0) then begin
     FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,nil,Error,0,buf,sizeof(buf),nil);
     messagebox(0,buf,pchar('Error in stage: '+Stage),mb_ok or mb_iconexclamation);
  end;
end;

function TMainForm.MyStartService(Machine, Service: string) : integer;
var
  dwOldCheckPoint : dword;
  hSCM : SC_HANDLE;
  hService : SC_HANDLE;
  ss : TServiceStatus;
  p : pchar;
begin
  hSCM:=0;
  hService:=0;
  Result:=0;

  try
    // Open the SCM database
    hSCM := OpenSCManager(pchar(Machine), NIL, SC_MANAGER_CONNECT);
    if hSCM = 0 then begin
      Result:=GetLastError;
      SysErr('OpenSCManager', GetLastError);
      exit;
    end;

    // Open the specified service
    hService := OpenService(hSCM, pchar(Service), SERVICE_START + SERVICE_QUERY_STATUS);
    if hService = 0 then begin
      Result:=GetLastError;
      SysErr('OpenService', GetLastError);
      exit;
    end;

    p:='';
    if not StartService(hService, 0, p) then begin
      Result:=GetLastError;
      SysErr('StartService', GetLastError);
      exit;
    end;

    // Check the status until the service is running.
    if not QueryServiceStatus(hService, ss) then begin
      Result:=GetLastError;
      SysErr('QueryServiceStatus', GetLastError);
      exit;
    end;

    while ss.dwCurrentState <> SERVICE_RUNNING do begin
      // Save the current checkpoint.
      dwOldCheckPoint := ss.dwCheckPoint;

      // Wait for the specified interval.
      sleep(ss.dwWaitHint);

      // Check the status again.
      if QueryServiceStatus(hService, ss) then
        break;

      // Break if the checkpoint has not been incremented.
      if dwOldCheckPoint >= ss.dwCheckPoint then
        break;
    end;
  finally
    if not QueryServiceStatus(hService, ss) then begin
      Result:=GetLastError;
      SysErr('QueryServiceStatus 3', GetLastError);
    end else begin
      if ss.dwCurrentState = SERVICE_RUNNING then begin
        Result:=ERROR_SUCCESS;
      end;
    end;

    // Disconnect
    if hService <> 0 then
       CloseServiceHandle( hService );

    if hSCM <> 0 then
       CloseServiceHandle( hSCM );
  end;
end;

function TMainForm.MyStopService(Machine, Service: string; StopDependencies: boolean;
  Timeout: dword) : integer;
var
  i, intBytesNeeded, intCount : dword;
  dwStartTime : dword;
  hSCM : SC_HANDLE;
  hService, hDepService : SC_HANDLE;
  ss : TServiceStatus;

  lpDependencies : PEnumServiceStatus;
  ess : TEnumServiceStatus;
begin
  hSCM:=0;
  hService:=0;
  hDepService:=0;
  Result:=0;

  try
    // Open the SCM database
    hSCM := OpenSCManager(pchar(Machine), NIL, SC_MANAGER_CONNECT);
    if hSCM = 0 then begin
      Result:=GetLastError;
      SysErr('OpenSCManager', GetLastError);
      exit;
    end;

    // Open the specified service
    hService := OpenService(hSCM, pchar(Service), SERVICE_STOP +
      SERVICE_QUERY_STATUS + SERVICE_ENUMERATE_DEPENDENTS);
    if hService = 0 then begin
      Result:=GetLastError;
      SysErr('OpenService', GetLastError);
      exit;
    end;

    dwStartTime:=GetTickCount;

    // Make sure the service is not already stopped
    if not QueryServiceStatus(hService, ss) then begin
      Result:=GetLastError;
      SysErr('QueryServieStatus', GetLastError);
      exit;
    end;

    if ss.dwCurrentState = SERVICE_STOPPED then begin
      result:=ERROR_SUCCESS;
      exit;
    end;

    // If a stop is pending, just wait for it
    while ( ss.dwCurrentState = SERVICE_STOP_PENDING ) do begin
      sleep(ss.dwWaitHint);
      if not QueryServiceStatus(hService, ss) then begin
        Result:=GetLastError;
        SysErr('QueryServiceStatus 2', GetLastError);
        exit;
      end;
      if (ss.dwCurrentState = SERVICE_STOPPED) then begin
        Result:=ERROR_SUCCESS;
        exit;
      end;

      if (GetTickCount - dwStartTime) > Timeout then begin
        Result:=ERROR_TIMEOUT;
        exit;
      end;
    end;

    // Stop dependencies?
    if StopDependencies then begin

      lpDependencies:=nil;

      // Pass a zero-length buffer to get the required buffer size
      if EnumDependentServices(hService, SERVICE_ACTIVE,
        lpDependencies^, 0, intBytesNeeded, intCount) then begin
          // If the Enum call succeeds, then there are no dependent
          // services so do nothing
      end else begin
        if GetLastError <> ERROR_MORE_DATA then begin
          Result:=GetLastError;
          SysErr('EnumDependencies', GetLastError);
          exit;
        end;

        // Allocate a buffer for the dependencies
        // 8 = HEAP_ZERO_MEMORY
        lpDependencies := PEnumServiceStatus(HeapAlloc(GetProcessHeap(), 8,
          intBytesNeeded));

        if lpDependencies = nil then begin
          Result:=GetLastError;
          SysErr('EnumDependencies', GetLastError);
          exit;
        end;

        try
          // Enumerate the dependencies
          if not EnumDependentServices( hService, SERVICE_ACTIVE,
                lpDependencies^, intBytesNeeded, intBytesNeeded,
                intCount) then begin
            Result:=GetLastError;
            SysErr('EnumDependencies', GetLastError);
            exit;
          end;

          for i:=0 to intCount-1 do begin
            ess := PEnumServiceStatus(pointer(pchar(lpDependencies) +(i*sizeof(ess)) ))^;
            // Open the service
            hDepService := OpenService( hSCM, ess.lpServiceName,
                  SERVICE_STOP + SERVICE_QUERY_STATUS);
            if hDepService = 0 then
              Result:=GetLastError;
              SysErr('OpenService 2', GetLastError);
              exit;
            end;

            try
              // Send a stop code
              if not ControlService( hDepService, SERVICE_CONTROL_STOP, ss) then begin
                Result:=GetLastError;
                SysErr('Send stop code', GetLastError);
                exit;
              end;

              // Wait for the service to stop
              while ss.dwCurrentState <> SERVICE_STOPPED do begin

                sleep(ss.dwWaitHint);
                if not QueryServiceStatus( hDepService, ss ) then begin
                  Result:=GetLastError;
                  SysErr('QueryServiceStatus 3', GetLastError);
                  exit;
                end;

                if ss.dwCurrentState = SERVICE_STOPPED then
                  break;

                if (GetTickCount - dwStartTime) > Timeout then begin
                  Result:=ERROR_TIMEOUT;
                  exit;
                end;
              end;

            finally
              // Always release the service handle
              CloseServiceHandle(hDepService);
            end;
        finally
          // Always free the enumeration buffer
          HeapFree(GetProcessHeap(), 0, lpDependencies);
        end;
      end;
    end;

    // Send a stop code to the main service
    if not ControlService( hService, SERVICE_CONTROL_STOP, ss) then begin
      Result:=GetLastError;
      SysErr('Send stop code 2', GetLastError);
      exit;
    end;

    // Wait for the service to stop
    while ss.dwCurrentState <> SERVICE_STOPPED do begin

      sleep(ss.dwWaitHint);
      if not QueryServiceStatus( hService, ss ) then begin
        Result:=GetLastError;
        SysErr('QueryServiceStatus 4', GetLastError);
        exit;
      end;

      if ss.dwCurrentState = SERVICE_STOPPED then
        break;

      if (GetTickCount - dwStartTime) > Timeout then begin
        Result:=ERROR_TIMEOUT;
        exit;
      end;
    end;

    Result:=ERROR_SUCCESS;

  finally
    // Disconnect
    if Result = ERROR_TIMEOUT then
      SysErr('ERROR TIMEOUT', Result);
    if hService <> 0 then
       CloseServiceHandle( hService );

    if hSCM <> 0 then
       CloseServiceHandle( hSCM );
  end;
end;
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question