source/bases/Win32Service.c
//-----------------------------------------------------------------------------
// Service_Install()
//   Install the service with the given name.
//-----------------------------------------------------------------------------
Service_Install()
Install the service with the given name.
Service_Install () Install the service with the given name .
static int Service_Install(wchar_t *name, wchar_t *configFileName)
{
    PyObject *executableNameObj, *configFileNameObj, *formatObj, *nameObj;
    PyObject *fullName, *displayName, *formatArgs, *command;
    wchar_t *wfullName, *wdisplayName, *wcommand, *wdescription;
    wchar_t fullPathConfigFileName[PATH_MAX + 1];
    SC_HANDLE managerHandle, serviceHandle;
    SERVICE_DESCRIPTIONW sd;
    udt_ServiceInfo info;
    if (Service_SetupPython(&info) < 0)
        return -1;
    nameObj = PyUnicode_FromWideChar(name, -1);
    if (!nameObj)
        return LogPythonException("cannot create service name obj");
    formatArgs = PyTuple_Pack(1, nameObj);
    if (!formatArgs)
        return LogPythonException("cannot create service name tuple");
    fullName = PyUnicode_Format(info.nameFormat, formatArgs);
    if (!fullName)
        return LogPythonException("cannot create service name");
    displayName = PyUnicode_Format(info.displayNameFormat, formatArgs);
    if (!displayName)
        return LogPythonException("cannot create display name");
    Py_CLEAR(formatArgs);
    Py_CLEAR(nameObj);
    executableNameObj = PyUnicode_FromWideChar(g_ExecutableName, -1);
    if (!executableNameObj)
        return LogPythonException("cannot create executable name obj");
    if (!configFileName) {
        formatObj = PyUnicode_FromString("\"%s\"");
        if (!formatObj)
            return LogPythonException("cannot create format string");
        formatArgs = PyTuple_Pack(1, executableNameObj);
        if (!formatArgs)
            return LogPythonException("cannot create short command tuple");
    } else {
        Py_CLEAR(formatArgs);
        if (!_wfullpath(fullPathConfigFileName, configFileName,
                PATH_MAX + 1))
            return LogWin32Error(GetLastError(),
                    "cannot calculate absolute path of config file name");
        formatObj = PyUnicode_FromString("\"%s\" \"%s\"");
        if (!formatObj)
            return LogPythonException("cannot create format string");
        configFileNameObj = PyUnicode_FromWideChar(fullPathConfigFileName, -1);
        if (!configFileNameObj)
            return LogPythonException("cannot create config file name string");
        formatArgs = PyTuple_Pack(2, executableNameObj, configFileNameObj);
        if (!formatArgs)
            return LogPythonException("cannot create long command tuple");
        Py_CLEAR(configFileNameObj);
    }
    Py_CLEAR(executableNameObj);
    command = PyUnicode_Format(formatObj, formatArgs);
    if (!command)
        return LogPythonException("cannot create command");
    Py_CLEAR(formatObj);
    Py_CLEAR(formatArgs);
    managerHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
    if (!managerHandle)
        return LogWin32Error(GetLastError(), "cannot open service manager");
    wfullName = PyUnicode_AsWideCharString(fullName, NULL);
    wdisplayName = PyUnicode_AsWideCharString(displayName, NULL);
    wcommand = PyUnicode_AsWideCharString(command, NULL);
    serviceHandle = CreateServiceW(managerHandle, wfullName, wdisplayName,
            SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, info.startType,
            SERVICE_ERROR_NORMAL, wcommand, NULL, NULL, NULL, NULL, NULL);
    PyMem_Free(wfullName);
    PyMem_Free(wdisplayName);
    PyMem_Free(wcommand);
    if (!serviceHandle)
        return LogWin32Error(GetLastError(), "cannot create service");
    if (info.description) {
        wdescription = PyUnicode_AsWideCharString(info.description, NULL);
        sd.lpDescription = wdescription;
        if (!ChangeServiceConfig2W(serviceHandle,
                                   SERVICE_CONFIG_DESCRIPTION, &sd)) {
            PyMem_Free(wdescription);
            return LogWin32Error(GetLastError(),
                                 "cannot set service description");
        }
        PyMem_Free(wdescription);
    }
    if (info.startType == SERVICE_AUTO_START) {
        if (!StartService(serviceHandle, 0, NULL))
            return LogWin32Error(GetLastError(), "cannot start service");
    }
    CloseServiceHandle(serviceHandle);
    CloseServiceHandle(managerHandle);
    return 0;
}
static int Service_Install ( wchar_t * name , wchar_t * configFileName ) { PyObject * executableNameObj , * configFileNameObj , * formatObj , * nameObj ; PyObject * fullName , * displayName , * formatArgs , * command ; wchar_t * wfullName , * wdisplayName , * wcommand , * wdescription ; wchar_t fullPathConfigFileName [ PATH_MAX + 1 ] ; SC_HANDLE managerHandle , serviceHandle ; SERVICE_DESCRIPTIONW sd ; udt_ServiceInfo info ; if ( Service_SetupPython ( & info ) < 0 ) return -1 ; nameObj = PyUnicode_FromWideChar ( name , -1 ) ; if ( ! nameObj ) return LogPythonException ( " " ) ; formatArgs = PyTuple_Pack ( 1 , nameObj ) ; if ( ! formatArgs ) return LogPythonException ( " " ) ; fullName = PyUnicode_Format ( info . nameFormat , formatArgs ) ; if ( ! fullName ) return LogPythonException ( " " ) ; displayName = PyUnicode_Format ( info . displayNameFormat , formatArgs ) ; if ( ! displayName ) return LogPythonException ( " " ) ; Py_CLEAR ( formatArgs ) ; Py_CLEAR ( nameObj ) ; executableNameObj = PyUnicode_FromWideChar ( g_ExecutableName , -1 ) ; if ( ! executableNameObj ) return LogPythonException ( " " ) ; if ( ! configFileName ) { formatObj = PyUnicode_FromString ( " \" \" " ) ; if ( ! formatObj ) return LogPythonException ( " " ) ; formatArgs = PyTuple_Pack ( 1 , executableNameObj ) ; if ( ! formatArgs ) return LogPythonException ( " " ) ; } else { Py_CLEAR ( formatArgs ) ; if ( ! _wfullpath ( fullPathConfigFileName , configFileName , PATH_MAX + 1 ) ) return LogWin32Error ( GetLastError ( ) , " " ) ; formatObj = PyUnicode_FromString ( " \" \" \" \" " ) ; if ( ! formatObj ) return LogPythonException ( " " ) ; configFileNameObj = PyUnicode_FromWideChar ( fullPathConfigFileName , -1 ) ; if ( ! configFileNameObj ) return LogPythonException ( " " ) ; formatArgs = PyTuple_Pack ( 2 , executableNameObj , configFileNameObj ) ; if ( ! formatArgs ) return LogPythonException ( " " ) ; Py_CLEAR ( configFileNameObj ) ; } Py_CLEAR ( executableNameObj ) ; command = PyUnicode_Format ( formatObj , formatArgs ) ; if ( ! command ) return LogPythonException ( " " ) ; Py_CLEAR ( formatObj ) ; Py_CLEAR ( formatArgs ) ; managerHandle = OpenSCManager ( NULL , NULL , SC_MANAGER_ALL_ACCESS ) ; if ( ! managerHandle ) return LogWin32Error ( GetLastError ( ) , " " ) ; wfullName = PyUnicode_AsWideCharString ( fullName , NULL ) ; wdisplayName = PyUnicode_AsWideCharString ( displayName , NULL ) ; wcommand = PyUnicode_AsWideCharString ( command , NULL ) ; serviceHandle = CreateServiceW ( managerHandle , wfullName , wdisplayName , SERVICE_ALL_ACCESS , SERVICE_WIN32_OWN_PROCESS , info . startType , SERVICE_ERROR_NORMAL , wcommand , NULL , NULL , NULL , NULL , NULL ) ; PyMem_Free ( wfullName ) ; PyMem_Free ( wdisplayName ) ; PyMem_Free ( wcommand ) ; if ( ! serviceHandle ) return LogWin32Error ( GetLastError ( ) , " " ) ; if ( info . description ) { wdescription = PyUnicode_AsWideCharString ( info . description , NULL ) ; sd . lpDescription = wdescription ; if ( ! ChangeServiceConfig2W ( serviceHandle , SERVICE_CONFIG_DESCRIPTION , & sd ) ) { PyMem_Free ( wdescription ) ; return LogWin32Error ( GetLastError ( ) , " " ) ; } PyMem_Free ( wdescription ) ; } if ( info . startType == SERVICE_AUTO_START ) { if ( ! StartService ( serviceHandle , 0 , NULL ) ) return LogWin32Error ( GetLastError ( ) , " " ) ; } CloseServiceHandle ( serviceHandle ) ; CloseServiceHandle ( managerHandle ) ; return 0 ; }
Service_Install()
Install the service with the given name.
Service_Install () Install the service with the given name .
// set up Python // determine name and display name to use for the service // determine command to use for the service // open up service control manager // create service // set the description of the service, if one was specified // if the service is one that should be automatically started, start it // close the service handles