We help IT Professionals succeed at work.

Programmatically Tell if the Apache Web Service is Running and then Stop It

xassets
xassets asked
on
Medium Priority
552 Views
Last Modified: 2008-02-07
Hi can I tell through API calls if the apache service is running.

Its the usual scenario where users install oracle with all default options and IIS stops working because apache takes over port 80.

So in the installation program I want to detect if the apache service is started and give the user the option of stopping it. If I could call up the service form to change it to manual and stop it that would be OK, or if I can do it from my own form thats OK too.

thanks
Comment
Watch Question

CERTIFIED EXPERT

Commented:
You can use the "NET START service" and "NET STOP service" commands to control a service. The command "NET START" prints all currently running services.
CERTIFIED EXPERT

Commented:
Just found this old question that may provide more information https://www.experts-exchange.com/Web/Web_Servers/IIS/Q_20931314.html
CERTIFIED EXPERT
Top Expert 2012
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
jkr, thats it.

I knew you'd know. big thanks

Author

Commented:
One more question if you can jkr

How can I change the service start up from "Automatic" to "Manual" programmatically

(so it doesn't come back at the next reboot)
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
You can do that using 'QueryServiceConfig()' and 'ChangeServiceConfig()' - see also http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/changing_a_service_configuration.asp ("Changing a Service Configuration"), the code sample on that page pretty much does what you want:

VOID ReconfigureSampleService(BOOL fDisable, LPSTR lpDesc)
{
    SC_LOCK sclLock;
    LPQUERY_SERVICE_LOCK_STATUS lpqslsBuf;
    SERVICE_DESCRIPTION sdBuf;
    DWORD dwBytesNeeded, dwStartType;
 
    // Need to acquire database lock before reconfiguring.
 
    sclLock = LockServiceDatabase(schSCManager);
 
    // If the database cannot be locked, report the details.
 
    if (sclLock == NULL)
    {
        // Exit if the database is not locked by another process.
 
        if (GetLastError() != ERROR_SERVICE_DATABASE_LOCKED)
            MyErrorExit("LockServiceDatabase");
 
        // Allocate a buffer to get details about the lock.
 
        lpqslsBuf = (LPQUERY_SERVICE_LOCK_STATUS) LocalAlloc(
            LPTR, sizeof(QUERY_SERVICE_LOCK_STATUS)+256);
        if (lpqslsBuf == NULL)
            MyErrorExit("LocalAlloc");
 
        // Get and print the lock status information.
 
        if (!QueryServiceLockStatus(
            schSCManager,
            lpqslsBuf,
            sizeof(QUERY_SERVICE_LOCK_STATUS)+256,
            &dwBytesNeeded) )
            MyErrorExit("QueryServiceLockStatus");
 
        if (lpqslsBuf->fIsLocked)
            printf("Locked by: %s, duration: %d seconds\n",
                lpqslsBuf->lpLockOwner,
                lpqslsBuf->dwLockDuration);
        else
            printf("No longer locked\n");
 
        LocalFree(lpqslsBuf);
        MyErrorExit("Could not lock database");
    }
 
    // The database is locked, so it is safe to make changes.
 
    // Open a handle to the service.
 
    schService = OpenService(
        schSCManager,           // SCManager database
        "Sample_Srv",           // name of service
        SERVICE_CHANGE_CONFIG); // need CHANGE access
    if (schService == NULL)
        MyErrorExit("OpenService");
 
    dwStartType = (fDisable) ? SERVICE_DISABLED :
                            SERVICE_DEMAND_START;
 
    // Make the changes.

    if (! ChangeServiceConfig(
        schService,        // handle of service
        SERVICE_NO_CHANGE, // service type: no change
        dwStartType,       // change service start type
        SERVICE_NO_CHANGE, // error control: no change
        NULL,              // binary path: no change
        NULL,              // load order group: no change
        NULL,              // tag ID: no change
        NULL,              // dependencies: no change
        NULL,              // account name: no change
        NULL,              // password: no change
        NULL) )            // display name: no change
    {
        MyErrorExit("ChangeServiceConfig");
    }
    else
        printf("ChangeServiceConfig SUCCESS\n");
 
    sdBuf.lpDescription = lpDesc;

    if( !ChangeServiceConfig2(
        schService,                 // handle to service
        SERVICE_CONFIG_DESCRIPTION, // change: description
        &sdBuf) )                   // value: new description
    {
        MyErrorExit("ChangeServiceConfig2");
    }
    else
        printf("ChangeServiceConfig2 SUCCESS\n");

    // Release the database lock.
 
    UnlockServiceDatabase(sclLock);
 
    // Close the handle to the service.
 
    CloseServiceHandle(schService);
}

Author

Commented:
Excellent

thanks once again
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.