Click here to Skip to main content
15,921,452 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Derive Class from CTime Pin
e-DJ1-Mar-07 4:19
e-DJ1-Mar-07 4:19 
AnswerRe: Derive Class from CTime Pin
Nibu babu thomas28-Feb-07 22:55
Nibu babu thomas28-Feb-07 22:55 
GeneralRe: Derive Class from CTime Pin
e-DJ28-Feb-07 23:48
e-DJ28-Feb-07 23:48 
AnswerRe: Derive Class from CTime Pin
David Crow1-Mar-07 2:39
David Crow1-Mar-07 2:39 
QuestionWin Service problem Pin
david bagaturia28-Feb-07 21:38
david bagaturia28-Feb-07 21:38 
AnswerRe: Win Service problem Pin
JudyL_MD1-Mar-07 2:44
JudyL_MD1-Mar-07 2:44 
GeneralRe: Win Service problem Pin
david bagaturia1-Mar-07 3:11
david bagaturia1-Mar-07 3:11 
GeneralRe: Win Service problem Pin
JudyL_MD1-Mar-07 3:45
JudyL_MD1-Mar-07 3:45 
I'm asuming when you say "windows service 1998" that you are refering to Windows 98. I've never even tried to use one under anything earlier the NT4. Also, the MSDN documentation for the service control functions (both those from within the service like StartServiceCtrlDispatcher and those used by the calling program such as StartService) states they are NT4 or higher also. Looks like 98 is out.

Services are not the easiest things to debug. You just can't "start" them in the debugger. I typically put an ASSERT(false) in the very beginning of my _main function to force an exception and then begin debugging from there, putting break points into my ServiceMain function.

From your earlier post, it looks like you haven't done much with services before. Here is a basic outline of what one should look like. Note that I've omitted the error checking.

SERVICE_STATUS_HANDLE glb_sshStatusHandle;
SERVICE_STATUS        glb_ssStatus;

VOID WINAPI MMServiceMain (DWORD dwArgc, LPTSTR *lpszArgv);

DWORD WINAPI MMServiceControl (DWORD dwCtrlCode, DWORD dwEventType,
                               LPVOID lpEventData, LPVOID lpContext);

int _tmain (int argc, TCHAR* argv[], TCHAR* envp[])
{
    SERVICE_TABLE_ENTRY dispatchTable[] = {
        { "MMService", (LPSERVICE_MAIN_FUNCTION) MMServiceMain},
        { NULL, NULL} };

    StartServiceCtrlDispatcher (dispatchTable);
    glb_ssStatus.dwCheckPoint = 0;
    return 0;
}

void WINAPI MMServiceMain (DWORD dwArgc, LPTSTR *lpszArgv)
{
    // entries that never change
    glb_ssStatus.dwServiceType             = SERVICE_WIN32_OWN_PROCESS;
    glb_ssStatus.dwServiceSpecificExitCode = 0;

    // register our service control handler
    glb_sshStatusHandle = RegisterServiceCtrlHandlerEx (
                 "MMService", MMServiceControl, NULL);

    // report the status to the service control manager
    glb_ssStatus.dwControlsAccepted = 0;
    glb_ssStatus.dwCurrentState     = SERVICE_START_PENDING;
    glb_ssStatus.dwWin32ExitCode    = NO_ERROR;
    glb_ssStatus.dwWaitHint         = 5000;
    glb_ssStatus.dwCheckPoint       = glb_ssStatus.dwCheckPoint++;
    SetServiceStatus (glb_sshStatusHandle, &glb_ssStatus);

    // initialize
    if (!Init ()) {
        glb_ssStatus.dwCurrentState  = SERVICE_STOPPED;
        glb_ssStatus.dwWin32ExitCode = NO_ERROR;
        glb_ssStatus.dwWaitHint      = 0;
        glb_ssStatus.dwCheckPoint    = glb_ssStatus.dwCheckPoint++;
        SetServiceStatus (glb_sshStatusHandle, &glb_ssStatus);
        return;
    }

    // report that we are up and running
    glb_ssStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
    glb_ssStatus.dwCurrentState     = SERVICE_RUNNING;
    glb_ssStatus.dwWin32ExitCode    = NO_ERROR;
    glb_ssStatus.dwWaitHint         = 0;
    glb_ssStatus.dwCheckPoint       = glb_ssStatus.dwCheckPoint++;
    SetServiceStatus (glb_sshStatusHandle, &glb_ssStatus);

    // Main loop for the service
    Run ();

    // all done
    glb_ssStatus.dwCurrentState  = SERVICE_STOPPED;
    glb_ssStatus.dwWin32ExitCode = NO_ERROR;
    glb_ssStatus.dwWaitHint      = 0;
    glb_ssStatus.dwCheckPoint    = glb_ssStatus.dwCheckPoint++;
    SetServiceStatus (glb_sshStatusHandle, &glb_ssStatus);
    return;
}

