Click here to Skip to main content
15,118,713 members
Articles / Desktop Programming / Win32
Tip/Trick
Posted 11 Jan 2011

Tagged as

Stats

11.3K views
4 bookmarked

Create a service

Rate me:
Please Sign up or sign in to vote.
2.60/5 (5 votes)
15 Jan 2011CPOL
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)

Share

About the Author

Mukit, Ataul
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 16:09
Memberbrianbacon15-Jan-11 16:09 
GeneralRe: You are right.. it's not an article.. it's a tip.. and the b... Pin
Mukit, Ataul16-Jan-11 18:53
MemberMukit, Ataul16-Jan-11 18:53 
GeneralAdded the pre tab for you... Pin
Steve Maier13-Jan-11 8:45
professionalSteve Maier13-Jan-11 8:45 
GeneralRe: How very nice.. thnx.. Pin
Mukit, Ataul15-Jan-11 18:52
MemberMukit, Ataul15-Jan-11 18:52 
GeneralI tried, doesn't seem to work.. the pre tag.. Pin
Mukit, Ataul13-Jan-11 2:21
MemberMukit, Ataul13-Jan-11 2:21 
GeneralReason for my vote of 1 Do I really need to explain on this ... Pin
fjdiewornncalwe12-Jan-11 14:41
professionalfjdiewornncalwe12-Jan-11 14:41 
GeneralReason for my vote of 1 c&ping code from MSDN is not a tip. Pin
SledgeHammer0112-Jan-11 5:55
MemberSledgeHammer0112-Jan-11 5: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.