Solved

Programming Services in C++

Posted on 2009-05-14
4
259 Views
Last Modified: 2012-05-07
I need a way to change the service parameters like SERVICE_EXIT_CODE value to 2 other than 0.

Plz see the description below.

In command prompt by typing
sc query themes

Displays:
SERVICE_NAME: themes
        TYPE               : 20  WIN32_SHARE_PROCESS
        STATE              : 4  RUNNING
                                (STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

I want to change these parameters(ex: WIN32_EXIT_CODE value to -223 or any other value).
FYI: (There is a structure SERVICE_STATUS which takes care of these values.)
I need a program in C++  to achieve it.
0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24393087
The service exit code is the return value of an service when terminating. The service can set it to the SERVICE_STATUS structure associated with the service by calling SetServiceStatus passing the handle to the updated structure. If you would write 2 or -223 to the dwCurrentState member of that structure, the Service Control Manager surely would reflect that status.
0
 
LVL 9

Author Comment

by:Subrat (C++ windows/Linux)
ID: 24393338
Can u plz send me the code to do that.
Thanks
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 24393937
Below is sample code how a service sets its current status.  

The full sample can be found at http://support.microsoft.com/kb/201349/en-us

If your sservice stops or was stopped by SCM (Service Control Manager) you would set your status to the code you want. Try to not match to status code which have a different meaning, e. g. SERVICE_START_PENDING

Here are the codes for predefined status values

//
// Service State -- for CurrentState
//
#define SERVICE_STOPPED                0x00000001
#define SERVICE_START_PENDING          0x00000002
#define SERVICE_STOP_PENDING           0x00000003
#define SERVICE_RUNNING                0x00000004
#define SERVICE_CONTINUE_PENDING       0x00000005
#define SERVICE_PAUSE_PENDING          0x00000006
#define SERVICE_PAUSED                 0x00000007


      // Register the ServiceHandler() function to receive control requests.
      hService = RegisterServiceCtrlHandler( SERVICE_NAME,
            ServiceHandler );
 
      // Set service status to start pending.
      ss.dwCurrentState = SERVICE_START_PENDING;
      ss.dwCheckPoint = 1; 
      ss.dwWaitHint = 500;
      SetServiceStatus( hService, &ss );
 
      // TODO: add additional initialization code here.
 
      // set service status to running
      ss.dwCurrentState = SERVICE_RUNNING;
      ss.dwCheckPoint = ss.dwWaitHint = 0;   
      SetServiceStatus( hService, &ss );

Open in new window

0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

724 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