[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2458
  • Last Modified:

Start/Stop IIS 6 Services Programatically

How to pragramatically start and stop IIS 6 (windows 2003) services in c++, especially the SMTP/NNTP services? Does the ADSI/WMI api have specific functions related to this?
1 Solution


You can use NET STOP [service name] /Y for any service that is running, just replace [service name] with the name of the service, you find the name of the service by going to Services..
bphanikumarAuthor Commented:
But I want to do that programatically using the ADSI/WMI APIs in c++. With IIS5 Microsoft delivers the scripts Startsrv.vbs and Stopsrv.vbs but in IIS6 these scripts are not delivered.

To be more specific, programatically find all the services available for IIS6, their current state and able to start & stop each service...
SvenTech Lead Web-DevelopmentCommented:
I am no C++ programmer, but in VB I programmed a tool which calls the Shell-API and does all work for me. Here are the important parts:

Option Explicit

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Const STILL_ACTIVE = &H103

Private Sub ShellWait(ByVal CommandLine As String, Optional ByVal WindowStyle As VbAppWinStyle = vbHide)
    Dim hProcess As Long
    Dim RetVal As Long
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, Shell(CommandLine, WindowStyle))
        GetExitCodeProcess hProcess, RetVal
        DoEvents: Sleep 100
    Loop While RetVal = STILL_ACTIVE
End Sub

Private Sub Form_Load()

    ShellWait "NET STOP IISADMIN /Y"

End Sub
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

bphanikumarAuthor Commented:
Using WMI it's easy to configure IIS programmatically. WIM Studio gives extensive information about the classes to be used and other details.  But I can't use it for my application. Only ADSI to be used. Most of the sites explain using VBScript examples but rarely with C++/COM.  Like for the simple VBScript code shown below how do u write in C++,

        IIsObjectPath = "IIS://" & MachineName & "/" & ObjectPath
        Set IIsObject = GetObject(IIsObjectPath)

The code is picked from the adsutil.vbs file.

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now