C++加载驱动实例
2012-09-19 00:26:11
驱动程序可以是针对某一特定硬件的,为系统提供管理硬件的各种功能;也可以是针对系统设备的,对系统的输入输出做一些处理,实现特定的功能,比如当软件要做的事用应用程序无法实现或者难以实现某种功能时,但驱动程序可以实现,则需要驱动程序。
以下就是一个C++以服务的方式,加载驱动的实例
2 4 #include<windows.h> 5 #include "stdio.h" 6 void installDvr();//安装驱动 7 void startDvr();//启动驱动 8 void stopDvr();//停止驱动 9 void unloadDvr();//卸载驱动 10 void main() 11 { 12 13 installDvr();//安装驱动 14 startDvr();//启动驱动 15 16 stopDvr();//停止驱动 17 unloadDvr();//卸载驱动 18 19 20 21 } 22 23 void installDvr()//安装 24 { 25 SC_HANDLE schSCManager; 26 schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); 27 if (schSCManager) 28 { 29 SC_HANDLE schService = CreateService( schSCManager, 30 31 32 33 "babaHelloDDKk", 34 35 36 37 "babaMyHelloDDKk", 38 39 40 41 SERVICE_ALL_ACCESS, 42 43 44 45 SERVICE_KERNEL_DRIVER, //创建的服务类型1为驱动服务 46 47 48 49 SERVICE_DEMAND_START, //用于当有进程调用StartService 函数时由服务控制管理器(SCM)启动的服务 50 51 。查询Starting Services on Demand以获取更多信息。 52 53 54 55 SERVICE_ERROR_IGNORE, 56 57 58 59 "e:\\s\\HelloDDK.sys",//驱动文件存放路径 60 61 62 63 NULL, 64 65 66 67 NULL, 68 69 70 71 NULL, 72 73 74 75 NULL, 76 77 78 79 NULL); 80 CloseServiceHandle(schService); //创建完记得释放句柄 81 if(schService) 82 { 83 printf("按装服务成功\n"); 84 85 } 86 else 87 { 88 printf("按装服务失败\n"); 89 } 90 CloseServiceHandle(schSCManager); 91 } 92 93 94 } 95 void startDvr()//启动 96 { 97 SC_HANDLE schSCManager; 98 SC_HANDLE hs; 99 schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); 100 if(schSCManager) 101 { 102 hs=OpenService(schSCManager, "babaHelloDDKk", SERVICE_ALL_ACCESS); //打开服 103 104 务 105 if (hs) 106 { 107 StartService(hs,0,0); 108 printf("启动服务成功\n"); 109 110 CloseServiceHandle(hs); 111 } 112 CloseServiceHandle(schSCManager); 113 } 114 } 115 void stopDvr()//停止 116 { 117 SC_HANDLE schSCManager; 118 SC_HANDLE hs; 119 schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); 120 if(schSCManager) 121 { 122 hs=OpenService(schSCManager, "babaHelloDDKk", SERVICE_ALL_ACCESS); //打开服 123 124 务 125 if (hs) 126 { 127 SERVICE_STATUS status; 128 int num=0; 129 130 QueryServiceStatus(hs, &status); 131 if (status.dwCurrentState != SERVICE_STOPPED && 132 133 status.dwCurrentState != SERVICE_STOP_PENDING) 134 { 135 ControlService(hs,SERVICE_CONTROL_STOP, &status); 136 do 137 { 138 Sleep(50); 139 num++; 140 QueryServiceStatus(hs, &status); 141 }while (status.dwCurrentState != SERVICE_STOPPED || 142 143 num>80); 144 } 145 146 if(num>80) 147 { 148 printf("停止服务失败\n"); 149 } 150 else 151 { 152 printf("停止服务成功\n"); 153 } 154 CloseServiceHandle(hs); 155 } 156 CloseServiceHandle(schSCManager); 157 } 158 159 } 160 void unloadDvr()//卸载 161 { 162 SC_HANDLE schSCManager; 163 SC_HANDLE hs; 164 schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); 165 if(schSCManager) 166 { 167 hs=OpenService(schSCManager, "babaHelloDDKk", SERVICE_ALL_ACCESS); //打开服 168 169 务 170 if (hs) 171 { 172 bool a = DeleteService(hs); //删除服务 173 if (!a) 174 { 175 printf("删除服务失败\n"); 176 } 177 else 178 { 179 printf("已删除服务\n"); 180 } 181 182 CloseServiceHandle(hs);//释放完后可完服务可从服务表中消失 释放前是 183 184 } 186 CloseServiceHandle(schSCManager); 187 } 188 189 }