Solved

WWW Service Running?

Posted on 2000-02-26
4
152 Views
Last Modified: 2010-04-02
How do I check, using a C++ program, to see if IIS 4 is running on NT or Peer Web Services is running on Win95?  If it is running, how do I shut it down?
0
Comment
Question by:paulca
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 300 total points
ID: 2561366
See

 HOWTO: Enumerate Applications in Win32
 http://support.microsoft.com/support/kb/articles/Q175/0/30.ASP  

All you need to know is the .exe name of the process you want to find (inetsvc.exe on NT).

The problem is how to shut down the process on NT - NT services run in a provileged environment and therefor cannot just be terminated.

Use

void StopService()
{
    SC_HANDLE   schService;
    SC_HANDLE   schSCManager;

    schSCManager = OpenSCManager(
                        NULL,                   // machine (NULL == local)
                        NULL,                   // database (NULL == default)
                        SC_MANAGER_ALL_ACCESS   // access required
                        );
    if ( schSCManager )
    {
        schService = OpenService(schSCManager, TEXT(SZSERVICENAME), SERVICE_ALL_ACCESS);

        if (schService)
        {
            // try to stop the service
            if ( ControlService( schService, SERVICE_CONTROL_STOP, &ssStatus ) )
            {
                _tprintf(TEXT("Stopping %s."), TEXT(SZSERVICEDISPLAYNAME));
                Sleep( 1000 );

                while( QueryServiceStatus( schService, &ssStatus ) )
                {
                    if ( ssStatus.dwCurrentState == SERVICE_STOP_PENDING )
                    {
                        _tprintf(TEXT("."));
                        Sleep( 1000 );
                    }
                    else
                        break;
                }

                if ( ssStatus.dwCurrentState == SERVICE_STOPPED )
                    _tprintf(TEXT("\n%s stopped.\n"), TEXT(SZSERVICEDISPLAYNAME) );
                else
                    _tprintf(TEXT("\n%s failed to stop.\n"), TEXT(SZSERVICEDISPLAYNAME) );

            }


            CloseServiceHandle(schService);
        }
        else
            _tprintf(TEXT("OpenService failed - %s\n"), GetLastErrorText(szErr,256));

        CloseServiceHandle(schSCManager);
    }
    else
        _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(szErr,256));
}

Where 'SZSERVICENAME' is the internal service name (e.g. 'W3SVC') and 'SZSERVICEDISPLAYNAME' is the display name (e.g. 'WWW Publishing Service').


On Win9x, you could simply use the process ID retrieved with the code from the MS sample in a call to 'OpenProcess()', which returns a handle that can be used to call 'TerminateProcess()'


Feel free to ask if you need more information!
0
 
LVL 86

Expert Comment

by:jkr
ID: 2561382
BTW: On NT, 'QueryServiceStatus()' would be sufficiant to find out whether W3SVC is running, e.g.:

BOOL IsServiceRunning ( LPCTSTR szServiceName)
{
    BOOL bRC = FALSE;

    SC_HANDLE   schService;
    SC_HANDLE   schSCManager;
    SERVICE_STATUS ssStatus;

    schSCManager = OpenSCManager(
                        NULL,                   // machine (NULL == local)
                        NULL,                   // database (NULL == default)
                        SC_MANAGER_ALL_ACCESS   // access required
                        );
    if ( schSCManager )
    {
        schService = OpenService(schSCManager, szServiceName), SERVICE_ALL_ACCESS);

        if (schService)
        {
                if( QueryServiceStatus( schService, &ssStatus ) )
                {
                    if ( ssStatus.dwCurrentState == SERVICE_RUNNING )
                    {
                       bRC = TRUE;
                    }
                }

            CloseServiceHandle(schService);
        }
        else
            _tprintf(TEXT("OpenService failed - %s\n"), GetLastErrorText(szErr,256));

        CloseServiceHandle(schSCManager);
    }
    else
        _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(szErr,256));

 return ( bRC);
}
0
 

Author Comment

by:paulca
ID: 2561391
What about Win95/98?  Services do not exist.
0
 
LVL 86

Expert Comment

by:jkr
ID: 2561444
As I stated above (sorry for not emphasizing this):

On Win9x, you could simply use the process ID retrieved with the code from the MS sample in a call to 'OpenProcess()', which returns a handle that can be used to call 'TerminateProcess()'  

This is not as clean as on NT, but the only method...
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
Starting a function in a c++ application with a TCP command from C# 4 89
Writing a parser for java language 4 100
Dialogbox API leak? 18 114
Create a path if not exists 7 119
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

740 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