Usages of Deviceiocteatol in C + + _c language

Source: Internet
Author: User
Tags readfile sprintf

This article is a translation, mainly in the form of examples to describe the use of deviceiocteatol in C + +. Share to everyone for your reference. The specific methods are as follows:

The application code is as follows:

Copy Code code as follows:
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,, &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 < 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 < 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;
}

The driver code is as follows:

Copy Code code as follows:
#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
//---------------------------------------------------
The following code writes the primary boot area with the I/O port

MOV dx,1f6h//To read the disk number and magnetic number one
MOV al,0a0h//Disk 0, head 0
Out Dx,al

MOV dx,1f2h//number of sectors to write
MOV al,1//write one sector
Out Dx,al

MOV dx,1f3h//The sector area code to be written
MOV al,1//write to 1 sectors
Out Dx,al

MOV dx,1f4h//low 8 bits of cylinder to be written
MOV al,0//low 8 digits 0
Out Dx,al

MOV dx,1f5h//high 2 bits of cylinder to be written
MOV al,0//high 2 digits 0
Out Dx,al

MOV dx,1f7h//Command port
mov al,30h//Try to write a sector
Out Dx,al

Still_going_1:
In AL,DX
Test al,8//If the sector buffer is not ready then jump until it is ready to execute
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
//---------------------------------------------------
The following code reads the primary boot area with the I/O port

MOV dx,1f6h//To read the disk number and magnetic number one
MOV al,0a0h//Disk 0, head 0
Out Dx,al

MOV dx,1f2h//number of sectors to read
MOV al,1//Read a sector
Out Dx,al

MOV dx,1f3h//To read the sector area code
MOV al,1//Sector area code is 1
Out Dx,al

MOV dx,1f4h//low 8 bits of cylinder to be read
MOV al,0//cylinder low 8 digits 0
Out Dx,al

MOV dx,1f5h//cylinder height 2 bits
MOV al,0//cylinder height 2 bits 0 (through 1f4h and 1f5h ports we can determine that the cylinder number to read is 0)
Out Dx,al

MOV dx,1f7h//Command port
mov al,20h//try to read sector
Out Dx,al

Still_going_2:
In AL,DX//sector buffers are ready
Test al,8//jumps If the sector buffer is not ready until it is ready to execute.
JZ still_going_2

/* mov CX,512/2//set cycle times (512/2 times)
MOV Di,offset buffer
mov dx,1f0h//bytes of data to be transmitted
Rep INSW//Transmit data * *

//---------------------------------------------------
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;
}

I hope this article will help you with the C + + program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.