'What is the correct way to create and dispatch an IRP_MJ_WRITE request for writing to a file?

I am creating a kernel mode driver, and i now i need to write to a file and i am in a context where i can't use ZwWriteFile/NtWriteFile. I must directly send the IRP. I tried to use IoBuildAsynchronousFsdRequest but i think im not using it correctly because: the machine makes a bsod once the IRP is sent, and the machine fails to boot again, i guess because these bytes are written on the disk rather on the wanted file. Should i use IoAllocateIrp? Should i use IoBuildAsynchronousFsdRequest? How to specify the FILE_OBJECT to write to?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source