Using the Windows service related functions in C language


// Don't forget to link to Advapi32.lib library if needed...

// For WinXp as a target, change appropriately

#define _WIN32_WINNT 0x0501

#include <windows.h>

#include <stdio.h>


// Prototypes, just skeletons...

void SvcDebugOut(LPSTR String, DWORD Status);

void WINAPI MyServiceCtrlHandler(DWORD opcode);

void MyServiceStart(DWORD argc, LPTSTR *argv); // ServiceMain()

DWORD MyServiceInitialization(DWORD argc, LPTSTR *argv, DWORD *specificError);


void main(void)


DWORD Status;

// Using 2-D array as a table, the service name just a fictitious...

// The name of a service to be run in this service process - "MyService". The function as the starting point for a service - MyServiceStart or

// a pointer to a ServiceMain() function. The members of the last entry in the table must have NULL values to designate the end of the table...

SERVICE_TABLE_ENTRY DispatchTable[] = {{L"MyService", (LPSERVICE_MAIN_FUNCTION)MyServiceStart}, {NULL, NULL}};


// Check the StartServiceCtrlDispatcher() return value. If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.

Status = StartServiceCtrlDispatcher(DispatchTable);

printf("StartServiceCtrlDispatcher() return value is: %d\n", Status);

if (!StartServiceCtrlDispatcher(DispatchTable))

SvcDebugOut("StartServiceCtrlDispatcher() failed", GetLastError());


printf("StartServiceCtrlDispatcher() looks OK.\n");




// =============================================

// Prototype definitions, just skeletons here...

void WINAPI MyServiceCtrlHandler(DWORD opcode)


// Service control information here...

printf("Service control information here...\n");




// The generic ServiceMain()

void MyServiceStart(DWORD argc, LPTSTR *argv)


// Starting service information here...

printf("Starting service information here...\n");




DWORD MyServiceInitialization(DWORD argc, LPTSTR *argv, DWORD *specificError)


// Service initialization information here...

printf("Service initialization information here...\n");

return 0;



// A very simple info to the debugger and standard output...

void SvcDebugOut(LPSTR String, DWORD Status)


CHAR Buffer[1024];

printf("In SvcDebugOut() lol!\n");


if (strlen(String) < 500)


// sprintf(Buffer, String, Status);

sprintf_s(Buffer, sizeof(Buffer), String, Status);

// The OutputDebugString() function sends a string to the debugger for display. If the application has no debugger,

// the system debugger displays the string. When you debug this program, see the output window at the bottom for the error message...


// If the application has debugger or the system debugger is not active, OutputDebugString() does we have to redirect it to the standard output...

printf("The error message string: %s and error code is: %d\n", Buffer, Status);



printf("Error message string is too long...\n");




Output example:


StartServiceCtrlDispatcher() return value is: 0

In SvcDebugOut() lol!

The error message string: StartServiceCtrlDispatcher() failed and error code is: 1056

Press any key to continue . . .


Note: If you debug the program, the error in the output window is:

StartServiceCtrlDispatcher() failed...

Which means:

ERROR_FAILED_SERVICE_CONTROLLER_CONNECT - (1063) The service process could not connect to the service controller.


Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Target platform: none, just for learning and fun

Header file: Standard and Windows

Additional library: [url=]Windows Platform SDK[/url]

Additional project setting: Set project to be compiled as C. Click Project menu->your_project_name Properties->Configuration Properties->C/C++->Advanced->Compiled As: Compiled as C Code (/TC)

Other info: non-CLR or unmanaged. Need to add Advapi32.lib (Advapi32.dll) to the project. Click the Project menu->Select the your_project_name Properties... sub menu->Expand the Configuration Properties folder on the left pane->Expand the Linker subfolder->Select the Input subfolder->Select the Additional Dependencies field on the right pane->Click the ... at the end of the field->Type in 'Advapi32.lib' in the empty pane->Click the OK button->Click the OK button second time to close the project Properties dialog.

To do: Using the Windows service related functions for Windows services programming

To show: Windows services related functions in Win32 programming




C and C++ Programming Resources | C & C++ Code Example Index