Solved

Programming Services in C++

Posted on 2009-05-14
4
255 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

738 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