Solved

My Service crashes in XP machine and not in other OS

Posted on 2004-09-16
5
205 Views
Last Modified: 2010-04-15
Requirement : I need to run a service in a XP machine from a remote windows (NT/2K/XP) machine using C program.
                     The service exe in the XP machine crashes after execution.

Description : I have "test.exe" ,a service exe , that calls "StartServiceCtrlDispatcher()" method. This exe is put in a specific location in
a XP Machine. I have another C program that will install (a service named 'Test') and run the service ('Test'), This C program will reside in a remote machine (NT/XP/2K). On running the service (using the C Program) test.exe will be exectuted.

The service in XP Machine is created using the 'local account'  (by passing username and password as NULL for CreateService() method).
When i run the service using StartService() method, the test.exe runs in the remote machine and crashes at the end.

What may be the reason that it crashes.???!!!

Hint  1 : This (crash) happens only when the test.exe is run in the XP machine. It works fine when run in any other Windows OS.
Hint  2 : after  debugging the C program (that installs/ runs the service), and putting some logs in the test.exe, i came to know that
             the crash happenned only after the COMPLETE execution of test.exe in the remote (XP machine). i.e, i was able to print a log at the
             last line of test.exe.

Does any one have an idea of where the problem could be??

-SRK
0
Comment
Question by:srk_karthik
5 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 12074528

Services typically run "forever" or until specifically stopped by another process (such as "Manage Services").

Does Test.exe tell the host O/S that it is terminating and will no longer be a running service?

Kent
0
 

Author Comment

by:srk_karthik
ID: 12081412
Yes, the following code in test.exe does that :

------------------------------------------------------------
SERVICE_STATUS          ServiceStatus;
SERVICE_STATUS_HANDLE   hStatus;

void
ControlHandler(DWORD request) {
   switch(request)
   {
      case SERVICE_CONTROL_STOP:
      case SERVICE_CONTROL_SHUTDOWN:
         ServiceStatus.dwWin32ExitCode = 0;
         ServiceStatus.dwCurrentState = SERVICE_STOPPED;
         SetServiceStatus (hStatus, &ServiceStatus);
         return;        
      default:
         break;
    }  
    SetServiceStatus (hStatus, &ServiceStatus);
    return;
}
------------------------------------------------------------

The above  ControlHandler(DWORD) method
is registered using RegisterServiceCtrlHandler()

-SRK
0
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 500 total points
ID: 12082214
Heres some code in a service I work with that stops itself. I didnt write it but it may be of help.

 //
//  FUNCTION: service_ctrl
//
//  PURPOSE: This function is called by the SCM whenever
//           ControlService() is called on this service.
//
//  PARAMETERS:
//    dwCtrlCode - type of control requested
//
//  RETURN VALUE:
//    none
//
//  COMMENTS:
//
VOID WINAPI service_ctrl(DWORD dwCtrlCode)
{
    // Handle the requested control code.
    //
    switch(dwCtrlCode)
    {
        // Stop the service.
        //
        // SERVICE_STOP_PENDING should be reported before
        // setting the Stop Event - hServerStopEvent - in
        // ServiceStop().  This avoids a race condition
        // which may result in a 1053 - The Service did not respond...
        // error.
        case SERVICE_CONTROL_STOP:
            ReportStatusToSCMgr(SERVICE_STOP_PENDING, NO_ERROR, 0);
            ServiceStop();
            return;

        // Update the service status.
        //
        case SERVICE_CONTROL_INTERROGATE:
            break;

        // invalid control code
        //
        default:
            break;

    }

    ReportStatusToSCMgr(ssStatus.dwCurrentState, NO_ERROR, 0);
}


//
//  FUNCTION: ReportStatusToSCMgr()
//
//  PURPOSE: Sets the current status of the service and
//           reports it to the Service Control Manager
//
//  PARAMETERS:
//    dwCurrentState - the state of the service
//    dwWin32ExitCode - error code to report
//    dwWaitHint - worst case estimate to next checkpoint
//
//  RETURN VALUE:
//    TRUE  - success
//    FALSE - failure
//
//  COMMENTS:
//
BOOL ReportStatusToSCMgr(DWORD dwCurrentState,
                         DWORD dwWin32ExitCode,
                         DWORD dwWaitHint)
{
    static DWORD dwCheckPoint = 1;
    BOOL fResult = TRUE;


    if (dwCurrentState == SERVICE_START_PENDING)
        ssStatus.dwControlsAccepted = 0;
    else
        ssStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;

    ssStatus.dwCurrentState = dwCurrentState;
    ssStatus.dwWin32ExitCode = dwWin32ExitCode;
    ssStatus.dwWaitHint = dwWaitHint;

    if ( ( dwCurrentState == SERVICE_RUNNING ) ||
         ( dwCurrentState == SERVICE_STOPPED ) )
        ssStatus.dwCheckPoint = 0;
    else
        ssStatus.dwCheckPoint = dwCheckPoint++;


    // Report the status of the service to the service control manager.
    //
    if (!(fResult = SetServiceStatus( sshStatusHandle, &ssStatus))) {
        AddToMessageLog(3, "SetServiceStatus failed.");
    }

    return fResult;
}


This is a more gentle approach because we hold a NetBios channel open while we are running and we have to wait 'till its closed before closing.

Paul
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Converting a float to a string function in c languange 10 207
How to set environment variables in C 2 84
Passing command line arguments in C 10 92
sameEnds challenge 3 161
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question