Solved

WWW Service Running?

Posted on 2000-02-26
4
153 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

707 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