Starting a Service in Windows XP

Environment: VC++ 6.0, Windows XP

Introduction

I was learning how to start a service from a program (VC++) in a Windows XP environment.

Background

Basically, my problem was putting a system tray icon through a DLL that will be run by the winmgmt service in Windows XP. I tried so many ways within my knowledge but failed. So, I tried running it as a service option. It is simple. It just uses a few functions.

Using the Code

I used the sample code available in MSDN, everything relating to starting a service put into a function called StartSampleService(). The core part starts with OpenSCManager.

The OpenSCManager function establishes a connection to the service control manager on the specified computer and opens the specified service control manager database.

SC_HANDLE OpenSCManager(
LPCTSTR lpMachineName,
LPCTSTR lpDatabaseName,
DWORD   dwDesiredAccess
);

This returns a handle of type SC_HANDLE that will be used to open a service.

schService =

OpenService( schSCManager,          // SCM database
"IconService",                      // service name Here I gave
                                    // my sample service, Please
change this SERVICE_ALL_ACCESS);    // use according to your wishes.
                                    // See that the service is not
                                    // running currently.

Then next call is to StartService(..). The results displayed through the console are done through message boxes.

Points of Interest

The interesting thing I learned is doing Console applications and still using the windows functionality such as MessageBoxes.To get this, I started my project as a Win 32 console application and in next dialog box, I selected "An Application that supports MFC." It made things simpler.

Downloads

Download demo project - 26 Kb
Download source - 3 Kb


Comments

  • Code help

    Posted by Legacy on 01/04/2004 12:00am

    Originally posted by: ash

    Sir i just downloaded the project and tried to run it on vc++ 6.0,it is not starting my service,please help,iam only two weeks old in vc++.

    Reply
  • thanks!!!

    Posted by Legacy on 07/24/2003 12:00am

    Originally posted by: shaun

    hi im new to VC++/MFC thanks a lot!!! helps loads

    Reply
  • Bug when we login again the icondoesn't appear in system Tray

    Posted by Legacy on 05/13/2003 12:00am

    Originally posted by: Pooja

    Once we start the service .The icon comes in the systemtray.
    But we log off and then log in the system tray doesn't display the icon
    one has to go to the scm editor to stop the service.

    Is There any fix for the same ???


    Regards
    Pooja.

    Reply
  • Vow!!! Nice yaar!!

    Posted by Legacy on 04/15/2003 12:00am

    Originally posted by: Koti Reddy

    Very nice.
    Keep it up!!!!!!!!(?)

    Reply
  • 10000th version of "Hello World"

    Posted by Legacy on 04/14/2003 12:00am

    Originally posted by: Programmer

    Isn't it? Man.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

  • Live Event Date: December 18, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this upcoming webcast …

Most Popular Programming Stories

More for Developers

RSS Feeds