Before we loaded the driver is the use of instdrv this application, the principle is to write the corresponding fields in the registry, this section we manually write code to load the driver, the principle is similar:
The dynamic loading of a device driver is primarily done by the Service Control Manager (MANAGER,SCM) system component . Loading and unloading NT drivers is divided into four steps:
Create a new service for NT drivers;
Open this service;
Close the service;
Delete the service created by the NT driver.
The main flow of Loadntdriver load driver code is as follows:
The code is as follows:
1BOOL Loadntdriver (Char*lpszdrivername,Char*Lpszdriverpath) {2 Charszdriverimagepath[ the];3 // get the full drive path 4Getfullpathname (Lpszdriverpath, the, Szdriverimagepath, NULL);5BOOL BRet =FALSE;6Sc_handle hservicemgr =NULL;7Sc_handle HSERVICEDDK =NULL;8 // Open the Service Control Manager 9Hservicemgr =OpenSCManager (null, NULL, sc_manager_all_access);Ten if(NULL = =hservicemgr) { Oneprintf"OpenSCManager () failed:%d \ n", GetLastError ()); A GotoBeforeleave; -BRet =FALSE; - } the Else{ -printf"OpenSCManager () ok!\n"); - } - // Create a service corresponding to the driver +HSERVICEDDK =CreateService (Hservicemgr, - Lpszdrivername, + Lpszdrivername, A service_all_access, at Service_kernel_driver, - Service_demand_start, - Service_error_ignore, - Szdriverimagepath, - NULL, NULL, NULL, and null ); - in DWORD Dwrtn; - if(NULL = =HSERVICEDDK) { toDwrtn =GetLastError (); + if(Dwrtn! = error_io_pending && Dwrtn! =error_service_exists) { -printf"CreateService failed:%d!\n", DWRTN); theBRet =FALSE; * GotoBeforeleave; $ }Panax Notoginseng Else{ -printf"createservice failed:error_io_pending or error_service_exists\n"); the } + // Create failure description service already exists, open directly AHSERVICEDDK =OpenService (Hservicemgr, Lpszdrivername, service_all_access); the if(NULL = =HSERVICEDDK) { +Dwrtn =GetLastError (); -printf"OpenService () failed:%d!\n", DWRTN); $BRet =FALSE; $ GotoBeforeleave; - } - Else{ theprintf"OpenService ok!\n"); - }Wuyi } the Else{ -printf"CreateService ok!\n"); Wu } - About // Create service successfully, open this service $BRet =StartService (HSERVICEDDK, NULL, NULL); - if(!BRet) { -Dwrtn =GetLastError (); - if(Dwrtn! = error_io_pending && Dwrtn! =error_service_already_running) { Aprintf"StartService failed:%d!\n", DWRTN); +BRet =FALSE; the GotoBeforeleave; - } $ Else{ the if(Error_io_pending = =Dwrtn) { theprintf"StartService () failed:error_io_pending"); theBRet =FALSE; the GotoBeforeleave; - } in Else{ theprintf"StartService () failed:error_service_already_running"); theBRet =TRUE; About GotoBeforeleave; the } the } the } +BRet =TRUE; - Beforeleave: the if(HSERVICEDDK) {Bayi Closeservicehandle (HSERVICEDDK); the } the if(hservicemgr) { - Closeservicehandle (hservicemgr); - } the returnBRet; the}
The main code flow for the Unloadntdriver offload driver is as follows:
The code is as follows:
1BOOL Unloadntdriver (Char*szsvrname) {2BOOL BRet =FALSE;3Sc_handle hservicemgr =NULL;4Sc_handle HSERVICEDDK =NULL;5Hservicemgr =OpenSCManager (null, NULL, sc_manager_all_access);6 if(NULL = =hservicemgr) {7printf"OpenSCManager () failed:%d \ n", GetLastError ());8 GotoBeforeleave;9BRet =FALSE;Ten } One Else{ Aprintf"OpenSCManager () ok!\n"); - } -HSERVICEDDK =OpenService (Hservicemgr, Szsvrname, service_all_access); the if(NULL = =HSERVICEDDK) { -printf"OpenService () failed:%d\n", GetLastError ()); -BRet =FALSE; - GotoBeforeleave; + } - Else{ +printf"OpenService () ok!\n"); A } at service_status Svrsta; - // stop driver - if(! ControlService (HSERVICEDDK, Service_control_stop, &Svrsta)) { -printf"ControlService () failed:%d\n", GetLastError ()); - } - Else{ inprintf"ControlService () ok!\n"); - } to // dynamic unload driver + if(!DeleteService (HSERVICEDDK)) { -printf"DeleteService () failed:%d\n", GetLastError ()); the } * Else{ $printf"DeleteService () ok!\n");Panax Notoginseng } -BRet =TRUE; the + Beforeleave: A if(HSERVICEDDK) { the Closeservicehandle (HSERVICEDDK); + } - if(hservicemgr) { $ Closeservicehandle (hservicemgr); $ } - returnBRet; -}
Install and Uninstall drivers:
Note that the driver name and path here should be set to:
Run successfully:
Programming of Windows Driver development technology-loading NT driver