Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / September 2005

Tip: Looking for answers? Try searching our database.

Call SetServiceStatus( , SERVICE_STOPPED) in ServiceMain()

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
lauch2 - 28 Sep 2005 05:21 GMT
I am writing a service. Before exiting the ServiceMain(), I want to call
SetServiceStatus(hServiceHandle, SERVICE_STOPPED) to set the service status
to SERVICE_STOPPED, but the SetServiceStatus() function crash immediately.
For example:

ServiceMain(...)
{
 SERVICE_STATUS          MyServiceStatus;
 SERVICE_STATUS_HANDLE   MyServiceStatusHandle;

 ...

 MyServiceStatus.dwCurrentState       = SERVICE_STOPPED;
 MyServiceStatus. ... (other values)
 SetServiceStatus(MyServiceStatusHandle, &MyServiceStatus); // this line
crash
 MyLog("ServiceMain Existed"); // this line never reach.
}

Any idea? or I don't need to call SetServiceStatus(hServiceHandle,
SERVICE_STOPPED) to set the service status to SERVICE_STOPPED in the
ServiceMina()?

Thanks in advance,
lauch2.
Egbert Nierop (MVP for IIS) - 28 Sep 2005 09:18 GMT
>I am writing a service. Before exiting the ServiceMain(), I want to call
> SetServiceStatus(hServiceHandle, SERVICE_STOPPED) to set the service
[quoted text clipped - 15 lines]
>  MyLog("ServiceMain Existed"); // this line never reach.
> }

This might help.

//
//  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 ( !bDebug ) // when debugging we don't report to the SCM
  {
     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(L"SetServiceStatus");
     }
  }
  return fResult;
}

void WINAPI service_main(DWORD dwArgc, PWSTR *lpszArgv)
{

  // register our service control handler:
  //
  sshStatusHandle = RegisterServiceCtrlHandler( SZSERVICENAME,
service_ctrl);

  if (sshStatusHandle != NULL)
  {

 // SERVICE_STATUS members that don't change in example
 //
 ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
 ssStatus.dwServiceSpecificExitCode = 0;

 // report the status to the service control manager.
 //
 if (ReportStatusToSCMgr(
       SERVICE_START_PENDING, // service state
       NO_ERROR,              // exit code
       3000))                 // wait hint
  ServiceStart( dwArgc, lpszArgv );

 // try to report the stopped status to the service control manager.
 //

  ReportStatusToSCMgr(SERVICE_STOPPED, dwErr,0);
  }
  return;
}

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.