Exemple #1
0
LPSERVICE_STATUS_PROCESS
QueryService(LPCTSTR ServiceName)
{
    SC_HANDLE hSCManager = NULL;
    LPSERVICE_STATUS_PROCESS pServiceInfo = NULL;
    SC_HANDLE hSc = NULL;
    DWORD BufSiz = 0;
    DWORD BytesNeeded = 0;
    DWORD Ret;

    hSCManager = OpenSCManager(NULL,
                               NULL,
                               SC_MANAGER_CONNECT);
    if (hSCManager == NULL)
    {
        ReportLastError();
        return NULL;
    }

    hSc = OpenService(hSCManager,
                      ServiceName,
                      SERVICE_QUERY_STATUS);
    if (hSc == NULL)
        goto fail;

    Ret = QueryServiceStatusEx(hSc,
                               SC_STATUS_PROCESS_INFO,
                               NULL,
                               BufSiz,
                               &BytesNeeded);
    if ((Ret != 0) || (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
        goto fail;

    pServiceInfo = (LPSERVICE_STATUS_PROCESS)HeapAlloc(GetProcessHeap(),
                                                       0,
                                                       BytesNeeded);
    if (pServiceInfo == NULL)
        goto fail;

    if (!QueryServiceStatusEx(hSc,
                              SC_STATUS_PROCESS_INFO,
                              (LPBYTE)pServiceInfo,
                              BytesNeeded,
                              &BytesNeeded))
    {
        goto fail;
    }

    CloseServiceHandle(hSc);
    CloseServiceHandle(hSCManager);
    return pServiceInfo;

fail:
    ReportLastError();
    if (pServiceInfo) HeapFree(GetProcessHeap(), 0, pServiceInfo);
    if (hSc) CloseServiceHandle(hSc);
    if (hSCManager) CloseServiceHandle(hSCManager);
    return NULL;
}
Exemple #2
0
static void gl_swapinterval_changed(cvar_t *self)
{
    if (self->integer < 0 && !(glw.extensions & QWGL_EXT_swap_control_tear)) {
        Com_Printf("Negative swap interval is not supported on this system.\n");
        Cvar_Reset(self);
    }

    if (qwglSwapIntervalEXT && !qwglSwapIntervalEXT(self->integer))
        ReportLastError("wglSwapIntervalEXT");
}
Exemple #3
0
BOOL Delete(LPCTSTR ServiceName)
{
    SC_HANDLE hSCManager = NULL;
    SC_HANDLE hSc = NULL;

#ifdef SCDBG
    _tprintf(_T("service to delete - %s\n\n"), ServiceName);
#endif

    hSCManager = OpenSCManager(NULL,
                               NULL,
                               SC_MANAGER_CONNECT);
    if (hSCManager != NULL)
    {
        hSc = OpenService(hSCManager, ServiceName, DELETE);
        if (hSc != NULL)
        {
            if (DeleteService(hSc))
            {
                _tprintf(_T("[SC] DeleteService SUCCESS\n"));

                CloseServiceHandle(hSc);
                CloseServiceHandle(hSCManager);

                return TRUE;
            }
        }
    }

    ReportLastError();

    if (hSc) CloseServiceHandle(hSc);
    if (hSCManager) CloseServiceHandle(hSCManager);

    return FALSE;
}
Exemple #4
0
BOOL
Control(DWORD Control,
        LPCTSTR ServiceName,
        LPCTSTR *Args,
        INT ArgCount)
{
    SC_HANDLE hSCManager = NULL;
    SC_HANDLE hService = NULL;
    SERVICE_STATUS Status;
    DWORD dwDesiredAccess = 0;
    BOOL bResult = TRUE;
    SERVICE_STATUS_PROCESS StatusEx;

#ifdef SCDBG
    LPCTSTR *TmpArgs = Args;
    INT TmpCnt = ArgCount;
    _tprintf(_T("service to control - %s\n"), ServiceName);
    _tprintf(_T("command - %lu\n"), Control);
    _tprintf(_T("Arguments:\n"));
    while (TmpCnt)
    {
        _tprintf(_T("  %s\n"), *TmpArgs);
        TmpArgs++;
        TmpCnt--;
    }
    _tprintf(_T("\n"));
#endif /* SCDBG */

    switch (Control)
    {
        case SERVICE_CONTROL_STOP:
            dwDesiredAccess = SERVICE_STOP;
            break;

        case SERVICE_CONTROL_PAUSE:
            dwDesiredAccess = SERVICE_PAUSE_CONTINUE;
            break;

        case SERVICE_CONTROL_CONTINUE:
            dwDesiredAccess = SERVICE_PAUSE_CONTINUE;
            break;

        case SERVICE_CONTROL_INTERROGATE:
            dwDesiredAccess = SERVICE_INTERROGATE;
            break;

        case SERVICE_CONTROL_SHUTDOWN:
            dwDesiredAccess = 0;
            break;

    }

    hSCManager = OpenSCManager(NULL,
                               NULL,
                               SC_MANAGER_CONNECT);
    if (hSCManager == NULL)
    {
        _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
        bResult = FALSE;
        goto done;
    }

    hService = OpenService(hSCManager,
                           ServiceName,
                           dwDesiredAccess);
    if (hService == NULL)
    {
        _tprintf(_T("[SC] OpenService FAILED %lu:\n\n"), GetLastError());
        bResult = FALSE;
        goto done;
    }

    if (!ControlService(hService,
                        Control,
                        &Status))
    {
        _tprintf(_T("[SC] ControlService FAILED %lu:\n\n"), GetLastError());
        bResult = FALSE;
        goto done;
    }

    /* FIXME: lazy hack ;) */
    CopyMemory(&StatusEx, &Status, sizeof(Status));
    StatusEx.dwProcessId = 0;
    StatusEx.dwServiceFlags = 0;

    PrintService(ServiceName,
                 &StatusEx,
                 FALSE);

done:
    if (!bResult)
        ReportLastError();

    if (hService)
        CloseServiceHandle(hService);

    if (hSCManager)
        CloseServiceHandle(hSCManager);

    return bResult;
}
FvUInt32 StartProcess( const char *pcRoot,
					  const char *pcResPath,
					  const char *pcConfig,
					  const char *pcType,
					  FvUInt16 uiUID,
					  FvUInt16 uiGID,
					  const char *pcHome,
					  int iArgc,
					  const char **pcArgv,
					  FvMachined &kMachined )
{
	char acPath[512];

	strcpy( acPath, pcRoot );
	//strcat( acPath, "\\futurevision\\bin\\server_" );
	strcat( acPath, pcConfig );
	strcat( acPath, "\\" );
	strncat( acPath, pcType, 32 );
	strcat( acPath, ".exe" );

	TCHAR szArgs[MAX_PATH] = TEXT("");
	int iLen = 0;
	for ( int ndx = 1; ndx < iArgc; ndx++ )
	{
		iLen += wsprintf( szArgs+iLen, TEXT( " \"%s\"" ), pcArgv[ndx] );
	}

	EnvironMap kEnvMap;
	ReadEnviron(kEnvMap);
	kEnvMap["FV_ROOT"] = pcRoot;
	kEnvMap["FV_RES_PATH"] = pcResPath;
	std::ostringstream kUIDToString;
	kUIDToString << uiUID;
	kEnvMap["UID"] = kUIDToString.str();
	char * pcEnvStr = MakeEnvironStr(kEnvMap);

	TCHAR szApp[MAX_PATH];
	lstrcpy( szApp, acPath );

	strcpy(acPath, pcRoot);
	//strcat(acPath, "\\futurevision\\bin\\server_" );
	strcat(acPath, pcConfig );

	STARTUPINFO si;
	ZeroMemory( &si, sizeof(si) );
	si.cb = sizeof(si);
	PROCESS_INFORMATION pi;
	FvSysLog( FV_SYSLOG_INFO, "starting: '%s'\nwith args: '%s'\nand working path: '%s'", szApp, szArgs, acPath );
	if ( CreateProcess( szApp, szArgs, NULL, NULL, TRUE,
		NORMAL_PRIORITY_CLASS |	CREATE_NO_WINDOW, pcEnvStr, acPath, &si, &pi ) )
	{
		CloseHandle( pi.hThread );
		CloseHandle( pi.hProcess );
	}
	else
	{
		ReportLastError();
	}

	delete[] pcEnvStr;
	return pi.dwProcessId;
}
Exemple #6
0
// This is called when the user first loads the add-in, and on start-up
//  of each subsequent Developer Studio session
STDMETHODIMP CDSAddIn::OnConnection (IApplication * pApp, VARIANT_BOOL bFirstTime,
				     long dwCookie, VARIANT_BOOL * OnConnection)
{
	AFX_MANAGE_STATE (AfxGetStaticModuleState ());
	*OnConnection = VARIANT_FALSE;

	// Store info passed to us
	IApplication *pApplication = NULL;
	HRESULT hr;

	hr = pApp->QueryInterface (IID_IApplication, (void **) &pApplication);
	if (FAILED (hr))
	{
		ReportLastError (hr);
		return E_UNEXPECTED;
	}
	if (pApplication == NULL)
	{
		ReportInternalError ("IApplication::QueryInterface");
		return E_UNEXPECTED;
	}

	m_dwCookie = dwCookie;

	// Create command dispatch, send info back to DevStudio
	CCommandsObj::CreateInstance (&m_pCommands);
	if (! m_pCommands)
	{
		ReportInternalError ("CCommandsObj::CreateInstance");
		return E_UNEXPECTED;
	}
	m_pCommands->AddRef ();

	// The QueryInterface above AddRef'd the Application object.  It will
	// be Release'd in CCommand's destructor.
	m_pCommands->SetApplicationObject (pApplication);

	hr = pApplication->SetAddInInfo ((long) AfxGetInstanceHandle (),
		(LPDISPATCH) m_pCommands, IDR_TOOLBAR_MEDIUM, IDR_TOOLBAR_LARGE,
		m_dwCookie);
	if (FAILED (hr))
	{
		ReportLastError (hr);
		return E_UNEXPECTED;
	}

	// Inform DevStudio of the commands we implement
	if (! AddCommand (pApplication, "VisVimDialog", "VisVimDialogCmd",
			  IDS_CMD_DIALOG, 0, bFirstTime))
		return E_UNEXPECTED;
	if (! AddCommand (pApplication, "VisVimEnable", "VisVimEnableCmd",
			  IDS_CMD_ENABLE, 1, bFirstTime))
		return E_UNEXPECTED;
	if (! AddCommand (pApplication, "VisVimDisable", "VisVimDisableCmd",
			  IDS_CMD_DISABLE, 2, bFirstTime))
		return E_UNEXPECTED;
	if (! AddCommand (pApplication, "VisVimToggle", "VisVimToggleCmd",
			  IDS_CMD_TOGGLE, 3, bFirstTime))
		return E_UNEXPECTED;
	if (! AddCommand (pApplication, "VisVimLoad", "VisVimLoadCmd",
			  IDS_CMD_LOAD, 4, bFirstTime))
		return E_UNEXPECTED;

	*OnConnection = VARIANT_TRUE;
	return S_OK;
}
Exemple #7
0
BOOL EnumDepend(LPCTSTR ServiceName)
{
    SC_HANDLE hManager = NULL;
    SC_HANDLE hService = NULL;
    BOOL bResult = TRUE;
    DWORD BufferSize = 0;
    DWORD EntriesRead = 0;
    LPENUM_SERVICE_STATUS pBuffer = NULL;
    DWORD i;

    hManager = OpenSCManager(NULL,
                             NULL,
                             SC_MANAGER_CONNECT);
    if (hManager == NULL)
    {
        _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
        bResult = FALSE;
        goto done;
    }

    hService = OpenService(hManager, ServiceName, SERVICE_ENUMERATE_DEPENDENTS);
    if (hService == NULL)
    {
        _tprintf(_T("[SC] OpenService FAILED %lu:\n\n"), GetLastError());
        bResult = FALSE;
        goto done;
    }

    if (!EnumDependentServices(hService,
                               SERVICE_STATE_ALL,
                               NULL,
                               0,
                               &BufferSize,
                               &EntriesRead))
    {
        if (BufferSize == 0)
        {
            _tprintf(_T("[SC] EnumDependentServices FAILED %lu:\n\n"), GetLastError());
            bResult = FALSE;
            goto done;
        }
    }

    pBuffer = HeapAlloc(GetProcessHeap(), 0, BufferSize);
    if (pBuffer == NULL)
    {
        SetLastError(ERROR_OUTOFMEMORY);
        _tprintf(_T("[SC] HeapAlloc FAILED %lu:\n\n"), GetLastError());
        bResult = FALSE;
        goto done;
    }

    if (!EnumDependentServices(hService,
                               SERVICE_STATE_ALL,
                               pBuffer,
                               BufferSize,
                               &BufferSize,
                               &EntriesRead))
    {
        _tprintf(_T("[SC] EnumDependentServices FAILED %lu:\n\n"), GetLastError());
        bResult = FALSE;
        goto done;
    }

    _tprintf(_T("Enum: entriesRead = %lu\n"), EntriesRead);

    for (i = 0; i < EntriesRead; i++)
    {
        _tprintf(_T("\n"));
        _tprintf(_T("SERVICE_NAME: %s\n"), pBuffer[i].lpServiceName);
        _tprintf(_T("DISPLAY_NAME: %s\n"), pBuffer[i].lpDisplayName);
        PrintServiceStatus(&pBuffer[i].ServiceStatus);
    }

done:
    if (bResult == FALSE)
        ReportLastError();

    if (pBuffer != NULL)
        HeapFree(GetProcessHeap(), 0, pBuffer);

    if (hService)
        CloseServiceHandle(hService);

    if (hManager)
        CloseServiceHandle(hManager);

    return bResult;
}
Exemple #8
0
static int SetupGL(int colorbits, int depthbits, int stencilbits, int multisamples)
{
    PIXELFORMATDESCRIPTOR pfd;
    int pixelformat;

    // create the main window
    Win_Init();

    if (colorbits == 0)
        colorbits = 24;

    if (depthbits == 0)
        depthbits = colorbits > 16 ? 24 : 16;

    if (depthbits < 24)
        stencilbits = 0;

    // choose pixel format
    if (qwglChoosePixelFormatARB && multisamples > 1) {
        int iAttributes[20];
        UINT numFormats;

        iAttributes[0] = WGL_DRAW_TO_WINDOW_ARB;
        iAttributes[1] = TRUE;
        iAttributes[2] = WGL_SUPPORT_OPENGL_ARB;
        iAttributes[3] = TRUE;
        iAttributes[4] = WGL_DOUBLE_BUFFER_ARB;
        iAttributes[5] = TRUE;
        iAttributes[6] = WGL_PIXEL_TYPE_ARB;
        iAttributes[7] = WGL_TYPE_RGBA_ARB;
        iAttributes[8] = WGL_COLOR_BITS_ARB;
        iAttributes[9] = colorbits;
        iAttributes[10] = WGL_DEPTH_BITS_ARB;
        iAttributes[11] = depthbits;
        iAttributes[12] = WGL_STENCIL_BITS_ARB;
        iAttributes[13] = stencilbits;
        iAttributes[14] = WGL_SAMPLE_BUFFERS_ARB;
        iAttributes[15] = 1;
        iAttributes[16] = WGL_SAMPLES_ARB;
        iAttributes[17] = multisamples;
        iAttributes[18] = 0;
        iAttributes[19] = 0;

        if (qwglChoosePixelFormatARB(win.dc, iAttributes, NULL, 1, &pixelformat, &numFormats) == FALSE) {
            ReportLastError("wglChoosePixelFormatARB");
            goto soft;
        }
        if (numFormats == 0) {
            Com_EPrintf("No suitable OpenGL pixelformat found for %d multisamples\n", multisamples);
            goto soft;
        }
    } else {
        memset(&pfd, 0, sizeof(pfd));
        pfd.nSize = sizeof(pfd);
        pfd.nVersion = 1;
        pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
        pfd.iPixelType = PFD_TYPE_RGBA;
        pfd.cColorBits = colorbits;
        pfd.cDepthBits = depthbits;
        pfd.cStencilBits = stencilbits;
        pfd.iLayerType = PFD_MAIN_PLANE;

        if (glw.minidriver) {
            if ((pixelformat = qwglChoosePixelFormat(win.dc, &pfd)) == 0) {
                ReportLastError("wglChoosePixelFormat");
                goto soft;
            }
        } else {
            if ((pixelformat = ChoosePixelFormat(win.dc, &pfd)) == 0) {
                ReportLastError("ChoosePixelFormat");
                goto soft;
            }
        }
    }

    // set pixel format
    if (glw.minidriver) {
        qwglDescribePixelFormat(win.dc, pixelformat, sizeof(pfd), &pfd);
        ReportPixelFormat(pixelformat, &pfd);

        if (qwglSetPixelFormat(win.dc, pixelformat, &pfd) == FALSE) {
            ReportLastError("wglSetPixelFormat");
            goto soft;
        }
    } else {
        DescribePixelFormat(win.dc, pixelformat, sizeof(pfd), &pfd);
        ReportPixelFormat(pixelformat, &pfd);

        if (SetPixelFormat(win.dc, pixelformat, &pfd) == FALSE) {
            ReportLastError("SetPixelFormat");
            goto soft;
        }
    }

    // check for software emulation
    if (pfd.dwFlags & PFD_GENERIC_FORMAT) {
        if (!gl_allow_software->integer) {
            Com_EPrintf("No hardware OpenGL acceleration detected\n");
            goto soft;
        }
        Com_WPrintf("...using software emulation\n");
    } else if (pfd.dwFlags & PFD_GENERIC_ACCELERATED) {
        Com_DPrintf("...MCD acceleration found\n");
        win.flags |= QVF_ACCELERATED;
    } else {
        Com_DPrintf("...ICD acceleration found\n");
        win.flags |= QVF_ACCELERATED;
    }

    // startup the OpenGL subsystem by creating a context and making it current
    if ((glw.hGLRC = qwglCreateContext(win.dc)) == NULL) {
        ReportLastError("wglCreateContext");
        goto hard;
    }

    if (qwglMakeCurrent(win.dc, glw.hGLRC) == FALSE) {
        ReportLastError("wglMakeCurrent");
        qwglDeleteContext(glw.hGLRC);
        glw.hGLRC = NULL;
        goto hard;
    }

    return FAIL_OK;

soft:
    // it failed, clean up
    Win_Shutdown();
    return FAIL_SOFT;

hard:
    Win_Shutdown();
    return FAIL_HARD;
}
Exemple #9
0
static int LoadGL(const char *driver)
{
    int colorbits = Cvar_ClampInteger(gl_colorbits, 0, 32);
    int depthbits = Cvar_ClampInteger(gl_depthbits, 0, 32);
    int stencilbits = Cvar_ClampInteger(gl_stencilbits, 0, 8);
    int multisamples = Cvar_ClampInteger(gl_multisamples, 0, 32);
    int ret;

    // figure out if we're running on a minidriver or not
    if (!Q_stricmp(driver, "opengl32") ||
        !Q_stricmp(driver, "opengl32.dll")) {
        glw.minidriver = qfalse;
    } else {
        Com_Printf("...running a minidriver: %s\n", driver);
        glw.minidriver = qtrue;
    }

    // load the OpenGL library and bind to it
    if (!WGL_Init(driver)) {
        ReportLastError("WGL_Init");
        return FAIL_SOFT;
    }

    // check if basic WGL entry points are present
    if (!qwglCreateContext || !qwglMakeCurrent || !qwglDeleteContext) {
        Com_EPrintf("Required WGL entry points are missing\n");
        goto fail;
    }

    if (glw.minidriver) {
        // check if MCD entry points are present if using a minidriver
        if (!qwglChoosePixelFormat || !qwglSetPixelFormat ||
            !qwglDescribePixelFormat || !qwglSwapBuffers) {
            Com_EPrintf("Required MCD entry points are missing\n");
            goto fail;
        }
    }

    // check for WGL_ARB_multisample by creating a fake window
    if (multisamples > 1) {
        unsigned extensions = GetFakeWindowExtensions();

        if (extensions & QWGL_ARB_multisample) {
            if (qwglChoosePixelFormatARB) {
                Com_Printf("...enabling WGL_ARB_multisample\n");
            } else {
                Com_Printf("...ignoring WGL_ARB_multisample, WGL_ARB_pixel_format not found\n");
                Cvar_Set("gl_multisamples", "0");
                multisamples = 0;
            }
        } else {
            Com_Printf("WGL_ARB_multisample not found\n");
            Cvar_Set("gl_multisamples", "0");
            multisamples = 0;
        }
    }

    // create window, choose PFD, setup OpenGL context
    ret = SetupGL(colorbits, depthbits, stencilbits, multisamples);

    // attempt to recover
    if (ret == FAIL_SOFT && (colorbits || depthbits || stencilbits || multisamples > 1)) {
        Cvar_Set("gl_multisamples", "0");
        ret = SetupGL(0, 0, 0, 0);
    }

    if (ret)
        goto fail;

    return FAIL_OK;

fail:
    // it failed, clean up
    WGL_Shutdown();
    return FAIL_SOFT;
}
Exemple #10
0
BOOL Create(LPCTSTR *ServiceArgs, INT ArgCount)
{
    SC_HANDLE hSCManager;
    SC_HANDLE hSc;
    BOOL bRet = FALSE;

    INT i;
    INT Length;
    LPTSTR lpBuffer = NULL;
    SERVICE_CREATE_INFO ServiceInfo;

    if (!ParseCreateArguments(ServiceArgs, ArgCount, &ServiceInfo))
    {
        CreateUsage();
        return FALSE;
    }

    if (!ServiceInfo.dwServiceType)
        ServiceInfo.dwServiceType = SERVICE_WIN32_OWN_PROCESS;

    if (!ServiceInfo.dwStartType)
        ServiceInfo.dwStartType = SERVICE_DEMAND_START;

    if (!ServiceInfo.dwErrorControl)
        ServiceInfo.dwErrorControl = SERVICE_ERROR_NORMAL;

    if (ServiceInfo.lpDependencies)
    {
        Length = lstrlen(ServiceInfo.lpDependencies);

        lpBuffer = HeapAlloc(GetProcessHeap(),
                             0,
                            (Length + 2) * sizeof(TCHAR));

        for (i = 0; i < Length; i++)
            if (ServiceInfo.lpDependencies[i] == _T('/'))
                lpBuffer[i] = 0;
            else
                lpBuffer[i] = ServiceInfo.lpDependencies[i];

        lpBuffer[Length] = 0;
        lpBuffer[Length + 1] = 0;

        ServiceInfo.lpDependencies = lpBuffer;
    }

#ifdef SCDBG
    _tprintf(_T("service name - %s\n"), ServiceInfo.lpServiceName);
    _tprintf(_T("display name - %s\n"), ServiceInfo.lpDisplayName);
    _tprintf(_T("service type - %lu\n"), ServiceInfo.dwServiceType);
    _tprintf(_T("start type - %lu\n"), ServiceInfo.dwStartType);
    _tprintf(_T("error control - %lu\n"), ServiceInfo.dwErrorControl);
    _tprintf(_T("Binary path - %s\n"), ServiceInfo.lpBinaryPathName);
    _tprintf(_T("load order group - %s\n"), ServiceInfo.lpLoadOrderGroup);
    _tprintf(_T("tag - %lu\n"), ServiceInfo.dwTagId);
    _tprintf(_T("dependencies - %s\n"), ServiceInfo.lpDependencies);
    _tprintf(_T("account start name - %s\n"), ServiceInfo.lpServiceStartName);
    _tprintf(_T("account password - %s\n"), ServiceInfo.lpPassword);
#endif

    hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);

    if (hSCManager != NULL)
    {
        hSc = CreateService(hSCManager,
                            ServiceInfo.lpServiceName,
                            ServiceInfo.lpDisplayName,
                            SERVICE_ALL_ACCESS,
                            ServiceInfo.dwServiceType,
                            ServiceInfo.dwStartType,
                            ServiceInfo.dwErrorControl,
                            ServiceInfo.lpBinaryPathName,
                            ServiceInfo.lpLoadOrderGroup,
                            ServiceInfo.bTagId ? &ServiceInfo.dwTagId : NULL,
                            ServiceInfo.lpDependencies,
                            ServiceInfo.lpServiceStartName,
                            ServiceInfo.lpPassword);

        if (hSc != NULL)
        {
            _tprintf(_T("[SC] CreateService SUCCESS\n"));

            CloseServiceHandle(hSc);
            bRet = TRUE;
        }
        else
            ReportLastError();

        CloseServiceHandle(hSCManager);
    }
    else
        ReportLastError();

    if (lpBuffer != NULL)
        HeapFree(GetProcessHeap(), 0, lpBuffer);

    return bRet;
}
Exemple #11
0
BOOL Start(LPCTSTR ServiceName, LPCTSTR *ServiceArgs, INT ArgCount)
{
    SC_HANDLE hSCManager = NULL;
    SC_HANDLE hSc = NULL;
    LPSERVICE_STATUS_PROCESS pServiceInfo = NULL;

#ifdef SCDBG
    LPCTSTR *TmpArgs = ServiceArgs;
    INT TmpCnt = ArgCount;
    _tprintf(_T("service to control - %s\n"), ServiceName);
    _tprintf(_T("Arguments:\n"));
    while (TmpCnt)
    {
        _tprintf(_T("  %s\n"), *TmpArgs);
        TmpArgs++;
        TmpCnt--;
    }
    _tprintf(_T("\n"));
#endif /* SCDBG */

    hSCManager = OpenSCManager(NULL,
                               NULL,
                               SC_MANAGER_CONNECT);
    if (hSCManager == NULL)
    {
        ReportLastError();
        return FALSE;
    }

    hSc = OpenService(hSCManager,
                      ServiceName,
                      SERVICE_START | SERVICE_QUERY_STATUS);

    if (hSc == NULL)
        goto fail;

    if (!ArgCount)
    {
        ServiceArgs = NULL;
    }

    if (! StartService(hSc,
                       ArgCount,
                       ServiceArgs))
    {
        _tprintf(_T("[SC] StartService FAILED %lu:\n\n"), GetLastError());
        goto fail;
    }

    pServiceInfo = QueryService(ServiceName);
    if (pServiceInfo != NULL)
    {
        PrintService(ServiceName,
                     pServiceInfo,
                     TRUE);
    }

    HeapFree(GetProcessHeap(), 0, pServiceInfo);
    CloseServiceHandle(hSc);
    CloseServiceHandle(hSCManager);

    return TRUE;

fail:
    ReportLastError();
    if (hSc) CloseServiceHandle(hSc);
    if (hSCManager) CloseServiceHandle(hSCManager);
    return FALSE;

}
Exemple #12
0
BOOL SetConfig(LPCTSTR *ServiceArgs, INT ArgCount)
{
    SC_HANDLE hManager = NULL;
    SC_HANDLE hService = NULL;
    BOOL bResult = TRUE;
    SERVICE_CREATE_INFO ServiceInfo;

    if (!ParseCreateConfigArguments(ServiceArgs, ArgCount, TRUE, &ServiceInfo))
    {
        SetConfigUsage();
        return FALSE;
    }

#ifdef SCDBG
    _tprintf(_T("service name - %s\n"), ServiceInfo.lpServiceName);
    _tprintf(_T("display name - %s\n"), ServiceInfo.lpDisplayName);
    _tprintf(_T("service type - %lu\n"), ServiceInfo.dwServiceType);
    _tprintf(_T("start type - %lu\n"), ServiceInfo.dwStartType);
    _tprintf(_T("error control - %lu\n"), ServiceInfo.dwErrorControl);
    _tprintf(_T("Binary path - %s\n"), ServiceInfo.lpBinaryPathName);
    _tprintf(_T("load order group - %s\n"), ServiceInfo.lpLoadOrderGroup);
    _tprintf(_T("tag - %lu\n"), ServiceInfo.dwTagId);
    _tprintf(_T("dependencies - %s\n"), ServiceInfo.lpDependencies);
    _tprintf(_T("account start name - %s\n"), ServiceInfo.lpServiceStartName);
    _tprintf(_T("account password - %s\n"), ServiceInfo.lpPassword);
#endif

    hManager = OpenSCManager(NULL,
                             NULL,
                             SC_MANAGER_CONNECT);
    if (hManager == NULL)
    {
        _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
        bResult = FALSE;
        goto done;
    }

    hService = OpenService(hManager,
                           ServiceInfo.lpServiceName,
                           SERVICE_CHANGE_CONFIG);
    if (hService == NULL)
    {
        _tprintf(_T("[SC] OpenService FAILED %lu:\n\n"), GetLastError());
        bResult = FALSE;
        goto done;
    }

    if (!ChangeServiceConfig(hService,
                             ServiceInfo.dwServiceType,
                             ServiceInfo.dwStartType,
                             ServiceInfo.dwErrorControl,
                             ServiceInfo.lpBinaryPathName,
                             ServiceInfo.lpLoadOrderGroup,
                             ServiceInfo.bTagId ? &ServiceInfo.dwTagId : NULL,
                             ServiceInfo.lpDependencies,
                             ServiceInfo.lpServiceStartName,
                             ServiceInfo.lpPassword,
                             ServiceInfo.lpDisplayName))
    {
        _tprintf(_T("[SC] ChangeServiceConfig FAILED %lu:\n\n"), GetLastError());
        bResult = FALSE;
        goto done;
    }

    _tprintf(_T("[SC] ChangeServiceConfig SUCCESS\n\n"));

done:
    if (bResult == FALSE)
        ReportLastError();

    if (hService)
        CloseServiceHandle(hService);

    if (hManager)
        CloseServiceHandle(hManager);

    return bResult;
}
Exemple #13
0
BOOL QueryConfig(LPCTSTR ServiceName)
{
    SC_HANDLE hManager = NULL;
    SC_HANDLE hService = NULL;
    BOOL bResult = TRUE;
    DWORD cbBytesNeeded = 0;
    LPQUERY_SERVICE_CONFIG pServiceConfig = NULL;
    LPWSTR lpPtr;
    SSIZE_T nLen, i;

#ifdef SCDBG
    _tprintf(_T("service to show configuration - %s\n\n"), ServiceName);
#endif

    hManager = OpenSCManager(NULL,
                             NULL,
                             SC_MANAGER_CONNECT);
    if (hManager == NULL)
    {
        _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
        bResult = FALSE;
        goto done;
    }

    hService = OpenService(hManager, ServiceName, SERVICE_QUERY_CONFIG);
    if (hService == NULL)
    {
        _tprintf(_T("[SC] OpenService FAILED %lu:\n\n"), GetLastError());
        bResult = FALSE;
        goto done;
    }

    if (!QueryServiceConfig(hService,
                            NULL,
                            0,
                            &cbBytesNeeded))
    {
        if (cbBytesNeeded == 0)
        {
            _tprintf(_T("[SC] QueryServiceConfig FAILED %lu:\n\n"), GetLastError());
            bResult = FALSE;
            goto done;
        }
    }

    pServiceConfig = HeapAlloc(GetProcessHeap(), 0, cbBytesNeeded);
    if (pServiceConfig == NULL)
    {
        SetLastError(ERROR_OUTOFMEMORY);
        _tprintf(_T("[SC] HeapAlloc FAILED %lu:\n\n"), GetLastError());
        bResult = FALSE;
        goto done;
    }

    if (!QueryServiceConfig(hService,
                            pServiceConfig,
                            cbBytesNeeded,
                            &cbBytesNeeded))
    {
        _tprintf(_T("[SC] QueryServiceConfig FAILED %lu:\n\n"), GetLastError());
        bResult = FALSE;
        goto done;
    }

    _tprintf(_T("[SC] QueryServiceConfig SUCCESS\n\n"));

    _tprintf(_T("SERVICE_NAME: %s\n"), ServiceName);
    _tprintf(_T("        TYPE               : %-3lx "), pServiceConfig->dwServiceType);
    switch (pServiceConfig->dwServiceType)
    {
        case SERVICE_KERNEL_DRIVER:
            _tprintf(_T("KERNEL_DRIVER\n"));
            break;

        case SERVICE_FILE_SYSTEM_DRIVER:
            _tprintf(_T("FILE_SYSTEM_DRIVER\n"));
            break;

        case SERVICE_WIN32_OWN_PROCESS:
            _tprintf(_T("WIN32_OWN_PROCESS\n"));
            break;

        case SERVICE_WIN32_SHARE_PROCESS:
            _tprintf(_T("WIN32_SHARE_PROCESS\n"));
            break;

        case SERVICE_WIN32_OWN_PROCESS + SERVICE_INTERACTIVE_PROCESS:
            _tprintf(_T("WIN32_OWN_PROCESS (interactive)\n"));
            break;

        case SERVICE_WIN32_SHARE_PROCESS + SERVICE_INTERACTIVE_PROCESS:
            _tprintf(_T("WIN32_SHARE_PROCESS (interactive)\n"));
            break;

        default:
            _tprintf(_T("\n"));
            break;
    }

    _tprintf(_T("        START_TYPE         : %-3lx "), pServiceConfig->dwStartType);
    switch (pServiceConfig->dwStartType)
    {
        case SERVICE_BOOT_START:
            _tprintf(_T("BOOT_START\n"));
            break;

        case SERVICE_SYSTEM_START:
            _tprintf(_T("SYSTEM_START\n"));
            break;

        case SERVICE_AUTO_START:
            _tprintf(_T("AUTO_START\n"));
            break;

        case SERVICE_DEMAND_START:
            _tprintf(_T("DEMAND_START\n"));
            break;

        case SERVICE_DISABLED:
            _tprintf(_T("DISABLED\n"));
            break;

        default:
            _tprintf(_T("\n"));
            break;
    }

    _tprintf(_T("        ERROR_CONTROL      : %-3lx "), pServiceConfig->dwErrorControl);
    switch (pServiceConfig->dwErrorControl)
    {
        case SERVICE_ERROR_IGNORE:
            _tprintf(_T("IGNORE\n"));
            break;

        case SERVICE_ERROR_NORMAL:
            _tprintf(_T("NORMAL\n"));
            break;

        case SERVICE_ERROR_SEVERE:
            _tprintf(_T("SEVERE\n"));
            break;

        case SERVICE_ERROR_CRITICAL:
            _tprintf(_T("CRITICAL\n"));
            break;

        default:
            _tprintf(_T("\n"));
            break;
    }

    _tprintf(_T("        BINARY_PATH_NAME   : %s\n"), pServiceConfig->lpBinaryPathName);
    _tprintf(_T("        LOAD_ORDER_GROUP   : %s\n"), pServiceConfig->lpLoadOrderGroup);
    _tprintf(_T("        TAG                : %lu\n"), pServiceConfig->dwTagId);
    _tprintf(_T("        DISPLAY_NAME       : %s\n"), pServiceConfig->lpDisplayName);
    _tprintf(_T("        DEPENDENCIES       : "));
    lpPtr = pServiceConfig->lpDependencies;
    i = 0;
    while (*lpPtr != _T('\0'))
    {
       nLen = _tcslen(lpPtr);
       if (i != 0)
           _tprintf(_T("\n                           : "));
       _tprintf(_T("%s"), lpPtr);
       lpPtr = lpPtr + nLen + 1;
       i++;
    }
    _tprintf(_T("\n"));

    _tprintf(_T("        SERVICE_START_NAME : %s\n"), pServiceConfig->lpServiceStartName);

done:
    if (bResult == FALSE)
        ReportLastError();

    if (pServiceConfig != NULL)
        HeapFree(GetProcessHeap(), 0, pServiceConfig);

    if (hService)
        CloseServiceHandle(hService);

    if (hManager)
        CloseServiceHandle(hManager);

    return bResult;
}
Exemple #14
0
static BOOL
EnumServices(ENUM_SERVICE_STATUS_PROCESS **pServiceStatus,
             DWORD ServiceType,
             DWORD ServiceState)
{
    SC_HANDLE hSCManager;
    DWORD BufSize = 0;
    DWORD BytesNeeded = 0;
    DWORD ResumeHandle = 0;
    DWORD NumServices = 0;
    DWORD Ret;

    hSCManager = OpenSCManager(NULL,
                               NULL,
                               SC_MANAGER_ENUMERATE_SERVICE);
    if (hSCManager == NULL)
    {
        ReportLastError();
        return FALSE;
    }

    Ret = EnumServicesStatusEx(hSCManager,
                               SC_ENUM_PROCESS_INFO,
                               ServiceType,
                               ServiceState,
                               (LPBYTE)*pServiceStatus,
                               BufSize,
                               &BytesNeeded,
                               &NumServices,
                               &ResumeHandle,
                               0);

    if ((Ret == 0) && (GetLastError() == ERROR_MORE_DATA))
    {
        *pServiceStatus = (ENUM_SERVICE_STATUS_PROCESS *)
                          HeapAlloc(GetProcessHeap(),
                                    0,
                                    BytesNeeded);
        if (*pServiceStatus != NULL)
        {
            if (EnumServicesStatusEx(hSCManager,
                                     SC_ENUM_PROCESS_INFO,
                                     ServiceType,
                                     ServiceState,
                                     (LPBYTE)*pServiceStatus,
                                     BytesNeeded,
                                     &BytesNeeded,
                                     &NumServices,
                                     &ResumeHandle,
                                     0))
            {
                CloseServiceHandle(hSCManager);
                return NumServices;
            }
        }
    }

    ReportLastError();
    if (*pServiceStatus)
        HeapFree(GetProcessHeap(), 0, *pServiceStatus);

    CloseServiceHandle(hSCManager);

    return NumServices;
}