Windows Service issue

Go To StackoverFlow.com

-2

I am trying to get windows service working using c++. The service doesnt do anything special @ the moment. The code that the service runs is

int main(int argc, char** argv) {

    if (argc != 1) return -1;

    ofstream fTestFile;
    fTestFile.open("C:\\ABC\\ServiceTest.txt", ios::app);

    fTestFile << "argc=" << argc << endl;
    for (int i=0;i <argc;i++)
        fTestFile << "argv " << i << "=: " <<argv[i] << endl;

    for (int i=0; i<100000; i++) {
        fTestFile << i << ",";
        if (i % 50 == 0) fTestFile << "\n";
        Sleep(10);
    }
    fTestFile << "\n";
    return 0;
}

When I click "Start" via services.msc on this service, the service tries to start but fails with (1053 error Taking too long to respond). In the servicetest.txt file I see some data, like I see the debug statements and I see the numbers till 2663 or something.

Is there a step that I am missing, any help greatly appreciated.

Thank You

2012-04-04 01:49
by ababeel
Sorry, I meant error 1053 not 1503 - ababeel 2012-04-04 01:52
you can actually edit this question and fix the mistake you've made - Jasonw 2012-04-04 02:00
There's more to a service than just doing stuff in your main. You have to register a service control function and communicate with the service control manager. The error message you seeing is from the service control manager, who is saying "I ran the program but it's not responding to my messages. - Raymond Chen 2012-04-04 02:16
-1 for not reading up beforehand - Seva Alekseyev 2012-04-04 02:21
@SevaAlekseyev: since OP can start the service from the service control manager, the service is registered. One would think at least some reading was done to get to that point - André Caron 2012-04-04 03:18
@SevaAlekseyev, I have read up on setting up the service etc, if I am able to start the service via services.msc, doesnt that mean that I have registered the service, initialized it etc. Maybe I needed to be more descriptive regarding what I have already done. If that's what I got a -1 for, if not could you please elaborate a little more. Thank - ababeel 2012-04-04 03:34
OK, let's open the relevant MSDN doc. The very first article that deals with service coding is "Service Entry Point". The first paragraph states that services typically have main() as the entry point. The second paragraph says: "When the SCM starts a service program, it waits for it to call the StartServiceCtrlDispatcher function." So to me, your question looks as if you read the first paragraph, did not read the second one, and started coding - Seva Alekseyev 2012-04-04 15:22


0

Your service needs to communicate with the service manager (services.exe) to report and update it's current status.

Read here http://msdn.microsoft.com/en-us/library/windows/desktop/ms687414%28v=vs.85%29.aspx

2012-04-04 02:15
by everdox
There's more than just the ServiceMain() function, you have to have the main() function initialize the service and start execution - André Caron 2012-04-04 02:20
Ads