阅读:1599回复:4
通过程序动态安装/卸载驱动程序
下面是我写的用来安装/卸载驱动程序的代码.经在Win2K,XP中测试,工作正常.
|
|
沙发#
发布于:2004-06-21 17:39
好样的,我拿去测试一下。
|
|
|
板凳#
发布于:2004-06-17 19:34
例子:
BOOL InstallDriver() { _TCHAR System32Directory[_MAX_PATH]; if(0 == GetSystemDirectory(System32Directory,_MAX_PATH)) { ::MessageBox(NULL,"不能找到系统目录","错误",MB_OK); return FALSE; } CString BinaryPath = System32Directory; BinaryPath = BinaryPath + "\\Drivers\\DirectIo.sys"; CDriver driver; BOOL bRet; bRet = driver.Init(); if(bRet == FALSE) return FALSE; bRet = driver.Install(BinaryPath.GetBuffer(BinaryPath.GetLength()),"DirectIo"); if(bRet == FALSE) { //Try again. driver.Stop("DirectIo"); driver.Uninstall("DirectIo"); bRet = driver.Install(BinaryPath.GetBuffer(BinaryPath.GetLength()),"DirectIo"); if(bRet == FALSE) { driver.Close(); return FALSE; } } bRet = driver.Start("DirectIo"); if(bRet == FALSE) { driver.Close(); return FALSE; } HANDLE hDirectIo; hDirectIo = CreateFile("\\\\.\\giveio",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); if(hDirectIo == INVALID_HANDLE_VALUE) { ::MessageBox(NULL,"打开驱动程序错误!","错误",MB_OK); driver.Stop("DirectIo"); driver.Uninstall("DirectIo"); driver.Close(); return FALSE; } CloseHandle(hDirectIo); driver.Stop("DirectIo"); driver.Uninstall("DirectIo"); driver.Close(); return TRUE; } |
|
地板#
发布于:2004-06-17 19:33
.Cpp文件
#include "stdafx.h" #include "Driver.h" CDriver::CDriver() { hSCObject = NULL; } CDriver::~CDriver() { } BOOL CDriver::Init() { hSCObject = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); if(hSCObject == INVALID_HANDLE_VALUE) { GetLastError(); return FALSE; } return TRUE; } BOOL CDriver::Install(LPCSTR lpBinaryPathName,LPCSTR lpDisplayName) { SC_HANDLE schService; schService = CreateService( hSCObject, lpDisplayName, lpDisplayName, SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, lpBinaryPathName, NULL, NULL, NULL, NULL, NULL); if(schService == NULL) { GetLastError(); return FALSE; } CloseServiceHandle(schService); return TRUE; } BOOL CDriver::Start(LPCSTR lpServiceName) { SC_HANDLE schService; schService = OpenService(hSCObject,lpServiceName,SERVICE_ALL_ACCESS); if(schService == NULL) { GetLastError(); return FALSE; } if(StartService(schService,0,NULL) == NULL) { GetLastError(); CloseServiceHandle(schService); return FALSE; } CloseServiceHandle(schService); return TRUE; } BOOL CDriver::Stop(LPCSTR lpServiceName) { SC_HANDLE schService; SERVICE_STATUS ServiceStatus; schService = OpenService(hSCObject,lpServiceName,SERVICE_ALL_ACCESS); if(schService == NULL) { GetLastError(); return FALSE; } if(ControlService(schService,SERVICE_CONTROL_STOP,&ServiceStatus) == NULL) { GetLastError(); CloseServiceHandle(schService); return FALSE; } CloseServiceHandle(schService); return TRUE; } BOOL CDriver::Uninstall(LPCSTR lpServiceName) { SC_HANDLE schService; schService = OpenService(hSCObject,lpServiceName,SERVICE_ALL_ACCESS); if(schService == NULL) { GetLastError(); return FALSE; } if(DeleteService(schService) == NULL) { GetLastError(); CloseServiceHandle(schService); return FALSE; } CloseServiceHandle(schService); return TRUE; } void CDriver::Close() { CloseServiceHandle(hSCObject); } |
|
地下室#
发布于:2004-06-17 19:32
头文件:
#ifndef DRIVER_H #define DRIVER_H #include "WinSvc.h" BOOL InstallDriver(); class CDriver { public: CDriver(); ~CDriver(); public: SC_HANDLE hSCObject; public: BOOL Init(); BOOL Install(LPCSTR lpBinaryPathName,LPCSTR lpDisplayName); BOOL Start(LPCSTR lpServiceName); BOOL Stop(LPCSTR lpServiceName); BOOL Uninstall(LPCSTR lpServiceName); void Close(); }; #endif |
|