C++ Windows安装服务/驱动

一、DeviceOperator.h

#include 
#include 
#include 

class DeviceOperator
{
public:
	DeviceOperator();
	~DeviceOperator();
	//安装应用服务接口,成功返回 TRUE,失败返回 FALSE
	//Name	要安装的服务名称
	//Path	服务程序路径
	BOOL InstallServiceInterface(const char * Name, const char * Path);
	//卸载服务,成功返回 TRUE,失败返回 FALSE
	//Name 要卸载的服务名称
	void UninstallServiceInterface(const char * Name);

	//安装驱动程序接口,成功返回 TRUE,失败返回 FALSE
	//Name	要安装的驱动名称,驱动也看作服务来安装
	//Path	驱动程序路径
	BOOL InstallMyDriverInterface(const char* lpszDriverName, const char* lpszDriverPath);

	//卸载驱动,成功返回 TRUE,失败返回 FALSE
	//Name 要卸载的驱动名称
	void UninstallMyDriverInterface(const char * Name);

	BOOL StartDriver(const char* lpszDriverName);

	BOOL StopDriver(const char* lpszDriverName);

	BOOL UninstallDriverInterface(const char* lpszDriverName);
	//查看服务是否运行
	// -1:错误
	int IsServiceRunning(const char* lpszServiceName);
};

二、DeviceOperator.cpp

#include "DeviceOperator.h"
#include 

DeviceOperator::DeviceOperator()
{
}

DeviceOperator::~DeviceOperator()
{
}

BOOL DeviceOperator::InstallServiceInterface(const char * Name, const char * Path)
{
	SC_HANDLE	schSCManager;     //SCM驱动管理句柄
	SC_HANDLE  schService;        //驱动程序的服务句柄

	//打开服务控制管理器
	schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
	if (schSCManager == NULL)
	{
		std::cout << "安装驱动失败!" << std::endl;
	}
    
    //创建驱动对应的服务
	schService = CreateService(schSCManager,          // SCManager database
		Name,           // name of service    //驱动程序在注册表中的名字
		Name,           // name to display    //注册表驱动程序的DisplayName值
		SERVICE_ALL_ACCESS,    // desired access  //加载驱动程序的访问权限
		SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS, // service type 
		SERVICE_AUTO_START,  // start type
		SERVICE_ERROR_NORMAL,  // error control type
		Path,            // service's binary        
		NULL,                  // no load ordering group
		NULL,                  // no tag identifier
		NULL,                  // no dependencies
		NULL,                  // LocalSystem account
		NULL                   // no password
		);
    
	DWORD dwRtn;
	if (schService == NULL)
	{
		dwRtn = GetLastError();
		if (dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_EXISTS)
		{
			std::cout << "服务创建失败" << std::endl;
		}
		else
		{
			std::cout << "服务已经创建过" << std::endl;
		}
	}
    
    CloseServiceHandle(schService);
	CloseServiceHandle(schSCManager);

	return NULL == schService ? FALSE : TRUE;
}

void DeviceOperator::UninstallServiceInterface(const char * Name)
{
	SC_HANDLE	schSCManager;
	SC_HANDLE   schService;
	BOOL        ret = 0;
	SERVICE_STATUS  serviceStatus;

	schSCManager = OpenSCManager(NULL,                 // machine (NULL == local)
		NULL,                 // database (NULL == default)
		SC_MANAGER_ALL_ACCESS // access required
		);

	schService = OpenService(schSCManager, Name, SERVICE_ALL_ACCESS);
	if (schService == NULL)
	{
		CloseServiceHandle(schSCManager);
		return;
	}

	//先停止服务,再删除服务
	ret = ControlService(schService, SERVICE_CONTROL_STOP, &serviceStatus);

	ret = DeleteService(schService);

	CloseServiceHandle(schService);
	CloseServiceHandle(schSCManager);
}

BOOL DeviceOperator::InstallMyDriverInterface(const char * Name, const char * Path)
{
    SC_HANDLE	schSCManager;     //SCM驱动管理句柄
    SC_HANDLE  schService;        //驱动程序的服务句柄

    //打开服务控制管理器
    schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
    if (schSCManager == NULL)
    {
        std::cout << "安装驱动失败!" << std::endl;
    }
    
    //创建驱动对应的服务
    schService = CreateService(schSCManager,          // SCManager database
        Name,           // name of service    //驱动程序在注册表中的名字
        Name,           // name to display    //注册表驱动程序的DisplayName值
        SERVICE_ALL_ACCESS,    // desired access  //加载驱动程序的访问权限
        SERVICE_FILE_SYSTEM_DRIVER, // service type 
        SERVICE_AUTO_START,  // start type
        SERVICE_ERROR_NORMAL,  // error control type
        Path,            // service's binary        
        NULL,                  // no load ordering group
        NULL,                  // no tag identifier
        NULL,                  // no dependencies
        NULL,                  // LocalSystem account
        NULL                   // no password
        );
    DWORD dwRtn;
    if (schService == NULL)
    {
        dwRtn = GetLastError();
        if (dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_EXISTS)
        {
            std::cout << "驱动创建失败" << std::endl;
        }
        else
        {
            std::cout << "驱动已经创建过" << std::endl;
        }
    }
    CloseServiceHandle(schService);
    CloseServiceHandle(schSCManager);

    if (NULL == schService)
    {
        return FALSE;
    }

    return TRUE;
}

