Development driven by cainiao 5

Source: Internet
Author: User

 

In the previous 4 sections, we learned a basic driver writing method, which is also an entry-level driver. We will introduce how to load and uninstall drivers at the application layer. There are two methods: 1. Modify the registry and 2. Use SCM-Service Control Manager to manage drivers. Today we will learn the second method, mainly to learn how to load and uninstall drivers.

I used the vs2010 development environment to create a project based on the MFC dialog box.

Drag two buttons, one for loading the driver and the other for detaching the driver.

 

The code for loading the driver is as follows:

// Load the NT driver bool loadntdriver (maid) {bool Bret = false; SC _handle hservicemgr = NULL; // the SCM manager handle SC _handle hserviceddk = NULL; // NT driver Service handle // open service control manager hservicemgr = openscmanager (null, null, SC _manager_all_access); If (hservicemgr = NULL) {// openscmanager failure trace ("openscmanager () faild % d! \ N ", getlasterror (); Bret = false; goto bexit;} else {// openscmanager success trace (" openscmanager () OK! \ N ") ;}// create the service hserviceddk = createservice (hservicemgr, lpdrivername, // name of the driver in the Registry lpdrivername, // The displayname value of the registry driver service_all_access, // The access permission for loading the driver service_kernel_driver, // indicates that the loaded service is the driver service_deman_start, // The start value of the registry driver service_error_ignore, // The errorcontrol value of the registry driver lpdriverpathname, // The ImagePath value of the registry driver is null, null ); DWORD dwrtn; // determines whether the service fails if (hservi Ceddk = NULL) {dwrtn = getlasterror (); If (dwrtn! = Error_io_pending & dwrtn! = Error_service_exists) {// trace ("crateservice () failed to create a service for other reasons % d! \ N ", dwrtn); Bret = false; goto bexit;} else {// service creation failed because the trace (" crateservice () Service creation failed, it is because the service has already created error is error_io_pending or error_service_exists! \ N ");} // The driver has been loaded. You only need to open hserviceddk = openservice (hservicemgr, lpdrivername, service_all_access); If (hserviceddk = NULL) {// if the service fails to be opened, the error dwrtn = getlasterror (); trace ("openservice () failed % d! \ N ", dwrtn); Bret = false; goto bexit;} else {trace (" openservice () successful! \ N ") ;}} else {trace (" crateservice () successful! \ N ") ;}// enable this service Bret = startservice (hserviceddk, null, null); If (! Bret) // failed to enable service {trace ("startservice () failed service may have enabled % d! \ N ", dwrtn);} BRET = true; // close the handle before leaving bexit: If (hserviceddk) {closeservicehandle (hserviceddk);} If (hservicemgr) {closeservicehandle (hservicemgr);} return Bret ;}//--------------------------------------------------------------------------

The uninstall code is as follows:

// Uninstall the driver bool unloadsys (lpctstr szsvrname) {// variable bool Bret = false; SC _handle hscm = NULL; // the SCM manager handle, used to store the return value of openscmanager SC _handle hservice = NULL; // The Service handle of the NT driver, used to store the return value of openservice service_status svrsta; // two open SCM manager hscm = openscmanager (null, null, SC _manager_all_access); If (hscm = NULL) {// trace ("openscmanager () faild % d! \ N ", getlasterror (); Bret = false; goto beforeleave;} else {// trace (" openscmanager () OK to open the SCM Manager successfully! \ N ") ;}// the service hservice = openservice (hscm, szsvrname, service_all_access) corresponding to the three open drivers; If (hservice = NULL) {// failed to exit trace ("openservice () faild % d! \ N ", getlasterror (); Bret = false; goto beforeleave;} else {trace (" openservice () OK! \ N "); // The service corresponding to the driver is successfully opened} // stop the driver. If the stop fails, the driver can be dynamically loaded only after it is restarted. If (! Controlservice (hservice, service_control_stop, & svrsta) {trace ("failed to stop the driver with controlservice () error code: % d! \ N ", getlasterror ();} else {// stop the driver successfully trace (" successfully stopped the driver with controlservice! \ N ") ;}// 5. dynamically uninstall the driver service. If (! Deleteservice (hservice) // true // false {// failed to uninstall trace ("failed to uninstall: deletesrevice () error code: % d! \ N ", getlasterror ();} else {// trace (" uninstalled successfully! \ N ") ;}bret = true; // close the Open Handle beforeleave: If (hservice> 0) {closeservicehandle (hservice);} If (hscm> 0) {closeservicehandle (hscm);} return Bret ;}

The Code contains detailed comments, which are easy to understand. The code is from the tutorial of Miss tulip. The function to use SCM must contain a header file:

#include <WinSvc.h>

 

Because we are an MFC project, the executable files compiled by default cannot be located in a system without an MFC dynamic library, so I changed the project to a static connection method, to reduce the dependence on the environment.

Modify as follows:

Project-configuration properties-General: Selection of MFC: use MFC in a static library

Project-configure properties-C/C ++-generate code: select the Runtime Library: multi-thread debugging (MTD)

For more information, see the complete source code.

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.