restart a window service programmatically

Can anyone guide me or give me a sample code to restart a window service automatically i mean restart through programming.
Call ExitProcess() :)
Well this is annoying, you would think that "ControlService()" would have a SERVICE_CONTROL_RESTART command but it doesn't. It looks like you need to take the long way around:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>

#include <windows.h>
#include <winsvc.h>

void pause()
{
    std::cin.sync();
    std::cin.ignore();
}

 
int main() 
{
    std::string Service = "Alerter"; //This Is The Name Of Your Service
    SERVICE_STATUS Status;
    
    SC_HANDLE SCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
    SC_HANDLE SHandle = OpenService(SCManager, Service.c_str(), SC_MANAGER_ALL_ACCESS);
    
    if(SHandle == NULL)
    {
        std::cout << "ERROR " << GetLastError() << std::endl;
    }
    
    if(!ControlService(SHandle, SERVICE_CONTROL_STOP, &Status))
    {
        std::cout << "FAILED TO SEND STOP SERVICE COMMAND: " << GetLastError();
    }
    else
    {
        std::cout << "Service Stop Command Sent\n";
    }
    
    do
    {
        QueryServiceStatus(SHandle, &Status);
        std::cout << "Checking Service Status...\n";
    }while(Status.dwCurrentState != SERVICE_STOPPED);
    
    
    if(!StartService(SHandle, 0, NULL))
    {
        std::cout << "Service Did Not Start Up Again: " << GetLastError() << std::endl;
    }
    else
    {
        std::cout << "Service Started\n";
    }
    
    pause();
    
    CloseServiceHandle(SCManager);
    CloseServiceHandle(SHandle);
    
    return 0;
}


You will also need to link to "advapi32.lib" from the MS SDK.

FUNCTIONS:
OpenSCManager(): http://msdn.microsoft.com/en-us/library/windows/desktop/ms684323(v=vs.85).aspx

OpenService(): http://msdn.microsoft.com/en-us/library/windows/desktop/ms684330(v=vs.85).aspx

ControlService(): http://msdn.microsoft.com/en-us/library/windows/desktop/ms682108(v=vs.85).aspx

StartService(): http://msdn.microsoft.com/en-us/library/windows/desktop/ms686321(v=vs.85).aspx

QueryServiceStatus(): http://msdn.microsoft.com/en-us/library/windows/desktop/ms684939(v=vs.85).aspx

STRUCTURES:
SERVICE_STATUS: http://msdn.microsoft.com/en-us/library/windows/desktop/ms685996(v=vs.85).aspx

Don't know why I did all of this, I'm just really hyper today and can't really leave my desk.
This code requires a second service running to restart the main service (or directly from main application, if it is running with administrative privileges).
@modoran: No it doesn't. I checked it half a dozen times, it runs as an executable off the desktop. You WILL need admin\elevated privileges but I would consider that obvious when you are working with services. I've tried this on XP and Win7 Pro, is there a platform that you've tried this on where it doesn't work?
I didn't say it didn't work, I only say that requires administrative privileges, which can be inconvenient sometimes. Read my post carefully:
or directly from main application, if it is running with administrative privileges


By "main application" I mean desktop executable, suppose that service is part of a larger program.

You WILL need admin\elevated privileges but I would consider that obvious when you are working with services.

Actually there IS a way to command a service do some work WITHOUT admin privileges, but not stopping/starting it, passing a user defined value to handler callback function registered with RegisterServiceCtrlHandler(), that's why I suggest a second service approach.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms683240(v=vs.85).aspx
Topic archived. No new replies allowed.