Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my VC++, I completed to write a code of windows console. But now, I must convert it to a windows service code.

So I have changed some code in that console program like as follows

C++
#include <winsvc.h>

int main()
{
    SERVICE_TABLE_ENTRY ste[] = {
       {_T("TEST Win"}, (LPSERVICE_MAIN_FUNCTION) MyTestWindows},
    };

    StartServiceCtrlDisplatcher(ste);

    return 0;
}

and in MyTestWindows(), I coded as follows.

C++
void MyTestWindows()
{
    g_hSrv = RegisterServiceCtrlHandler(_T("TestWin"),
                        LPHANDLER_FUNCTION)MyServiceHandler);
    if (g_hSrv == 0) return;

    MyServiceSet(SERVICE_START_PENDING);
    g_bPause = FALSE;

    g_ExitEvent = CreateEvent(NULL, TRUE, FALSE, _T("TESTWINSTATUS");

    MyServiceSet(SERVICE_RUNNING);

    // This is same of windows console program...

    MyServiceSet(SERVICE_STOPPED);
    return;
}


But when I run this program, No thread or windows service are running. It's silent ....?
Please let me know what is my fault of this coding and solution.

Thank you in advance.

What I have tried:

More 2 days wasted for this problem.
Posted
Updated 8-Nov-17 21:24pm
v2
Comments
Suvendu Shekhar Giri 8-Nov-17 23:51pm    
Any luck with debugging?

1 solution

You did not show us the worker thread creation and function and the MyServiceSet and MyServiceHandler implementations. So we can't locate problems within those function.

MyServiceSet(SERVICE_RUNNING);
 
// This is same of windows console program...
 
MyServiceSet(SERVICE_STOPPED);
A service usually creates a thread there instead:
C++
MyServiceSet(SERVICE_RUNNING);
HANDLE hThread = CreateThread (NULL, 0, ServiceWorkerThread, NULL, 0, NULL);
WaitForSingleObject(hThread, INFINITE);
MyServiceSet(SERVICE_STOPPED);
DWORD WINAPI ServiceWorkerThread(LPVOID lpParam)
{
    while (WaitForSingleObject(g_ExitEvent, SOME_TIMEOUT) != WAIT_OBJECT_0)
    {
        // Code from your console program
    }
    return 0;
}
g_ExitEvent has to be set by the MyServiceHandler upon SERVICE_CONTROL_STOP.

Finally, just to be sure: Have you installed your service?
 
Share this answer
 
Comments
Member 12330615 10-Nov-17 0:46am    
"Have you installed your service?"
I am sorry I don't understand this question mean....
Jochen Arndt 10-Nov-17 2:59am    

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900