// Loaddrives. cpp: defines the entry point of the console application. <Br/> // <br/> # include "stdafx. H "<br/> # include <windows. h> </P> <p> lpctstr lpregsoftpath = "system // CurrentControlSet // Services"; // Registry Service (driver) path <br/> maid driver path. </P> <p> void delsvr (tchar * szsvrname); // uninstall the driver <br/> int startsvr (tchar * szpath, tchar * szsvrname ); <br/> void checkreg () <br/>{< br/> hkey hsoftkey = NULL; <br/> If (rego Penkeyex (HKEY_LOCAL_MACHINE, lpregpath, <br/> 0, key_read | key_write, & hsoftkey )! = Error_success) <br/>{< br/> printf ("CREATE"); <br/> DWORD dwdisp; <br/> If (regcreatekeyex (HKEY_LOCAL_MACHINE, lpregpath, 0, reg_none, reg_option_non_volatile, <br/> key_write | key_read, null, & hsoftkey, & dwdisp )! = Error_success) <br/>{< br/> printf ("creation failed "); <br/>}< br/> else <br/> {<br/> tchar szdisplayname [] = "helloddk_test"; <br/> DWORD dwerrorcontrol = 1; <br/> tchar szgroup [] = ""; <br/> tchar szimagepath [] = "//?? // E: // helloddk. sys] //? // E: // helloddk. sys "; // driver path <br/> DWORD dwstart = 3; <br/> DWORD dwtype = 1; <br/> regsetvalueex (hsoftkey," displayname ", 0, REG_SZ, (byte *) szdisplayname, (DWORD) sizeof (szdisplayname); <br/> regsetvalueex (hsoftkey, "errorcontrol", 0, REG_DWORD, (const byte *) & dwerrorcontrol, sizeof (DWORD); <br/> regsetvalueex (hsoftkey, "group", 0, REG_SZ, (byte *) szgroup, sizeof (szgroup )); <br/> regsetvalueex (hsoftkey, "imagepa Th ", 0, reg_expand_sz, (byte *) szimagepath, sizeof (szimagepath); <br/> regsetvalueex (hsoftkey," Start ", 0, REG_DWORD, (const byte *) & dwstart, sizeof (DWORD); <br/> regsetvalueex (hsoftkey, "type", 0, REG_DWORD, (const byte *) & dwtype, sizeof (DWORD )); <br/>}< br/> regclosekey (hsoftkey); <br/>}< br/> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> // checkreg (); // checks the registry <br/> // startsvr ("E: // helloddk. sys "," he Loddk "); <br/> delsvr (" helloddk "); <br/> system (" pause "); <br/> return 0; <br/>}< br/> int startsvr (tchar * szpath, tchar * szsvrname) <br/>{< br/> SC _handle hservicemgr, hservicetwdm; <br/> bool brtn; <br/> DWORD dwrtn, dwsize = 256; <br/> tchar szdir [256]; <br/> strcpy (szdir, szpath ); // driver file path <br/> lpctstr lpszbinarypathname = text (szdir); <br/> hservicemgr = openscmanager (null, null, SC _manager_all_access ); // Open the Service Control Manager <br/> If (hservicemgr = NULL) <br/>{< br/> printf ("Open scmanager () faild % d! /N ", getlasterror (); <br/> return 0; <br/>}< br/> else <br/> {<br/> printf ("Open scmanager () OK! /N "); <br/>}< br/> printf (" =================== press the key to create or open the service. /n "); <br/> system (" pause "); <br/> // create a driver service <br/> hservicetwdm = createservice (hservicemgr, <br/> text (szsvrname), // name of the system/CurrentControlSet/services Driver in the registry <br/> text (szsvrname ), // displayname value of the registry driver <br/> service_all_access, // access permission for loading the driver <br/> service_kernel_driver, // indicates that the loaded service is the driver <br/> service_demand_start, // The start value of the registry driver <Br/> service_error_ignore, // errorcontrol value of the registry driver <br/> lpszbinarypathname, // ImagePath value of the registry driver <br/> null, <br/> null, <br/> null, <br/> null, <br/> null); <br/> If (hservicetwdm = NULL) <br/>{< br/> dwrtn = getlasterror (); <br/> If (dwrtn! = Error_io_pending & dwrtn! = Error_service_exists) <br/>{< br/> closeservicehandle (hservicemgr); <br/> printf ("create Service () faild % d! /N ", dwrtn); <br/> system (" pause "); <br/> return 0; <br/>}< br/> else <br/> {<br/> printf ("create Service () faild service is error_io_pending or error_service_exists! /N "); <br/>}< br/> // The driver has been loaded. Open <br/> hservicetwdm = openservice (hservicemgr, text (szsvrname ), service_all_access); <br/> If (hservicetwdm = NULL) <br/>{< br/> dwrtn = getlasterror (); <br/> closeservicehandle (hservicemgr ); <br/> printf ("openservice () faild % d! /N ", dwrtn); <br/> system (" pause "); <br/> return 0; <br/>}< br/> else <br/> {<br/> printf ("open service () OK! /N "); <br/>}< br/> else <br/>{< br/> printf (" create Service () OK! /N "); <br/>}< br/> printf (" =================== press the key to start the driver service. /n "); <br/> system (" pause "); <br/> // start the driver, call the driver's DriverEntry function <br/> brtn = startservice (hservicetwdm, null, null); <br/> If (! Brtn) <br/>{< br/> dwrtn = getlasterror (); <br/> If (dwrtn! = Error_io_pending & dwrtn! = Error_service_already_running) <br/>{< br/> printf ("startservice () faild % d! /N ", dwrtn); <br/> closeservicehandle (hservicetwdm); <br/> closeservicehandle (hservicemgr); <br/> return 0; <br/>}< br/> else <br/>{< br/> If (dwrtn! = Error_io_pending) <br/>{< br/> printf ("Start Service () faild error_io_pending! /N "); <br/>}< br/> else <br/> {<br/> printf (" Start Service () faild error_service_already_running! /N "); <br/>}< br/> else <br/>{< br/> printf ("Start Service () successful! /N "); <br/>}< br/> printf ("======================= press the key to close the service. /n "); <br/> system (" pause "); <br/> service_status servicestatus; <br/> controlservice (hservicetwdm, service_control_stop, & servicestatus ); <br/> closeservicehandle (hservicetwdm); <br/> closeservicehandle (hservicemgr); <br/>}< br/> // uninstall the driver. <Br/> void delsvr (tchar * szsvrname) <br/>{< br/> SC _handle hservicemgr, hservicetwdm; <br/> service_status svrsta; <br/> hservicemgr = openscmanager (null, null, SC _manager_all_access); <br/> If (hservicemgr = NULL) <br/>{< br/> printf ("delsvr:: openscmanager () faild % d! /N ", getlasterror (); <br/> return; <br/>}< br/> else <br/>{< br/> printf (" delsvr :: openscmanager () OK! /N "); <br/>}< br/> hservicetwdm = openservice (hservicemgr, text (szsvrname), service_all_access); <br/> If (hservicetwdm = NULL) <br/>{< br/> closeservicehandle (hservicemgr); <br/> printf ("delsvr: openservice () faild % d! /N ", getlasterror (); <br/> return; <br/>}< br/> else <br/>{< br/> printf (" delsvr :: openservice () OK! /N "); <br/>}< br/> // stop the driver. If the stop fails, it must be restarted before dynamic loading. <Br/> If (! Controlservice (hservicetwdm, service_control_stop, & svrsta) <br/>{< br/> printf ("delsvr: controlservice () faild % d! /N ", getlasterror (); <br/>}< br/> else <br/> {<br/> printf (" delsvr: controlservice () OK! /N "); <br/>}< br/> // uninstall the driver dynamically. <Br/> If (! Deleteservice (hservicetwdm) <br/>{< br/> printf ("delsvr: eletesrevice () faild % d! /N ", getlasterror (); <br/>}< br/> else <br/> {<br/> printf (" delsvr: eletesrevice () OK! /N "); <br/>}< br/> closeservicehandle (hservicetwdm); <br/> closeservicehandle (hservicemgr); <br/> return; <br/>}< br/>