Solved

Programming Services in C++

Posted on 2009-05-14
4
241 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
  • 2
4 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
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 8

Author Comment

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

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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 recursion in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now