
展开全部
#include <windows.h>
#include <winsvc.h>
#include <conio.h>
#include <stdio.h>
#define DRIVER_NAME "123467"
#define DRIVER_PATH "..\\HelloDDK.sys"
//装载NT驱动程 />BOOL LoadNTDriver(char* lpszDriverName,char* lpszDriverPath)
{
/************************ 加载NT驱动的代码*******************************
① 祊enSCManager,碈M管理乒祷豊ULL,载失败,诽续
② 祌eateService,创建服谓ǔ晒υ街 ⑥
③ 用GetLastError的得到错呜值
④ 返回值为ERROR_IO_PENDING,说铆创建过,用OpenService此服蝦 /> ⑤ 返回值为其它值, 创建武服诬,返回失败.
⑥ 祎artService开 /> ⑦ 成功返回
************************************************************************/
char szDriverImagePath[256];
//得到完整的驱动模式
GetFullPathName(lpszDriverPath, 256, szDriverImagePath, NULL);
BOOL bRet = FALSE;
SC_HANDLE hServiceMgr=NULL;// SCM管理其柄
SC_HANDLE hServiceDDK=NULL;// NT驱动程玄晰
//逮纹管理 />hServiceMgr = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
if( hServiceMgr == NULL )
{
// OpenSCManager失败
printf( "OpenSCManager() Faild %d ! \n", GetLastError() );
bRet = FALSE;
goto BeforeLeave;
}
else
{
// OpenSCManager成功
printf( "OpenSCManager() ok ! \n" );
}
//创建驱动所对应的服 />hServiceDDK = CreateService( hServiceMgr,
lpszDriverName,// 驱动程汹注册表中的名字
lpszDriverName,// 注册表驱动程蠨isplayName 值
SERVICE_ALL_ACCESS,// 加载驱动程忻问权限
SERVICE_KERNEL_DRIVER, // 表示加载的服锡动程衦 /> SERVICE_DEMAND_START, // 注册表驱动程蠸tart 值
SERVICE_ERROR_IGNORE, // 注册表驱动程蠩rrorControl 值
szDriverImagePath,// 注册表驱动程蠭magePath 值
NULL,
NULL,
NULL,
NULL,
NULL);

DWORD dwRtn;
// 判断服诬
if( hServiceDDK == NULL )
{
dwRtn = GetLastError();
if( dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_EXISTS )
{
//由于其他原舀服诬
printf( "CrateService() Faild %d ! \n", dwRtn );
bRet = FALSE;
goto BeforeLeave;
}
else
{
//服唯失败ifly文件用什么打开ifly文件用什么打开,是因为服苇创立过
printf( "CrateService() Faild Service is ERROR_IO_PENDING or ERROR_SERVICE_EXISTS! \n" );
}
// 驱动程协加载,只应该滁br /> hServiceDDK = OpenService( hServiceMgr, lpszDriverName, SERVICE_ALL_ACCESS );
if( hServiceDDK == NULL )
{
// 如果逮围败,远错 /> dwRtn = GetLastError();
printf( "OpenService() Faild %d ! \n", dwRtn );
bRet = FALSE;
goto BeforeLeave;
}
else
{
printf( "OpenService() ok ! \n" );
}
}
else
{
printf( "CrateService() ok ! \n" );
}
// 开祁服 />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 )

{
// 设备被挂住
printf( "StartService() Faild ERROR_IO_PENDING ! \n");
bRet = FALSE;
goto BeforeLeave;
}
else
{
// 服苇开 />printf( "StartService() Faild ERROR_SERVICE_ALREADY_RUNNING ! \n");
bRet = TRUE;
goto BeforeLeave;
}
}
}
bRet = TRUE;
// 离开前关闭句柄
BeforeLeave:
if(hServiceDDK)
{
CloseServiceHandle(hServiceDDK); // 服晰
}
if(hServiceMgr)
{
CloseServiceHandle(hServiceMgr); // SCM句柄
}
return bRet;
}
// 卸载驱动程衦 />BOOL UnloadNTDriver( char * szSvrName )
{
/************************* 卸载NT驱动的代码******************************
① 祊enSCManager,碈M管理乒祷豊ULL,载失败,诽续.
② 祊enService.如果返回NULL,载失败,诽续
③ 礶leteService卸载此项服蝦 /> ④ 成功返回.
************************************************************************/
BOOL bRet = FALSE;
SC_HANDLE hServiceMgr=NULL;// SCM管理其柄
SC_HANDLE hServiceDDK=NULL;// NT驱动程玄晰
SERVICE_STATUS SvrSta;
// 碈M管理 />hServiceMgr = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
if( hServiceMgr == NULL )
{
// 碈M管理栖
printf( "OpenSCManager() Faild %d ! \n", GetLastError() );
bRet = FALSE;
goto BeforeLeave;
}
else

{
// 碈M管理栖成功
printf( "OpenSCManager() ok ! \n" );
}
// 待动所对应的服 />hServiceDDK = OpenService( hServiceMgr, szSvrName, SERVICE_ALL_ACCESS );
if( hServiceDDK == NULL )
{
// 待动所对应的服诬
printf( "OpenService() Faild %d ! \n", GetLastError() );
bRet = FALSE;
goto BeforeLeave;
}
else
{
printf( "OpenService() ok ! \n" );
}
// 停止驱动程戌果停止失败,只有再次婆能,再动态加载。
if( !ControlService( hServiceDDK, SERVICE_CONTROL_STOP , &SvrSta ) )
{
printf( "ControlService() Faild %d !\n", GetLastError() );
}
else
{
// 待动所对应的失败
printf( "ControlService() ok !\n" );
}
// 动态卸载驱动程宵br />if( !DeleteService( hServiceDDK ) )
{
// 卸载失败
printf( "DeleteSrevice() Faild %d !\n", GetLastError() );
}
else
{
// 卸载成功
printf( "DelServer:eleteSrevice() ok !\n" );
}
bRet = TRUE;
BeforeLeave:
// 离开前关闭茨句柄
if(hServiceDDK)
{
CloseServiceHandle(hServiceDDK); // 服晰
}
if(hServiceMgr)
{
CloseServiceHandle(hServiceMgr); // SCM 句柄
}

return bRet;
}
void TestDriver()
{
// 测试驱动程衦 />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);
// 加载驱动
BOOL bRet = LoadNTDriver(DRIVER_NAME,DRIVER_PATH);
if (!bRet)
{
printf("LoadNTDriver error\n");
return 0;
}
// 加载成功
printf( "press any to create device!\n" );
getch();
TestDriver();
// 这时荷以借助注册表,畸查看符号连接的工具验证。
printf( "press any to unload the driver!\n" );
getch();
// 卸载驱动
UnloadNTDriver(DRIVER_NAME);
// if (!bRet)
// {
// printf("UnloadNTDriver error\n");
// return 0;
// }
system("pause");
return 0;
}
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/bofangqi/article-136457-1.html
我当击沉之
是怨那些害人的传统观念
开打