To load your own driver with vc, perform the following steps: 1. to load the driver service, use the following functions: SC _HANDLEWINAPIOpenSCManagerA (_ handler ,__ in_optLPCSTRlpDatabaseName ,__ inDWORDdwDesiredAccess ); create driver service SC _HANDLEWI
To load your own driver with vc, perform the following steps: 1. to load the driver service, use the following functions: SC _HANDLE WINAPI OpenSCManagerA (_ in_optLPCSTRlpMachineName, _ in_optLPCSTRlpDatabaseName, _ inDWORDdwDesiredAccess); // create the driver service SC _HANDLE WI
Load with vc ++YourselfOfDriverProgramThe procedure is as follows:
1. LoadDriverService
The following functions are mainly used:
- SC _HANDLE
- WINAPI
- OpenSCManagerA (
- _ In_opt LPCSTR lpMachineName,
- _ In_opt LPCSTR lpDatabaseName,
- _ In DWORD dwDesiredAccess
- );
- // CreateDriverService
- 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
- );
- // OpenDriverService
- SC _HANDLE
- WINAPI
- OpenServiceA (
- _ In SC _HANDLE hSCManager,
- _ In LPCSTR lpServiceName,
- _ In DWORD dwDesiredAccess
- );
- // StartDriverService
- BOOL
- WINAPI
- StartServiceA (
- _ In SC _HANDLE hService,
- _ In DWORD dwNumServiceArgs,
- _ In_ecount_opt (dwNumServiceArgs)
- LPCSTR * lpServiceArgVectors
- );
Call the OpenSCManager function to create a connection to the Service Control Manager, call CreateService to create a service, and call OpenService and StartService to open the service and start the service respectively.
After the service is started, you can use the CreateFile function to accessDriverProgramThe Code is as follows:
1. StartDriverServiceProgram
- // StartDriverService
- 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, // This isDriverThe service name is "MyWinRing0". You can customize any name.
- MYWINRIN0, // This isDriverService display name, which can be customized. Here I select the same name as above
- SERVICE_ALL_ACCESS,
- SERVICE_KERNEL_DRIVER,
- SERVICE_DEMAND_START,
- SERVICE_ERROR_NORMAL,
- GDriverPath ,//DriverStorage path, here and the ApplicationProgramSame directory
- NULL,
- NULL,
- NULL,
- NULL,
- NULL
- );
- CloseServiceHandle (hService); // close the handle after it is successfully created.
- HService1 = OpenService (hSCManager, MYWINRIN0, SERVICE_ALL_ACCESS); // open the service created above, named "MyWinRing0"
- Int result = StartService (hService1, 0, NULL); // start the service
- CloseServiceHandle (hSCManager );
- CloseServiceHandle (hService1 );
- }
2. Open
DriverService
- //DriverAfter the service is started, openDriver
- BOOL OpenDriver ()
- {
- Char message [256];
- GHandle = CreateFile (
- _ T ("\\\\\ \") OLS_DRIVER_ID, // the name of this place must beDriverProgramThe IoCreateDevice function is used to create a device.
- GENERIC_READ | GENERIC_WRITE,
- 0,
- NULL,
- OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL,
- NULL
- );
- Int code = GetLastError ();
- If(GHandle = INVALID_HANDLE_VALUE)
- {
- ReturnFALSE;
- }
- ReturnTRUE;
- }
3. Stop
Driver
- // StopDriver
- 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 );
- }
- ReturnRCode;
- }
4. Uninstall
Driver
- // UninstallDriver
- 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 );
- }
- ReturnRCode;
- }
Complete example
ProgramDownload: http://download.csdn.net/detail/xiaibiancheng/5449887