void DeviceOperator::UninstallMyDriverInterface(const char * Name)
{
    SC_HANDLE	schSCManager;
    SC_HANDLE   schService;
    BOOL        ret = 0;
    SERVICE_STATUS  serviceStatus;

    schSCManager = OpenSCManager(NULL,                 // machine (NULL == local)
        NULL,                 // database (NULL == default)
        SC_MANAGER_ALL_ACCESS // access required
        );

    schService = OpenService(schSCManager, Name, SERVICE_ALL_ACCESS);
    if (schService == NULL)
    {
        CloseServiceHandle(schSCManager);
        return;
    }

    //先停止服务,再删除服务
    ret = ControlService(schService, SERVICE_CONTROL_STOP, &serviceStatus);

    ret = DeleteService(schService);

    CloseServiceHandle(schService);
    CloseServiceHandle(schSCManager);
}

BOOL DeviceOperator::StopDriver(const char* lpszDriverName)
{
	SC_HANDLE        schManager;
	SC_HANDLE        schService;
	SERVICE_STATUS    svcStatus;
	bool            bStopped = false;

	schManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
	if (NULL == schManager)
	{
		return FALSE;
	}
	schService = OpenService(schManager, lpszDriverName, SERVICE_ALL_ACCESS);
	if (NULL == schService)
	{
		CloseServiceHandle(schManager);
		return FALSE;
	}
	if (!ControlService(schService, SERVICE_CONTROL_STOP, &svcStatus) && (svcStatus.dwCurrentState != SERVICE_STOPPED))
	{
		CloseServiceHandle(schService);
		CloseServiceHandle(schManager);
		return FALSE;
	}

	CloseServiceHandle(schService);
	CloseServiceHandle(schManager);

	return TRUE;
}

BOOL DeviceOperator::UninstallDriverInterface(const char* lpszDriverName)
{
	SC_HANDLE        schManager;
	SC_HANDLE        schService;
	SERVICE_STATUS    svcStatus;

	schManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
	if (NULL == schManager)
	{
		return FALSE;
	}
	schService = OpenService(schManager, lpszDriverName, SERVICE_ALL_ACCESS);
	if (NULL == schService)
	{
		CloseServiceHandle(schManager);
		return FALSE;
	}
	ControlService(schService, SERVICE_CONTROL_STOP, &svcStatus);
	if (!DeleteService(schService))
	{
		CloseServiceHandle(schService);
		CloseServiceHandle(schManager);
		return FALSE;
	}
	CloseServiceHandle(schService);
	CloseServiceHandle(schManager);

	return TRUE;
}

int DeviceOperator::IsServiceRunning(const char* lpszServiceName)
{
	SC_HANDLE schSCManager = NULL;
	SC_HANDLE schService = NULL;

	SERVICE_STATUS_PROCESS ssStatus;
	DWORD dwOldCheckPoint = 0;
	DWORD dwStartTickCount = 0;
	DWORD dwWaitTime = 0;
	DWORD dwBytesNeeded = 0;
	int iStatus = -1;

	schSCManager = OpenSCManager(
		NULL,                                // local computer
		NULL,                                // ServicesActive database
		SC_MANAGER_ALL_ACCESS);              // full access rights

	if (NULL == schSCManager)
	{
		return -1;
	}

	// Get a handle to the service.
	schService = OpenService(
		schSCManager,                      // SCM database
		lpszServiceName,                         // name of service
		SERVICE_QUERY_STATUS | SERVICE_ENUMERATE_DEPENDENTS);     // full access

	if (schService == NULL)
	{
		printf("OpenService failed (%d)\n", GetLastError());
		CloseServiceHandle(schSCManager);
	}
    
    // Check the status in case the service is not stopped.
	if (!QueryServiceStatusEx(
		schService,                         // handle to service
		SC_STATUS_PROCESS_INFO,             // information level
		(LPBYTE)&ssStatus,                 // address of structure
		sizeof(SERVICE_STATUS_PROCESS),     // size of structure
		&dwBytesNeeded))                  // size needed if buffer is too small
	{
		printf("QueryServiceStatusEx failed (%d)\n", GetLastError());
		CloseServiceHandle(schService);
		CloseServiceHandle(schSCManager);
		return -1;
	}
	CloseServiceHandle(schService);
	CloseServiceHandle(schSCManager);
	return ssStatus.dwCurrentState;
}

 

你可能感兴趣的:(c++)