Vc ++ load and uninstall your own driver

Source: Internet
Author: User
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:

  1. SC _HANDLE
  2. WINAPI
  3. OpenSCManagerA (
  4. _ In_opt LPCSTR lpMachineName,
  5. _ In_opt LPCSTR lpDatabaseName,
  6. _ In DWORD dwDesiredAccess
  7. );
  8. // CreateDriverService
  9. SC _HANDLE
  10. WINAPI
  11. CreateServiceA (
  12. _ In SC _HANDLE hSCManager,
  13. _ In LPCSTR lpServiceName,
  14. _ In_opt LPCSTR lpDisplayName,
  15. _ In DWORD dwDesiredAccess,
  16. _ In DWORD dwServiceType,
  17. _ In DWORD dwStartType,
  18. _ In DWORD dwErrorControl,
  19. _ In_opt LPCSTR lpBinaryPathName,
  20. _ In_opt LPCSTR lpLoadOrderGroup,
  21. _ Out_opt LPDWORD lpdwTagId,
  22. _ In_opt LPCSTR lpDependencies,
  23. _ In_opt LPCSTR lpServiceStartName,
  24. _ In_opt LPCSTR lpPassword
  25. );
  26. // OpenDriverService
  27. SC _HANDLE
  28. WINAPI
  29. OpenServiceA (
  30. _ In SC _HANDLE hSCManager,
  31. _ In LPCSTR lpServiceName,
  32. _ In DWORD dwDesiredAccess
  33. );
  34. // StartDriverService
  35. BOOL
  36. WINAPI
  37. StartServiceA (
  38. _ In SC _HANDLE hService,
  39. _ In DWORD dwNumServiceArgs,
  40. _ In_ecount_opt (dwNumServiceArgs)
  41. LPCSTR * lpServiceArgVectors
  42. );

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

  1. // StartDriverService
  2. Void LoadDriver ()
  3. {
  4. SC _HANDLE hSCManager = NULL;
  5. SC _HANDLE hService = NULL;
  6. SC _HANDLE hService1 = NULL;
  7. HSCManager = OpenSCManager (NULL, NULL, SC _MANAGER_ALL_ACCESS );
  8. HService = CreateService (hSCManager,
  9. MYWINRIN0, // This isDriverThe service name is "MyWinRing0". You can customize any name.
  10. MYWINRIN0, // This isDriverService display name, which can be customized. Here I select the same name as above
  11. SERVICE_ALL_ACCESS,
  12. SERVICE_KERNEL_DRIVER,
  13. SERVICE_DEMAND_START,
  14. SERVICE_ERROR_NORMAL,
  15. GDriverPath ,//DriverStorage path, here and the ApplicationProgramSame directory
  16. NULL,
  17. NULL,
  18. NULL,
  19. NULL,
  20. NULL
  21. );
  22. CloseServiceHandle (hService); // close the handle after it is successfully created.
  23. HService1 = OpenService (hSCManager, MYWINRIN0, SERVICE_ALL_ACCESS); // open the service created above, named "MyWinRing0"
  24. Int result = StartService (hService1, 0, NULL); // start the service
  25. CloseServiceHandle (hSCManager );
  26. CloseServiceHandle (hService1 );
  27. }

2. Open DriverService

  1. //DriverAfter the service is started, openDriver
  2. BOOL OpenDriver ()
  3. {
  4. Char message [256];
  5. GHandle = CreateFile (
  6. _ T ("\\\\\ \") OLS_DRIVER_ID, // the name of this place must beDriverProgramThe IoCreateDevice function is used to create a device.
  7. GENERIC_READ | GENERIC_WRITE,
  8. 0,
  9. NULL,
  10. OPEN_EXISTING,
  11. FILE_ATTRIBUTE_NORMAL,
  12. NULL
  13. );
  14. Int code = GetLastError ();
  15. If(GHandle = INVALID_HANDLE_VALUE)
  16. {
  17. ReturnFALSE;
  18. }
  19. ReturnTRUE;
  20. }

3. Stop Driver

  1. // StopDriver
  2. BOOL StopDriver (SC _HANDLE hSCManager, LPCTSTR DriverId)
  3. {
  4. SC _HANDLE hService = NULL;
  5. BOOL rCode = FALSE;
  6. SERVICE_STATUS serviceStatus;
  7. DWORD error = NO_ERROR;
  8. HService = OpenService (hSCManager, DriverId, SERVICE_ALL_ACCESS );
  9. If(HService! = NULL)
  10. {
  11. RCode = ControlService (hService, SERVICE_CONTROL_STOP, & serviceStatus );
  12. Error = GetLastError ();
  13. CloseServiceHandle (hService );
  14. }
  15. ReturnRCode;
  16. }

4. Uninstall Driver

  1. // UninstallDriver
  2. BOOL RemoveDriver (SC _HANDLE hSCManager, LPCTSTR DriverId)
  3. {
  4. SC _HANDLE hService = NULL;
  5. BOOL rCode = FALSE;
  6. HService = OpenService (hSCManager, DriverId, SERVICE_ALL_ACCESS );
  7. If(HService = NULL)
  8. {
  9. RCode = TRUE;
  10. }
  11. Else
  12. {
  13. RCode = DeleteService (hService );
  14. CloseServiceHandle (hService );
  15. }
  16. ReturnRCode;
  17. }

Complete example ProgramDownload: http://download.csdn.net/detail/xiaibiancheng/5449887

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.