Click here to Skip to main content
15,867,986 members
Articles / Desktop Programming / Win32
Tip/Trick

Create a service

Rate me:
Please Sign up or sign in to vote.
2.60/5 (5 votes)
15 Jan 2011CPOL 13.1K   4   7
This tip shows how to create a service
C++
#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)


Written By
Technical Lead Kotha Technologies
Bangladesh Bangladesh
If you are not in - you are out !
- Chapter 1

Comments and Discussions

 
GeneralReason for my vote of 1 Not an article. Pin
brianbacon15-Jan-11 15:09
brianbacon15-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
Mukit, Ataul16-Jan-11 17:53 
GeneralAdded the pre tab for you... Pin
Steve Maier13-Jan-11 7:45
professionalSteve Maier13-Jan-11 7:45 
GeneralRe: How very nice.. thnx.. Pin
Mukit, Ataul15-Jan-11 17:52
Mukit, Ataul15-Jan-11 17:52 
GeneralI tried, doesn't seem to work.. the pre tag.. Pin
Mukit, Ataul13-Jan-11 1:21
Mukit, Ataul13-Jan-11 1:21 
GeneralReason for my vote of 1 Do I really need to explain on this ... Pin
fjdiewornncalwe12-Jan-11 13:41
professionalfjdiewornncalwe12-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
SledgeHammer0112-Jan-11 4:55 

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

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