用vc加载 自己 的 驱动 程序 主要分为以下几个步骤: 1、加载 驱动 服务 主要要用到以下几个函数 SC_HANDLE WINAPI OpenSCManagerA( __in_optLPCSTRlpMachineName, __in_optLPCSTRlpDatabaseName, __inDWORDdwDesiredAccess ); //创建 驱动 服务 SC_HANDLE WI
用vc++加载自己的驱动程序主要分为以下几个步骤:
1、加载驱动服务
主要要用到以下几个函数
- SC_HANDLE
- WINAPI
- OpenSCManagerA(
- __in_opt LPCSTR lpMachineName,
- __in_opt LPCSTR lpDatabaseName,
- __in DWORD dwDesiredAccess
- );
- //创建驱动服务
- SC_HANDLE
- WINAPI
- CreateServiceA(
- __in SC_HANDLE hSCManager,
- __in LPCSTR lpServiceName,
- __in_opt LPCSTR lpDisplayName,
- __in DWORD dwDesiredAccess,
- __in DWORD dwServiceType,
- __in DWORD dwStartType,
- __in DWORD dwErrorControl,
- __in_opt LPCSTR lpBinaryPathName,
- __in_opt LPCSTR lpLoadOrderGroup,
- __out_opt LPDWORD lpdwTagId,
- __in_opt LPCSTR lpDependencies,
- __in_opt LPCSTR lpServiceStartName,
- __in_opt LPCSTR lpPassword
- );
- //打开驱动服务
- SC_HANDLE
- WINAPI
- OpenServiceA(
- __in SC_HANDLE hSCManager,
- __in LPCSTR lpServiceName,
- __in DWORD dwDesiredAccess
- );
- //启动驱动服务
- BOOL
- WINAPI
- StartServiceA(
- __in SC_HANDLE hService,
- __in DWORD dwNumServiceArgs,
- __in_ecount_opt(dwNumServiceArgs)
- LPCSTR *lpServiceArgVectors
- );
首先调用OpenSCManager函数 建立一个连接到服务控制管理器,然后再调用CreateService创建服务最后调用OpenService和StartService分别打开服务和启动服务。
当服务启动后就可以用CreateFile函数访问驱动程序了代码如下:
1.启动驱动服务程序
- //启动驱动服务
- void LoadDriver()
- {
- SC_HANDLE hSCManager = NULL;
- SC_HANDLE hService = NULL;
- SC_HANDLE hService1 = NULL;
- hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
- hService = CreateService(hSCManager,
- MYWINRIN0, //这是驱动服务名称 这里为"MyWinRing0" 可以自定义任意名称
- MYWINRIN0, //这是驱动服务显示名称,可以自定义任意名称,这里我选择和上面一样
- SERVICE_ALL_ACCESS,
- SERVICE_KERNEL_DRIVER,
- SERVICE_DEMAND_START,
- SERVICE_ERROR_NORMAL,
- gDriverPath,//驱动存放的路径,这里和应用程序同目录
- NULL,
- NULL,
- NULL,
- NULL,
- NULL
- );
- CloseServiceHandle(hService);//上面创建成功后就关闭掉这个句柄
- hService1 = OpenService(hSCManager, MYWINRIN0, SERVICE_ALL_ACCESS);//打开上面创建的服务,名字为"MyWinRing0"
- int result=StartService(hService1, 0, NULL);//启动服务
- CloseServiceHandle(hSCManager);
- CloseServiceHandle(hService1);
- }
2.打开驱动服务
- //驱动服务启动后,打开驱动
- BOOL OpenDriver()
- {
- char message[256];
- gHandle = CreateFile(
- _T("\\\\.\\") OLS_DRIVER_ID,//这个地方的名字必须是驱动程序里面IoCreateDevice函数创建设备是 使用的名字
- GENERIC_READ | GENERIC_WRITE,
- 0,
- NULL,
- OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL,
- NULL
- );
- int code=GetLastError();
- if(gHandle == INVALID_HANDLE_VALUE)
- {
- return FALSE;
- }
- return TRUE;
- }
3.停止驱动
- //停止驱动
- BOOL StopDriver(SC_HANDLE hSCManager,LPCTSTR DriverId)
- {
- SC_HANDLE hService = NULL;
- BOOL rCode = FALSE;
- SERVICE_STATUS serviceStatus;
- DWORD error = NO_ERROR;
- hService = OpenService(hSCManager, DriverId, SERVICE_ALL_ACCESS);
- if(hService != NULL)
- {
- rCode = ControlService(hService, SERVICE_CONTROL_STOP, &serviceStatus);
- error = GetLastError();
- CloseServiceHandle(hService);
- }
- return rCode;
- }
4.卸载驱动
- //卸载驱动
- BOOL RemoveDriver(SC_HANDLE hSCManager, LPCTSTR DriverId)
- {
- SC_HANDLE hService = NULL;
- BOOL rCode = FALSE;
- hService = OpenService(hSCManager, DriverId, SERVICE_ALL_ACCESS);
- if(hService == NULL)
- {
- rCode = TRUE;
- }
- else
- {
- rCode = DeleteService(hService);
- CloseServiceHandle(hService);
- }
- return rCode;
- }
完整示例程序下载:http://download.csdn.net/detail/xiaibiancheng/5449887