?? main.cpp
字號(hào):
#include <windows.h>
#include <winsvc.h>
#include <conio.h>
#include <stdio.h>
#define DRIVER_NAME "HelloDDK"
#define DRIVER_PATH "..\\MyDriver\\MyDriver_Check\\HelloDDK.sys"
//裝載NT驅(qū)動(dòng)程序
BOOL LoadNTDriver(char* lpszDriverName,char* lpszDriverPath)
{
char szDriverImagePath[256];
//得到完整的驅(qū)動(dòng)路徑
GetFullPathName(lpszDriverPath, 256, szDriverImagePath, NULL);
BOOL bRet = FALSE;
SC_HANDLE hServiceMgr=NULL;//SCM管理器的句柄
SC_HANDLE hServiceDDK=NULL;//NT驅(qū)動(dòng)程序的服務(wù)句柄
//打開服務(wù)控制管理器
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" );
}
//創(chuàng)建驅(qū)動(dòng)所對(duì)應(yīng)的服務(wù)
hServiceDDK = CreateService( hServiceMgr,
lpszDriverName, //驅(qū)動(dòng)程序的在注冊(cè)表中的名字
lpszDriverName, // 注冊(cè)表驅(qū)動(dòng)程序的 DisplayName 值
SERVICE_ALL_ACCESS, // 加載驅(qū)動(dòng)程序的訪問權(quán)限
SERVICE_KERNEL_DRIVER,// 表示加載的服務(wù)是驅(qū)動(dòng)程序
SERVICE_DEMAND_START, // 注冊(cè)表驅(qū)動(dòng)程序的 Start 值
SERVICE_ERROR_IGNORE, // 注冊(cè)表驅(qū)動(dòng)程序的 ErrorControl 值
szDriverImagePath, // 注冊(cè)表驅(qū)動(dòng)程序的 ImagePath 值
NULL,
NULL,
NULL,
NULL,
NULL);
DWORD dwRtn;
//判斷服務(wù)是否失敗
if( hServiceDDK == NULL )
{
dwRtn = GetLastError();
if( dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_EXISTS )
{
//由于其他原因創(chuàng)建服務(wù)失敗
printf( "CrateService() Faild %d ! \n", dwRtn );
bRet = FALSE;
goto BeforeLeave;
}
else
{
//服務(wù)創(chuàng)建失敗,是由于服務(wù)已經(jīng)創(chuàng)立過
printf( "CrateService() Faild Service is ERROR_IO_PENDING or ERROR_SERVICE_EXISTS! \n" );
}
// 驅(qū)動(dòng)程序已經(jīng)加載,只需要打開
hServiceDDK = OpenService( hServiceMgr, lpszDriverName, SERVICE_ALL_ACCESS );
if( hServiceDDK == NULL )
{
//如果打開服務(wù)也失敗,則意味錯(cuò)誤
dwRtn = GetLastError();
printf( "OpenService() Faild %d ! \n", dwRtn );
bRet = FALSE;
goto BeforeLeave;
}
else
{
printf( "OpenService() ok ! \n" );
}
}
else
{
printf( "CrateService() ok ! \n" );
}
//開啟此項(xiàng)服務(wù)
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 )
{
//設(shè)備被掛住
printf( "StartService() Faild ERROR_IO_PENDING ! \n");
bRet = FALSE;
goto BeforeLeave;
}
else
{
//服務(wù)已經(jīng)開啟
printf( "StartService() Faild ERROR_SERVICE_ALREADY_RUNNING ! \n");
bRet = TRUE;
goto BeforeLeave;
}
}
}
bRet = TRUE;
//離開前關(guān)閉句柄
BeforeLeave:
if(hServiceDDK)
{
CloseServiceHandle(hServiceDDK);
}
if(hServiceMgr)
{
CloseServiceHandle(hServiceMgr);
}
return bRet;
}
//卸載驅(qū)動(dòng)程序
BOOL UnloadNTDriver( char * szSvrName )
{
BOOL bRet = FALSE;
SC_HANDLE hServiceMgr=NULL;//SCM管理器的句柄
SC_HANDLE hServiceDDK=NULL;//NT驅(qū)動(dòng)程序的服務(wù)句柄
SERVICE_STATUS SvrSta;
//打開SCM管理器
hServiceMgr = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
if( hServiceMgr == NULL )
{
//帶開SCM管理器失敗
printf( "OpenSCManager() Faild %d ! \n", GetLastError() );
bRet = FALSE;
goto BeforeLeave;
}
else
{
//帶開SCM管理器失敗成功
printf( "OpenSCManager() ok ! \n" );
}
//打開驅(qū)動(dòng)所對(duì)應(yīng)的服務(wù)
hServiceDDK = OpenService( hServiceMgr, szSvrName, SERVICE_ALL_ACCESS );
if( hServiceDDK == NULL )
{
//打開驅(qū)動(dòng)所對(duì)應(yīng)的服務(wù)失敗
printf( "OpenService() Faild %d ! \n", GetLastError() );
bRet = FALSE;
goto BeforeLeave;
}
else
{
printf( "OpenService() ok ! \n" );
}
//停止驅(qū)動(dòng)程序,如果停止失敗,只有重新啟動(dòng)才能,再動(dòng)態(tài)加載。
if( !ControlService( hServiceDDK, SERVICE_CONTROL_STOP , &SvrSta ) )
{
printf( "ControlService() Faild %d !\n", GetLastError() );
}
else
{
//打開驅(qū)動(dòng)所對(duì)應(yīng)的失敗
printf( "ControlService() ok !\n" );
}
//動(dòng)態(tài)卸載驅(qū)動(dòng)程序。
if( !DeleteService( hServiceDDK ) )
{
//卸載失敗
printf( "DeleteSrevice() Faild %d !\n", GetLastError() );
}
else
{
//卸載成功
printf( "DelServer:eleteSrevice() ok !\n" );
}
bRet = TRUE;
BeforeLeave:
//離開前關(guān)閉打開的句柄
if(hServiceDDK)
{
CloseServiceHandle(hServiceDDK);
}
if(hServiceMgr)
{
CloseServiceHandle(hServiceMgr);
}
return bRet;
}
void TestDriver()
{
//測(cè)試驅(qū)動(dòng)程序
HANDLE hDevice = CreateFile("\\\\.\\HelloDDK",
GENERIC_WRITE | GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if( hDevice != INVALID_HANDLE_VALUE )
{
printf( "Create Device ok ! \n" );
}
else
{
printf( "Create Device faild %d ! \n", GetLastError() );
}
CloseHandle( hDevice );
}
int main(int argc, char* argv[])
{
//加載驅(qū)動(dòng)
BOOL bRet = LoadNTDriver(DRIVER_NAME,DRIVER_PATH);
if (!bRet)
{
printf("LoadNTDriver error\n");
return 0;
}
//加載成功
printf( "press any to create device!\n" );
getch();
TestDriver();
//這時(shí)候你可以通過注冊(cè)表,或其他查看符號(hào)連接的軟件驗(yàn)證。
printf( "press any to unload the driver!\n" );
getch();
//卸載驅(qū)動(dòng)
UnloadNTDriver(DRIVER_NAME);
if (!bRet)
{
printf("UnloadNTDriver error\n");
return 0;
}
return 0;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -