zoukankan      html  css  js  c++  java
  • 代码实现文件驱动的安装 (转载)

    转载:http://blog.csdn.net/anda0109/article/details/50172305

    .h文件

    #ifndef __H_FILTER_H__
    #define __H_FILTER_H__
    
    #include <windows.h>
    #include <winioctl.h>  
    
    /************************************************************************** 
    * 函数名:InstallDriver 
    * 功  能:安装驱动
    * 参  数:const char* lpszDriverName,驱动服务的名称
    *         const char* lpszDriverPath,驱动的全路径
    *         const char* lpszAltitude,  文件驱动的高度
    * 返回值:BOOL ,TRUE为安装成功
    **************************************************************************/
    BOOL InstallDriver(const char* lpszDriverName, const char* lpszDriverPath, const char* lpszAltitude);  
    
    /************************************************************************** 
    * 函数名:StartDriver 
    * 功  能:启动驱动服务
    * 参  数:const char* lpszDriverName,驱动服务名称
    * 返回值:BOOL ,
    **************************************************************************/
    BOOL StartDriver(const char* lpszDriverName);  
    
    /************************************************************************** 
    * 函数名:StopDriver 
    * 功  能:停止驱动服务
    * 参  数:const char* lpszDriverName,驱动服务名称
    * 返回值:BOOL ,TRUE为成功
    **************************************************************************/
    BOOL StopDriver(const char* lpszDriverName);  
    
    /************************************************************************** 
    * 函数名:DeleteDriver 
    * 功  能:卸载驱动服务
    * 参  数:const char* lpszDriverName,驱动服务名称
    * 返回值:BOOL ,TRUE为成功
    **************************************************************************/
    BOOL DeleteDriver(const char* lpszDriverName); 
    
    #endif

    .cpp文件

    #include "stdafx.h"  
    #include "FilterDriverInstall.h"  
    
    
    LPSTR ConvertErrorCodeToString(DWORD ErrorCode)    
    {    
        HLOCAL LocalAddress=NULL;    
        FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_FROM_SYSTEM,  
            NULL,ErrorCode,0,(PTSTR)&LocalAddress,0,NULL);    
        return (LPSTR)LocalAddress;    
    } 
    
    
    BOOL InstallDriver(const char* lpszDriverName, const char* lpszDriverPath, const char* lpszAltitude)  
    {  
        char szTempStr[MAX_PATH];  
        HKEY hKey;  
        DWORD dwData;  
    
        printf("lpszDriverName=%s, lpszDriverPath=%s
    ", lpszDriverName, lpszDriverPath);
        if( NULL==lpszDriverName || NULL==lpszDriverPath )  
        {  
            return FALSE;  
        }   
    
        SC_HANDLE hServiceMgr = NULL;// SCM管理器的句柄  
        SC_HANDLE hService = NULL;     // 驱动程序的服务句柄  
    
        //打开服务控制管理器  
        hServiceMgr = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );  
        if( hServiceMgr == NULL )  
        {  
            CloseServiceHandle(hServiceMgr);  
            return FALSE;  
        }  
    
        //创建驱动所对应的服务  
        hService = CreateService( hServiceMgr,  
            lpszDriverName,    // 驱动程序的在注册表中的名字  
            lpszDriverName, // 注册表驱动程序的DisplayName 值  
            SERVICE_ALL_ACCESS, // 加载驱动程序的访问权限  
            SERVICE_FILE_SYSTEM_DRIVER, // 表示加载的服务是文件系统驱动程序  
            SERVICE_AUTO_START,//SERVICE_DEMAND_START,注册表驱动程序的Start 值
            SERVICE_ERROR_IGNORE, // 注册表驱动程序的ErrorControl 值  
            lpszDriverPath, // 注册表驱动程序的ImagePath
            "FSFilter Activity Monitor",// 注册表驱动程序的Group 值  
            NULL,  
            "FltMgr", // 驱动依赖的服务,注册表驱动程序的DependOnService值  
            NULL,  
            NULL);  
    
        if( hService == NULL )  
        {  
            printf("CreateService failed:%d(%s)", GetLastError(), ConvertErrorCodeToString(GetLastError()));
            if( GetLastError() == ERROR_SERVICE_EXISTS )  
            {  
                CloseServiceHandle(hService);
                CloseServiceHandle(hServiceMgr);
                return TRUE;  
            }  
            else  
            {  
                CloseServiceHandle(hService);
                CloseServiceHandle(hServiceMgr);
                return FALSE;  
            }  
        }  
        CloseServiceHandle(hService);  
        CloseServiceHandle(hServiceMgr);  
    
        //-------------------------------------------------------------------------------------------------------  
        // SYSTEM\CurrentControlSet\Services\DriverName\Instances子健下的键值项  
        //-------------------------------------------------------------------------------------------------------  
        strcpy(szTempStr,"SYSTEM\CurrentControlSet\Services\");  
        strcat(szTempStr,lpszDriverName);  
        strcat(szTempStr,"\Instances");  
        if(RegCreateKeyEx(HKEY_LOCAL_MACHINE,szTempStr, 0, "", TRUE,KEY_ALL_ACCESS, NULL, &hKey,(LPDWORD)&dwData)!=ERROR_SUCCESS)  
        {  
            return FALSE;  
        }  
        // 注册表驱动程序的DefaultInstance 值  
        strcpy(szTempStr,lpszDriverName);  
        strcat(szTempStr," Instance");  
        if(RegSetValueEx(hKey,"DefaultInstance",0,REG_SZ,(CONST BYTE*)szTempStr,(DWORD)strlen(szTempStr))!=ERROR_SUCCESS)  
        {  
            return FALSE;  
        }  
        RegFlushKey(hKey);
        RegCloseKey(hKey);  
    
        //-------------------------------------------------------------------------------------------------------  
        // SYSTEM\CurrentControlSet\Services\DriverName\Instances\DriverName Instance子健下的键值项  
        //-------------------------------------------------------------------------------------------------------  
        strcpy(szTempStr,"SYSTEM\CurrentControlSet\Services\");  
        strcat(szTempStr,lpszDriverName);  
        strcat(szTempStr,"\Instances\");  
        strcat(szTempStr,lpszDriverName);  
        strcat(szTempStr," Instance");  
        if(RegCreateKeyEx(HKEY_LOCAL_MACHINE, szTempStr, 0, "", TRUE,KEY_ALL_ACCESS, NULL, &hKey, (LPDWORD)&dwData) != ERROR_SUCCESS)  
        {  
            return FALSE;  
        }  
        // 注册表驱动程序的Altitude 值  
        strcpy(szTempStr,lpszAltitude);  
        if(RegSetValueEx(hKey, "Altitude", 0, REG_SZ, (CONST BYTE*)szTempStr, (DWORD)strlen(szTempStr)) != ERROR_SUCCESS)  
        {  
            return FALSE;  
        }  
        // 注册表驱动程序的Flags 值  
        dwData=0x0;  
        if(RegSetValueEx(hKey,"Flags",0,REG_DWORD,(CONST BYTE*)&dwData,sizeof(DWORD))!=ERROR_SUCCESS)  
        {  
            return FALSE;  
        }  
        RegFlushKey(hKey);
        RegCloseKey(hKey);  
        
        return TRUE;  
    }  
    
    BOOL StartDriver(const char* lpszDriverName)  
    {  
        SC_HANDLE schManager;  
        SC_HANDLE schService;  
        SERVICE_STATUS svcStatus;  
    
        if(NULL==lpszDriverName)  
        {  
            return FALSE;  
        }  
    
        schManager=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);  
        if(NULL==schManager)  
        {  
            CloseServiceHandle(schManager);  
            return FALSE;  
        }  
        schService=OpenService(schManager,lpszDriverName,SERVICE_ALL_ACCESS);  
        if(NULL==schService)  
        {  
            CloseServiceHandle(schService);  
            CloseServiceHandle(schManager);  
            return FALSE;  
        }  
    
        if(!StartService(schService,0,NULL))  
        {  
            CloseServiceHandle(schService);  
            CloseServiceHandle(schManager);  
            if( GetLastError() == ERROR_SERVICE_ALREADY_RUNNING )  
            {  
                return TRUE;  
            }  
            return FALSE;  
        }  
    
        CloseServiceHandle(schService);  
        CloseServiceHandle(schManager);  
    
        return TRUE;  
    }  
    
    BOOL 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 DeleteDriver(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 _tmain(int argc, _TCHAR* argv[])
    {
        BOOL bRet = InstallDriver("TestMiniFilter", "D:\TestMiniFilter\TestMiniFilter.sys", "370030");
        if(bRet)
        {
            StartDriver("TestMiniFilter");
            printf("driver install success!");
        }
        else
        {
            printf("driver install failed!");
        }
        return 0;
    }
  • 相关阅读:
    C++PRIMER 阅读笔记 第三章
    一个for循环打印二维数组
    递归实现数组求和
    strlen 与 sizeof
    call,apply,bind,this
    js 原型继承
    vue 动画
    vuex学习心得
    vue+elementui dropdown 下拉菜单绑定方法
    vue 生命周期一点学习
  • 原文地址:https://www.cnblogs.com/chechen/p/7417573.html
Copyright © 2011-2022 走看看