1、与驱动程序通信的源码/ Driver.h文件#ifndef _DRIVER_H_#define _DRIVER_H_#include / 为了使用SCM函数class CDriverpublic:/ 构造函数和析构函数/ 构造函数,pszDriverPath为驱动所在目录,pszLinkName为符号连接名字/ 在类的构造函数中,将试图创建或打开服务,CDriver(LPCTSTR pszDriverPath, LPCTSTR pszLinkName);/ 析构函数。在这里,将停止服务,virtual CDriver();/ 属性/ 此驱动是否可用virtual BOOL IsValid()
2、return (m_hSCM != NULL / 操作/ 开启服务。也就是说驱动的DriverEntry函数将被调用virtual BOOL StartDriver();/ 结束服务。即驱动程序的DriverUnload例程将被调用virtual BOOL StopDriver();/ 打开设备,即取得到此驱动的一个句柄virtual BOOL OpenDevice();/ 向设备发送控制代码virtual DWORD IoControl(DWORD nCode, PVOID pInBuffer, DWORD nInCount, PVOID pOutBuffer, DWORD nOutCoun
3、t);/ 实现protected:char m_szLinkName56; / 符号连接名称BOOL m_bStarted; / 指定服务是否启动BOOL m_bCreateService; / 指定是否创建了服务HANDLE m_hSCM; / SCM数据库句柄HANDLE m_hService; / 服务句柄HANDLE m_hDriver; / 设备句柄;CDriver:CDriver(LPCTSTR pszDriverPath, LPCTSTR pszLinkName)strncpy(m_szLinkName, pszLinkName, 55);m_bStarted = FALSE;m
4、_bCreateService = FALSE;m_hSCM = m_hService = NULL;m_hDriver = INVALID_HANDLE_VALUE;/ 打开SCM管理器m_hSCM = :OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);if(m_hSCM = NULL)MessageBox(0, “打开服务控制管理器失败n“, “可能是因为您不拥有Administrator权限n“, 0);return;/ 创建或打开服务m_hService = :CreateService(m_hSCM, m_szLinkName, m_
5、szLinkName, SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, pszDriverPath, NULL, 0, NULL, NULL, NULL);if(m_hService = NULL)/ 创建服务失败,可能是因为服务已经存在,所以还要试图打开它int nError = :GetLastError();if(nError = ERROR_SERVICE_EXISTS | nError = ERROR_SERVICE_MARKED_FOR_DELETE)m_h
6、Service = :OpenService(m_hSCM, m_szLinkName, SERVICE_ALL_ACCESS);elsem_bCreateService = TRUE;CDriver:CDriver()/ 关闭设备句柄if(m_hDriver != INVALID_HANDLE_VALUE):CloseHandle(m_hDriver);/ 如果创建了服务,就将之删除if(m_bCreateService)StopDriver();:DeleteService(m_hService);/ 关闭句柄if(m_hService != NULL):CloseServiceHandl
7、e(m_hService);if(m_hSCM != NULL):CloseServiceHandle(m_hSCM);BOOL CDriver:StartDriver()if(m_bStarted)return TRUE;if(m_hService = NULL)return FALSE;/ 启动服务if(!:StartService(m_hService, 0, NULL)int nError = :GetLastError();if(nError = ERROR_SERVICE_ALREADY_RUNNING)m_bStarted = TRUE;else:DeleteService(m_
8、hService);else/ 启动成功后,等待服务进入运行状态int nTry = 0;SERVICE_STATUS ss;:QueryServiceStatus(m_hService, while(ss.dwCurrentState = SERVICE_START_PENDING :QueryServiceStatus(m_hService, if(ss.dwCurrentState = SERVICE_RUNNING)m_bStarted = TRUE;return m_bStarted;BOOL CDriver:StopDriver()if(!m_bStarted)return TRU
9、E;if(m_hService = NULL)return FALSE;/ 停止服务SERVICE_STATUS ss;if(!:ControlService(m_hService, SERVICE_CONTROL_STOP, else/ 等待服务完全停止运行int nTry = 0;while(ss.dwCurrentState = SERVICE_STOP_PENDING :QueryServiceStatus(m_hService, if(ss.dwCurrentState = SERVICE_STOPPED)m_bStarted = FALSE;return !m_bStarted;B
10、OOL CDriver:OpenDevice()if(m_hDriver != INVALID_HANDLE_VALUE)return TRUE;/ “.“是Win32中定义本地计算机的方法,/ m_szLinkName是设备对象的符号连接名称,后面章节会详细讨论char sz256 = “;wsprintf(sz, “.%s“, m_szLinkName);/ 打开驱动程序所控制设备m_hDriver = :CreateFile(sz,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);r
11、eturn (m_hDriver != INVALID_HANDLE_VALUE);DWORD CDriver:IoControl(DWORD nCode, PVOID pInBuffer, DWORD nInCount, PVOID pOutBuffer, DWORD nOutCount)if(m_hDriver = INVALID_HANDLE_VALUE)return -1;/ 向驱动程序发送控制代码DWORD nBytesReturn;BOOL bRet = :DeviceIoControl(m_hDriver, nCode, pInBuffer, nInCount, pOutBuffer, nOutCount, if(bRet)return nBytesReturn;elsereturn -1;#endif / _DRIVER_H_
Copyright © 2018-2021 Wenke99.com All rights reserved
工信部备案号:浙ICP备20026746号-2
公安局备案号:浙公网安备33038302330469号
本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。