1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
| #include <stdio.h> #include <windows.h> #pragma warning(disable : 4996) class cDrvCtrl { public: cDrvCtrl()//初始化各个变量 { m_pSysPath = NULL; m_pServiceName = NULL; m_pDisplayName = NULL; m_hSCManager = NULL; m_hService = NULL; m_hDriver = INVALID_HANDLE_VALUE; } ~cDrvCtrl()//清除垃圾 { CloseServiceHandle(m_hService); CloseServiceHandle(m_hSCManager); CloseHandle(m_hDriver); } public: DWORD m_dwLastError; //最后的错误 PCHAR m_pSysPath; //驱动路径 PCHAR m_pServiceName; //服务名 PCHAR m_pDisplayName; //显示名 HANDLE m_hDriver; //驱动句柄 SC_HANDLE m_hSCManager; //SCM句柄 SC_HANDLE m_hService; //服务句柄 BOOL Install(PCHAR pSysPath, PCHAR pServiceName, PCHAR pDisplayName); //安装驱动服务 BOOL Start();//启动驱动服务 BOOL Stop();//停止驱动服务 BOOL Remove();//移除驱动服务 BOOL Open(PCHAR pLinkName); //打开驱动句柄 BOOL IoControl(DWORD dwIoCode, PVOID InBuff, DWORD InBuffLen, PVOID OutBuff, DWORD OutBuffLen, DWORD* RealRetBytes);//IO控制 BOOL GetSvcHandle(PCHAR pServiceName); DWORD CTL_CODE_GEN(DWORD lngFunction); }; //打开已经存在的服务 BOOL cDrvCtrl::GetSvcHandle(PCHAR pServiceName) { m_pServiceName = pServiceName; m_hSCManager = OpenSCManagerA(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (NULL == m_hSCManager) { m_dwLastError = GetLastError(); return FALSE; } m_hService = OpenServiceA(m_hSCManager, m_pServiceName, SERVICE_ALL_ACCESS); if (NULL == m_hService) { CloseServiceHandle(m_hSCManager); return FALSE; } else { return TRUE; } }
//安装服务服务 BOOL cDrvCtrl::Install(PCHAR pSysPath, PCHAR pServiceName, PCHAR pDisplayName) { m_pSysPath = pSysPath; m_pServiceName = pServiceName; m_pDisplayName = pDisplayName; m_hSCManager = OpenSCManagerA(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (NULL == m_hSCManager) { m_dwLastError = GetLastError(); return FALSE; } m_hService = CreateServiceA(m_hSCManager, m_pServiceName, m_pDisplayName, SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, m_pSysPath, NULL, NULL, NULL, NULL, NULL); if (NULL == m_hService) { m_dwLastError = GetLastError(); if (ERROR_SERVICE_EXISTS == m_dwLastError) { m_hService = OpenServiceA(m_hSCManager, m_pServiceName, SERVICE_ALL_ACCESS); if (NULL == m_hService) { CloseServiceHandle(m_hSCManager); return FALSE; } } else { CloseServiceHandle(m_hSCManager); return FALSE; } } return TRUE; }
//启动服务 BOOL cDrvCtrl::Start() { if (!StartServiceA(m_hService, NULL, NULL)) { m_dwLastError = GetLastError(); return FALSE; } return TRUE; }
//停止服务 BOOL cDrvCtrl::Stop() { SERVICE_STATUS ss; if (!ControlService(m_hService, SERVICE_CONTROL_STOP, &ss)) { m_dwLastError = GetLastError(); return FALSE; } return TRUE;
}
//移除服务 BOOL cDrvCtrl::Remove() { if (!DeleteService(m_hService)) { m_dwLastError = GetLastError(); return FALSE; } return TRUE; }
//打开驱动的符号链接 BOOL cDrvCtrl::Open(PCHAR pLinkName)//example: \\\\.\\xxoo { if (m_hDriver != INVALID_HANDLE_VALUE) return TRUE; m_hDriver = CreateFileA(pLinkName, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if (m_hDriver != INVALID_HANDLE_VALUE) return TRUE; else return FALSE; }
DWORD cDrvCtrl::CTL_CODE_GEN(DWORD lngFunction) { return (FILE_DEVICE_UNKNOWN * 65536) | (FILE_ANY_ACCESS * 16384) | (lngFunction * 4) | METHOD_BUFFERED; }
//和驱动实现通信的核心函数 BOOL cDrvCtrl::IoControl(DWORD dwIoCode, PVOID InBuff, DWORD InBuffLen, PVOID OutBuff, DWORD OutBuffLen, DWORD* RealRetBytes) { DWORD dw; BOOL b = DeviceIoControl(m_hDriver, CTL_CODE_GEN(dwIoCode), InBuff, InBuffLen, OutBuff, OutBuffLen, &dw, NULL); if (RealRetBytes) *RealRetBytes = dw; return b; } //测试 void GetAppPath(char* szCurFile) //最后带斜杠 { GetModuleFileNameA(0, szCurFile, MAX_PATH); for (SIZE_T i = strlen(szCurFile) - 1;i >= 0;i--) { if (szCurFile[i] == '\\') { szCurFile[i + 1] = '\0'; break; } } } int main() { BOOL b; cDrvCtrl dc; //设置驱动名称 char szSysFile[MAX_PATH] = { 0 }; char szSvcLnkName[] = "KrnlHW64";; GetAppPath(szSysFile); strcat(szSysFile, "KrnlHW64.sys");//这里吗,这里是一个原因 //安装并启动驱动 b = dc.Install(szSysFile, szSvcLnkName, szSvcLnkName); b = dc.Start(); printf("%d\n", b); printf("LoadDriver=%d\n", b); //“打开”驱动的符号链接 CHAR sym[] = "\\\\.\\KrnlHW64"; dc.Open(sym); //使用控制码控制驱动(0x800:传入一个数字并返回一个数字) DWORD x = 100, y = 0, z = 0; dc.IoControl(0x800, &x, sizeof(x), &y, sizeof(y), &z); printf("INPUT=%ld\nOUTPUT=%ld\nReturnBytesLength=%ld\n", x, y, z); //使用控制码控制驱动(0x801:在DBGVIEW里显示HELLOWORLD) dc.IoControl(0x801, 0, 0, 0, 0, 0); //关闭符号链接句柄 CloseHandle(dc.m_hDriver); //停止并卸载驱动 b = dc.Stop(); b = dc.Remove(); printf("UnloadDriver=%d\n", b); getchar(); return 0; }
|