Solved

Programming Services in C++

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Assigning default value to structure in C for mutithread application 17 55
Connecting database in Xamarin? 6 51
Angular JS Route 3 50
Reset asp.net login password 4 29
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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 and use conditional statements in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

910 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

21 Experts available now in Live!

Get 1:1 Help Now