#include <windows.h>
#include <winsvc.h>
#include <conio.h>
#include <stdio.h>
#define DRIVER_NAME "123467"
#define Driver_path ". \\HelloDDK.sys "
Loading NT Drivers
BOOL loadntdriver (char* lpszdrivername,char* Lpszdriverpath)
{
/************************ loading NT-driven code *******************************
① calls OpenSCManager to open SCM Manager. If NULL is returned, the return fails, otherwise continue
② call CreateService, create a service, create a success then go to step ⑥
③ using GetLastError to get the wrong return value
The ④ return value is Error_io_pending, which indicates that the service has been created and opened with OpenService.
⑤ return value to another value, create a service failed, return failed.
⑥ call StartService to open service
⑦ successful return
************************************************************************/
Char szdriverimagepath[256];
Get the full drive path
Getfullpathname (Lpszdriverpath, N, Szdriverimagepath, NULL);
BOOL bRet = FALSE;
Sc_handle hservicemgr=null;//SCM Manager handle
Sc_handle Service handle for hserviceddk=null;//NT driver
Open the Service Control Manager
Hservicemgr = OpenSCManager (null, NULL, sc_manager_all_access);
if (hservicemgr = = NULL)
{
OpenSCManager failure
printf ("OpenSCManager () Faild%d!" \ n ", GetLastError ());
BRet = FALSE;
Goto Beforeleave;
}
Else
{
OpenSCManager success
printf ("OpenSCManager () ok!" \ n ");
}
//Create the service corresponding to the driver
Hserviceddk = CreateService (hservicemgr,
lpszdrivername, //Driver's name in the registry
lpszdrivername, //Registry driver DisplayName value
service_all_access, //Load driver access rights
service_kernel_driver,//indicates that the loaded service is a driver
Service_ demand_start, //Registry driver START value
service_error_ignore, //Registry driver ErrorControl value
szdriverimagepath, //Registry driver ImagePath value
null,
null,
null,
null,
NULL);
DWORD Dwrtn;
//Determine if the service failed
if (hserviceddk = = NULL)
{
Dwrtn = GetLastError ();
if (dwrtn! = error_io_pending && Dwrtn! = error_service_exists)
{
& nbsp Failed to create service for other reasons
printf ("Crateservice () Faild%d!" \ n ", dwrtn);
bRet = FALSE;
goto beforeleave;
} ;
else
{
//Service creation failed because the service has been created
printf ("Crateservice () faild Service is error_io_pending or error_service_exists! \ n ");
}
The driver is loaded, just open the
HSERVICEDDK = OpenService (Hservicemgr, Lpszdrivername, service_all_access);
if (HSERVICEDDK = = NULL)
{
If opening the service also fails, it means an error
Dwrtn = GetLastError ();
printf ("OpenService () Faild%d!" \ n ", DWRTN);
BRet = FALSE;
Goto Beforeleave;
}
Else
{
printf ("OpenService () ok!" \ n ");
}
}
Else
{
printf ("Crateservice () ok!" \ n ");
}
Open this service
Bret= StartService (HSERVICEDDK, NULL, NULL);
if (!bret)
{
DWORD Dwrtn = GetLastError ();
if (dwrtn! = error_io_pending && Dwrtn! = error_service_already_running)
{
printf ("StartService () Faild%d!" \ n ", DWRTN);
BRet = FALSE;
Goto Beforeleave;
}
Else
{
if (Dwrtn = = error_io_pending)
{
Device is suspended
printf ("StartService () faild error_io_pending!" \ n ");
BRet = FALSE;
Goto Beforeleave;
}
Else
{
Service is open
printf ("StartService () faild error_service_already_running!" \ n ");
BRet = TRUE;
Goto Beforeleave;
}
}
}
BRet = TRUE;
Close handle before leaving
Beforeleave:
if (HSERVICEDDK)
{
Closeservicehandle (HSERVICEDDK); Service Handle
}
if (hservicemgr)
{
Closeservicehandle (hservicemgr); SCM Handle
}
return bRet;
}
Uninstalling drivers
BOOL unloadntdriver (char * szsvrname)
{
/************************* Uninstalling NT-driven code ******************************
① calls OpenSCManager, opens the SCM Manager, returns a failure if NULL is returned, or continues.
② calls OpenService. Returns a failure if NULL is returned, otherwise continue
③ calls DeleteService to uninstall the service.
④ successfully returned.
************************************************************************/
BOOL bRet = FALSE;
Sc_handle hservicemgr=null;//SCM Manager handle
Sc_handle Service handle for hserviceddk=null;//NT driver
Service_status Svrsta;
Open SCM Manager
Hservicemgr = OpenSCManager (null, NULL, sc_manager_all_access);
if (hservicemgr = = NULL)
{
Failed to open SCM Manager
printf ("OpenSCManager () Faild%d!" \ n ", GetLastError ());
BRet = FALSE;
Goto Beforeleave;
}
Else
{
Failed to open SCM Manager successfully
printf ("OpenSCManager () ok!" \ n ");
}
Open the service for the driver
HSERVICEDDK = OpenService (Hservicemgr, Szsvrname, service_all_access);
if (HSERVICEDDK = = NULL)
{
//Open driver for service failure
printf ("openservice () Faild% D! \ n ", GetLastError ());
bRet = FALSE;
goto beforeleave;
}
else
{
printf ("OpenService () ok! \ n ");
}
//Stop the driver, if the stop fails, only a reboot is required, and then dynamically load.
if (! ControlService (HSERVICEDDK, Service_control_stop, &svrsta))
{
printf (" ControlService () Faild%d!\n ", GetLastError ());
}
else
{
//Failed to open driver printf ("ControlService () ok!\n");
}
//dynamic unload driver.
if (! DeleteService (HSERVICEDDK))
{
//Uninstall failed
printf ("Deletesrevice () Faild%d!\n ", GetLastError ());
}
else
{
//Uninstall succeeded
printf (" DelS Erver:eletesrevice () ok!\n ");
}
BRet = TRUE;
Beforeleave:
//Close open handle before leaving
if (HSERVICEDDK)
{
closeservicehandle (HSERVICEDDK);//Service handle
if (hservicemgr)
{
closeservicehandle (hservicemgr);//SCM handle
}
return bret;
}
void Testdriver ()
{
Test driver
HANDLE hdevice = CreateFile ("\\\\.\\HELLODDK",
Generic_write | Generic_read,
0,
Null
Open_existing,
0,
NULL);
if (hdevice! = INVALID_HANDLE_VALUE)
{
MessageBox (NULL, "Sucessfully....comeon ...", "Yes", 0);
printf ("Create Device OK! \ n ");
}
Else
{
printf ("Create Device faild%d! \ n ", GetLastError ());
MessageBox (NULL, "Faild ... Fuckking ... "," No ", 0);
}
CloseHandle (Hdevice);
}
int main (int argc, char* argv[])
{
Unloadntdriver (driver_name);
Load Driver
BOOL BRet = Loadntdriver (Driver_name,driver_path);
if (!bret)
{
printf ("Loadntdriver error\n");
return 0;
}
Load succeeded
printf ("Press any to create device!\n");
Getch ();
Testdriver ();
At this point you can verify the software through the registry, or another view of the symbolic connection.
printf ("Press any to unload the driver!\n");
Getch ();
Uninstalling the Drive
Unloadntdriver (driver_name);
if (!bret)
// {
printf ("Unloadntdriver error\n");
return 0;
// }
System ("pause");
return 0;
}