Creating the Windows service C program example using Win32 library
// For WinXp as a target
#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <stdio.h>
int main(void)
{
SC_HANDLE schSCManager, schService;
// The service executable location, just dummy here else make sure the executable is there :o)...
// Well as a real example, we are going to create our own telnet service.
// The executable for telnet is: C:\WINDOWS\system32\tlntsvr.exe
LPCTSTR lpszBinaryPathName = L"%SystemRoot%\\system32\\tlntsvr.exe";
// Service display name...
LPCTSTR lpszDisplayName = L"MyOwnTelnetService";
// Registry Subkey
LPCTSTR lpszServiceName = L"MyTelnetSrv";
// Open a handle to the SC Manager database...
schSCManager = OpenSCManager(
NULL, // local machine
NULL, // SERVICES_ACTIVE_DATABASE database is opened by default
SC_MANAGER_ALL_ACCESS); // full access rights
if (NULL == schSCManager)
printf("OpenSCManager() failed, error: %d.\n", GetLastError());
else
printf("OpenSCManager() looks OK.\n");
// Create/install service...
// If the function succeeds, the return value is a handle to the service. If the function fails, the return value is NULL.
schService = CreateService(
schSCManager, // SCManager database
lpszServiceName, // name of service
lpszDisplayName, // service name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_DEMAND_START, // start type
SERVICE_ERROR_NORMAL, // error control type
lpszBinaryPathName, // service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies, for real telnet there are dependencies lor
NULL, // LocalSystem account
NULL); // no password
if (schService == NULL)
{
printf("CreateService() failed, error: %ld\n", GetLastError());
return FALSE;
}
else
{
printf("CreateService() for %S looks OK.\n", lpszServiceName);
if (CloseServiceHandle(schService) == 0)
printf("CloseServiceHandle() failed, Error: %d.\n", GetLastError());
else
printf("CloseServiceHandle() is OK.\n");
return 0;
}
}
Output example:
OpenSCManager() looks OK.
CreateService() for MyTelnetSrv looks OK.
CloseServiceHandle() is OK.
Press any key to continue . . .
Well, verify the created service. Click Start->Programs->Administrative Tools->Services->Try finding "MyOwnTelnetService" service. It should be there...Select and right click->Click the Properties context menu. It is real service lor!!! This example will be used for the next several examples.
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: Windows Platform SDK
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: Creating a Windows service
To show: The Windows process and service programming using Win32 library