Solved

WWW Service Running?

Posted on 2000-02-26
4
148 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
  • 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

757 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

18 Experts available now in Live!

Get 1:1 Help Now