Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

WWW Service Running?

Posted on 2000-02-26
4
Medium Priority
?
155 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 1200 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

609 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