DWORD WINAPI MMServiceControl (DWORD dwCtrlCode, DWORD dwEventType,
                               LPVOID lpEventData, LPVOID lpContext)
{
    if (dwCtrlCode == SERVICE_CONTROL_STOP) {
        glb_ssStatus.dwCurrentState  = SERVICE_STOP_PENDING;
        glb_ssStatus.dwWin32ExitCode = NO_ERROR;
        glb_ssStatus.dwWaitHint      = 0;
        glb_ssStatus.dwCheckPoint    = glb_ssStatus.dwCheckPoint++;
        SetServiceStatus (glb_sshStatusHandle, &glb_ssStatus);

        SetEvent (hStopEvent);
        return NO_ERROR;
    }
    else
        return ERROR_CALL_NOT_IMPLEMENTED;
}

bool Init ()
{
    hStopEvent = CreateEvent (NULL, TRUE, FALSE, NULL);
    return true;
}

void Run ()
{
    while (WaitForSingleObject (hStopEvent, 1) != WAIT_OBJECT_0)
    {
        // do your work - check the event if it is long processing
    }

    CloseHandle (hStopEvent);
    return;
}

GeneralRe: Win Service problem Pin
david bagaturia1-Mar-07 18:58
david bagaturia1-Mar-07 18:58 
QuestionAbout Driver priority Pin
mt_samiei28-Feb-07 21:32
mt_samiei28-Feb-07 21:32 
Questionhow can i get all computer in the lan. Pin
bios808628-Feb-07 21:31
bios808628-Feb-07 21:31 
AnswerRe: how can i get all computer in the lan. Pin
bios80861-Mar-07 15:17
bios80861-Mar-07 15:17 
QuestionAbout DirectX??? Pin
siddharthsan28-Feb-07 21:18
siddharthsan28-Feb-07 21:18 
AnswerRe: About DirectX??? Pin
Mark Salsbery1-Mar-07 8:00
Mark Salsbery1-Mar-07 8:00 
QuestionNeed to draw background of edit control Pin
Nishad S28-Feb-07 21:16
Nishad S28-Feb-07 21:16 
AnswerRe: Need to draw background of edit control Pin
baerten28-Feb-07 22:41
baerten28-Feb-07 22:41 
GeneralRe: Need to draw background of edit control Pin
Nishad S28-Feb-07 23:39
Nishad S28-Feb-07 23:39 
AnswerRe: Need to draw background of edit control Pin
Hamid_RT28-Feb-07 23:35
Hamid_RT28-Feb-07 23:35 
Questionnew operator Pin
prithaa28-Feb-07 21:07
prithaa28-Feb-07 21:07 
AnswerRe: new operator Pin
Cedric Moonen28-Feb-07 21:16
Cedric Moonen28-Feb-07 21:16 
GeneralRe: new operator Pin
prithaa28-Feb-07 21:56
prithaa28-Feb-07 21:56 
Questionhow to use SetPrinterDataEx() Pin
mo_nica88128-Feb-07 20:58
mo_nica88128-Feb-07 20:58 
AnswerRe: how to use SetPrinterDataEx() Pin
prasad_som28-Feb-07 23:13
prasad_som28-Feb-07 23:13 
Questionnew operator not understood Pin
prithaa28-Feb-07 20:01
prithaa28-Feb-07 20:01 
AnswerRe: new operator not understood Pin
Cedric Moonen28-Feb-07 20:13
Cedric Moonen28-Feb-07 20:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.