No problem, sh*t happens
If you're interested in the details between the different METHOD_ codes, dig up these 2 OSR articles:
Sharing Memory Between Drivers and Applications
Defining Custom Device I/O Control Codes
For most uses METHOD_BUFFERED is all you need, either the input or output buffers can be declared as NULL in the DeviceIoControl call if you don't need them. METHOD_NEITHER can be used if you use, well, neither.
You should however *always* validate your buffer sizes first in your IOCTL call. A convenient way to make things portable is to declare shared I/O structures in a common header file for both the user app and driver.
For example:
Code:
// Define incoming and outgoing buffers for METHOD_BUFFERED use
// struct format of data passed to driver
typedef struct _IO_IN_BUFFER {
ULONG dummy;
UCHAR string[0x32];
} IO_IN_BUFFER, *PIO_IN_BUFFER;
// struct format of data returned from driver
typedef struct _IO_OUT_BUFFER {
ULONG someotherdummy;
UCHAR someotherstring[0x64];
} IO_OUT_BUFFER, *PIO_OUT_BUFFER;
These buffers become the relevant lpInBuffer and lpOutBuffer in the DeviceIoControl call. Then in the driver you validate their lengths. You can then access each buffer independantly.
Code:
// ULONG inputBufferLength = IrpStack->Parameters.DeviceIoControl.InputBufferLength;
// ULONG outputBufferLength = IrpStack->Parameters.DeviceIoControl.OutputBufferLength;
// IO_IN_BUFFER pInputBuffer;
// IO_OUT_BUFFER pOutputBuffer;
case IOCTL_Call_1:
//////////////////////////////////////////////////////////////////
// Validate buffer sizes
//////////////////////////////////////////////////////////////////
if ( inputBufferLength < sizeof(IO_IN_BUFFER) ||
outputBufferLength < sizeof(IO_OUT_BUFFER)
)
{
IoStatus->Status = STATUS_INVALID_BUFFER_SIZE;
IoStatus->Information = 0;
return FALSE;
}
// Cast Irp->AssociatedIrp.SystemBuffer to our input structure
pInputBuffer = *(PIO_IN_BUFFER) Irp->AssociatedIrp.SystemBuffer;
// Cast Irp->AssociatedIrp.SystemBuffer to our output structure
pOutputBuffer = *(PIO_OUT_BUFFER) Irp->AssociatedIrp.SystemBuffer;
...
Kayaker