Click here to Skip to main content
Click here to Skip to main content

Tagged as

Create a service

, 15 Jan 2011 CPOL
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
C++ is not C with classes, JQuery is not Javascript, Google Search is not Learning, Design Patterns are not fashion, A code written in 2005 is not backdated just because it's 2015
Follow on   Twitter

Comments and Discussions

 
GeneralReason for my vote of 1 Not an article. Pinmemberbrianbacon15-Jan-11 16:09 
GeneralRe: You are right.. it's not an article.. it's a tip.. and the b... PinmemberMukit, Ataul16-Jan-11 18:53 
GeneralAdded the pre tab for you... PinmemberSteve Maier13-Jan-11 8:45 
GeneralRe: How very nice.. thnx.. PinmemberMukit, Ataul15-Jan-11 18:52 
GeneralI tried, doesn't seem to work.. the pre tag.. PinmemberMukit, Ataul13-Jan-11 2:21 
GeneralReason for my vote of 1 Do I really need to explain on this ... PinmemberMarcus Kramer12-Jan-11 14:41 
GeneralReason for my vote of 1 c&ping code from MSDN is not a tip. PinmemberSledgeHammer0112-Jan-11 5: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.1411023.1 | Last Updated 16 Jan 2011
Article Copyright 2011 by Mukit, Ataul
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid