Click here to Skip to main content
11,709,033 members (62,468 online)
Click here to Skip to main content

Tagged as

Create a service

, 15 Jan 2011 CPOL 4.9K 4
Rate this:
Please Sign up or sign in to vote.
This tip shows how to create a service
#include <windows.h>
#include <stdio.h>

BOOL CreateSampleService(LPCTSTR lpszDisplayName, LPCTSTR szPath)
{
    TCHAR szPath[MAX_PATH];
 
    if( !GetModuleFileName( NULL, szPath, MAX_PATH ) )
    {
        printf("GetModuleFileName failed (%d)\n", GetLastError());
        return FALSE;
    }
 
    schService = CreateService(
        schSCManager,              // SCManager database
        TEXT("Sample_Srv"),        // 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
        szPath,                    // path to service's binary
        NULL,                      // no load ordering group
        NULL,                      // no tag identifier
        NULL,                      // no dependencies
        NULL,                      // LocalSystem account
        NULL);                     // no password

    if (schService == NULL)
    {
        printf("CreateService failed (%d)\n", GetLastError());
        return FALSE;
    }
    else
    {
        CloseServiceHandle(schService);
        return TRUE;
    }
}

To find out how to write a service, this site may come in handy:
http://www.devx.com/cplus/Article/9857

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mukit, Ataul
Chief Technology Officer Rational Technologies
Bangladesh Bangladesh
Your Lord does not like people who waste by extravagance, and only the righteous C s, and does so in increments, and they are of those people who are mindful of what memory and time has in offer and they waste none of it.

You may also be interested in...

Comments and Discussions

 
GeneralReason for my vote of 1 Not an article. Pin
brianbacon15-Jan-11 15:09
memberbrianbacon15-Jan-11 15:09 
GeneralRe: You are right.. it's not an article.. it's a tip.. and the b... Pin
Mukit, Ataul16-Jan-11 17:53
memberMukit, Ataul16-Jan-11 17:53 
GeneralAdded the pre tab for you... Pin
Steve Maier13-Jan-11 7:45
memberSteve Maier13-Jan-11 7:45 
GeneralRe: How very nice.. thnx.. Pin
Mukit, Ataul15-Jan-11 17:52
memberMukit, Ataul15-Jan-11 17:52 
GeneralI tried, doesn't seem to work.. the pre tag.. Pin
Mukit, Ataul13-Jan-11 1:21
memberMukit, Ataul13-Jan-11 1:21 
GeneralReason for my vote of 1 Do I really need to explain on this ... Pin
Marcus Kramer12-Jan-11 13:41
memberMarcus Kramer12-Jan-11 13:41 
GeneralReason for my vote of 1 c&ping code from MSDN is not a tip. Pin
SledgeHammer0112-Jan-11 4:55
memberSledgeHammer0112-Jan-11 4:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 16 Jan 2011
Article Copyright 2011 by Mukit, Ataul
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid