IOCTL Driver SystemBuffer always NULL

2019-08-21 09:00发布

问题:

I have a simple struct which I would like to pass to my driver. Here is the struct:

typedef struct readStruct
{
  ...
} ReadStruct, *pRreadStruct;

Here is my usermode application:

DWORD dwReturn;
readStruct reader{ ... };

WriteFile(hDriver, (LPCVOID)&reader, sizeof(ReadStruct), &dwReturn, NULL);

Here is my driver code, it always returns NULL to the readStruct. What am I doing wrong?

PIO_STACK_LOCATION pIoStackIrp = NULL;
pRreadStruct readStruct;

pIoStackIrp = IoGetCurrentIrpStackLocation(Irp);

DbgPrintEx(0, 0, "WriteBufferedIO\n");

if (pIoStackIrp)
{
    readStruct = (pRreadStruct)Irp->AssociatedIrp.SystemBuffer;
    if (readStruct)
    {
        // this is the place I never get into
        if (readStruct->ReadSize)
        {
            ReadMemOutputClient(readStruct);
        }
    }
}

回答1:

DO_BUFFERED_IO flag should be set in DriverEntry in DeviceObject->Flags.

Thanks to user @RbMm for pointing this out.