Exemplo n.º 1
0
static int startDispatcher(LPSERVICE_MAIN_FUNCTION svcMain)
{
    SC_HANDLE       mgr;
    char            name[80];
    ulong           len;

    if (!(mgr = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS))) {
        mprError("Can't open service manager");
        return MPR_ERR_CANT_OPEN;
    }
    /*
        Is the service installed?
     */
    len = sizeof(name);
    if (GetServiceDisplayName(mgr, app->serviceName, name, &len) == 0) {
        CloseServiceHandle(mgr);
        return MPR_ERR_CANT_READ;
    }
    /*
        Register this service with the SCM. This call will block and consume the main thread if the service is 
        installed and the app was started by the SCM. If started manually, this routine will return 0.
     */
    svcTable[0].lpServiceProc = svcMain;
    if (StartServiceCtrlDispatcher(svcTable) == 0) {
        mprError("Could not start the service control dispatcher: 0x%x", GetLastError());
        return MPR_ERR_CANT_INITIALIZE;
    }
    return 0;
}
Exemplo n.º 2
0
int MprWinService::startDispatcher(LPSERVICE_MAIN_FUNCTION svcMain)
{
	SC_HANDLE		mgr;
	char			name[80];
	ulong			len;

	if (!(mgr = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS))) {
		mprError(MPR_L, MPR_LOG, "Can't open service manager");
		return MPR_ERR_CANT_OPEN;
	}

	//
	//	Is the service installed?
	// 
	len = sizeof(name);
	if (GetServiceDisplayName(mgr, svcName, name, &len) == 0) {
		CloseServiceHandle(mgr);
		return MPR_ERR_CANT_READ;
	}

	//
	//	Register this service with the SCM. This call will block and consume
	//	the main thread if the service is installed and the app was started by
	//	the SCM. If started manually, this routine will return 0.
	// 
	svcTable[0].lpServiceProc = svcMain;
	if (StartServiceCtrlDispatcher(svcTable) == 0) {
		int rc = GetLastError();
		//	Failure
		mprLog(MPR_CONFIG, "Could not start the service control dispatcher\n");
		return MPR_ERR_CANT_INITIALIZE;
	}
	return 0;
}
Exemplo n.º 3
0
char *get_display_name(char *service_name)
{
    /* Get the service's display name from the SCM on NT/2000, since it
     * can be changed by the user on 2000, especially, from the
     * service control panel.  We can't trust the service name as
     * provided by the user.
     */
    if (isWindowsNT())
    {
        char display_name[MAX_PATH];
        SC_HANDLE scm = OpenSCManager(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
        DWORD namelen = sizeof(display_name);
        if (scm) {
            BOOL ok = GetServiceDisplayName(scm, service_name, display_name,
                                            &namelen);
            CloseServiceHandle(scm);
            if (ok)
                return strdup(display_name);
        }
    }
    return service_name;
}
Exemplo n.º 4
0
BOOL SetServicePassword (LPCTSTR lpszServiceName, const CaServiceConfig& cf)
{
	SC_HANDLE  schSCManager;
	SC_HANDLE  schService;
	TCHAR      tchszDisplayName [256];
	DWORD      dwpcchBuffer  = sizeof(tchszDisplayName);

	//
	// Connect to service control manager on the local machine and 
	// open the ServicesActive database
	schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
	if (schSCManager == NULL )
	{
		CString strMsg;
		LONG err = GetLastError();
		
		switch (err)
		{
		case ERROR_ACCESS_DENIED:
		case ERROR_DATABASE_DOES_NOT_EXIST:
		case ERROR_INVALID_PARAMETER:
			//strMsg = _T("Failed to connect to the Service Control Manager");
			AfxMessageBox (VDBA_MfcResourceString(IDS_E_CONNECT_SERVICE));
			break;
		}
		return FALSE;
	}
	// 
	// Check if the Service 'lpszServiceName' is already installed
	// REGEDT32.EXE can check this on
	// HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services
	schService = OpenService (schSCManager, lpszServiceName, SERVICE_ALL_ACCESS);
	if (schService == NULL)
		return FALSE;
#if defined (MAINWIN)
	return FALSE;
#else
	if (!GetServiceDisplayName(schSCManager, lpszServiceName, tchszDisplayName, &dwpcchBuffer))
	{
		//CString strMsg = _T("Cannot get the service's display name");
		AfxMessageBox (VDBA_MfcResourceString(IDS_E_GET_SERVICE_NAME));
		return FALSE;
	}
#endif
	//
	// Change the service config (password account).
	BOOL bOK = ChangeServiceConfig (
		schService,           // Handle of service
		SERVICE_NO_CHANGE,    // Type of service
		SERVICE_NO_CHANGE,    // When to start service
		SERVICE_NO_CHANGE,    // Severity if service fails to start
		NULL,                 // Address of service binary file name (Not Change)
		NULL,                 // Address of load ordering group name.(Not Change)
		NULL,                 // Pointer to variable to get tag identifier. (Not Change)
		NULL,                 // Address of array of dependency names. (Not Change)
		cf.m_strAccount,      // Address of account name of service
		cf.m_strPassword,     // Address of password for service account
		tchszDisplayName);    // Address of display name
	if (!bOK)
	{
		//CString strMsg = _T("Cannot change the service configuration, \nfail to set password");
		AfxMessageBox (VDBA_MfcResourceString(IDS_E_CHANGE_SERVICE_CONF));
		return FALSE;
	}
	return TRUE;
}