Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Stop and start IIS with Delphi?

Posted on 2001-09-13
5
Medium Priority
?
465 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 300 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Loops Section Overview
Suggested Courses

783 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