Windows Services Programming 18

 

 

A Complete Windows Service Working Program Example

 

The following working program example tries to combine all the previous code fragments into one Visual Studio C++ solution in creating simple Windows service console mode applications. We will create three projects in one solution.

 

 

 

Creating a New Console Application Program Example

 

Create a new empty Win32 console application project. Give a suitable project name and change the project location if needed.

 

Creating a New Console Application Program Example: Creating new Win32 C++ console application project in Visual C++ .NET

 

Creating a Message File (Generating DLL, Headers and Other Files)

 

Before proceeding to the real console mode application, we will create a message file which the generated DLL (also header and other related file) will be used later. Create a message file (WinService.mc) manually under the project folder shown below.

 

Creating a Message File (Generating DLL, Headers and Other Files)

 

 

The following is an example of message file that can be used to build a resource-only DLL which then can be utilized with the service sample when writing events to the event log. Use the following code for the message file and don’t forget to save it.

 

MessageIdTypedef=DWORD

 

SeverityNames=(Success=0x0:STATUS_SEVERITY_SUCCESS

    Informational=0x1:STATUS_SEVERITY_INFORMATIONAL

    Warning=0x2:STATUS_SEVERITY_WARNING

    Error=0x3:STATUS_SEVERITY_ERROR

    )

 

 

FacilityNames=(System=0x0:FACILITY_SYSTEM

    Runtime=0x2:FACILITY_RUNTIME

    Stubs=0x3:FACILITY_STUBS

    Io=0x4:FACILITY_IO_ERROR_CODE

)

 

LanguageNames=(English=0x409:MSG00409)

 

; // The following are message definitions.

 

MessageId=0x1

Severity=Error

Facility=Runtime

SymbolicName=SVC_ERROR

Language=English

An error has occurred (%2).

.

 

; // A message file must end with a period on its own line

; // followed by a blank line.

 

The message file content

 

Next, launch your Visual Studio command prompt.

 

Launching the Windows command prompt from Visual Studio

 

 

Then, use the following steps to build the DLL:

 

  1. mc -U file_name.mc
  2. rc -r file_name.rc
  3. link -dll -noentry -out:file_name.dll file_name.res /MACHINE:x86

 

Firstly, let compile the message file. Notice the generated files under the same folder.

 

 

Compiling the Windows message file

 

The generated winService.h header file's content is shown below.

 

// The following are message definitions.

//

//  Values are 32 bit values laid out as follows:

//

//   3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1

//   1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0

//  +---+-+-+-----------------------+-------------------------------+

//  |Sev|C|R|     Facility          |               Code            |

//  +---+-+-+-----------------------+-------------------------------+

//

//  where

//

//      Sev - is the severity code

//

//          00 - Success

//          01 - Informational

//          10 - Warning

//          11 - Error

//

//      C - is the Customer code flag

//

//      R - is a reserved bit

//

//      Facility - is the facility code

//

//      Code - is the facility's status code

//

//

// Define the facility codes

//

#define FACILITY_SYSTEM                  0x0

#define FACILITY_STUBS                   0x3

#define FACILITY_RUNTIME                 0x2

#define FACILITY_IO_ERROR_CODE           0x4

 

 

//

// Define the severity codes

//

#define STATUS_SEVERITY_WARNING          0x2

#define STATUS_SEVERITY_SUCCESS          0x0

#define STATUS_SEVERITY_INFORMATIONAL    0x1

#define STATUS_SEVERITY_ERROR            0x3

 

 

//

// MessageId: SVC_ERROR

//

// MessageText:

//

// An error has occurred (%2).

//

#define SVC_ERROR                        ((DWORD)0xC0020001L)

 

 // A message file must end with a period on its own line

 // followed by a blank line.

 

Next, generate the resource (.RES) file from the rc file using rc -r winservice.rc command.

 

Generating the Windows message file

 

 

 

< Windows Services 17 | Win32 Programming | Windows Services Win32 Programming | Windows Services 19 >