0

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);
        }
    }
}
melpomene
  • 84,125
  • 8
  • 85
  • 148
Jussi Hietanen
  • 181
  • 1
  • 1
  • 12
  • 1
    are you device used buffered io ? you set `DO_BUFFERED_IO` flag ? only in this case `AssociatedIrp.SystemBuffer` will be point to user buffer. also `IRP_MJ_WRITE` this is not `IRP_MJ_DEVICE_CONTROL` - so your header (in question) is not match real question – RbMm Aug 20 '17 at 11:19
  • for `IRP_MJ_WRITE` - **Irp->AssociatedIrp.SystemBuffer** Pointer to a system-supplied buffer to be used as an intermediate system buffer, if the **DO_BUFFERED_IO** flag is set in **DeviceObject->Flags**. Otherwise, this member is set to NULL. – RbMm Aug 20 '17 at 11:21
  • Why are you typedef'ing structs in C++? Now you have three names (`struct readStruct`, `readStruct`, `ReadStruct`) all referring to the same type. Also, I strongly recommend against hiding pointers behind typedefs (and pR**r**eadStruct is misspelled). – melpomene Aug 20 '17 at 11:23
  • @RbMm thank you. Frankly, I had it set to DO_DIRECT_IO and didn't even look into the DriverEntry routine as I thought the problem was in my sample code. If you want to, please make an answer and I can accept it as the solution. – Jussi Hietanen Aug 20 '17 at 11:40
  • I am having a very similar problem to this, I am oring the `DO_BUFFERED_IO` macro with `PDRIVER_OBJECT->Flags` and I am passing a string with the write method. The `SystemBuffer` is always `NULL`. `writeDataBuffer = (PCHAR)Irp->AssociatedIrp.SystemBuffer;` – gudenau Dec 04 '17 at 00:03

1 Answers1

0

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

Thanks to user @RbMm for pointing this out.

Jussi Hietanen
  • 181
  • 1
  • 1
  • 12