Solved

Stop and start IIS with Delphi?

Posted on 2001-09-13
5
427 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:mnasman
Comment Utility
take a look at this link, it's for delphi and IIS
http://www.fulgan.com/delphi
0
 
LVL 10

Expert Comment

by:Jacco
Comment Utility
On the local machine or on a remote one?

Regards Jacco
0
 

Author Comment

by:faustomen
Comment Utility
On a remote server.
0
 
LVL 12

Expert Comment

by:Lee_Nover
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now