Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. question related to drivers

question related to drivers

Scheduled Pinned Locked Moved C / C++ / MFC
question
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gamitech
    wrote on last edited by
    #1

    I have device driver and I want to write in a file from within the driver. I am using ZwCreateFile() ZwWriteFile() ZwClose() functions. When I call the InitializeObjectAttributesfunction() i do not know how should the object name look like. I mean if my file name is: D:\\status.txt or something like this: \Device\HarddiskVolume1\status.txt I want to ask now if these are the functions I should use when writting in a file from the kernel: Here is a piece of code. Can someone tell me what I do wrong? NTSTATUS WriteStatusInFile( IN PCWSTR FileName, IN HANDLE hProcessId ) { UNICODE_STRING ObjN; OBJECT_ATTRIBUTES ObjAttrib; UNICODE_STRING ObjName; IN HANDLE hFile; IN IO_STATUS_BLOCK StatBlock,WStatBlock; RtlInitUnicodeString(&ObjN,L"\\D:\\status.txt"); InitializeObjectAttributes(&ObjAttrib,&ObjN, OBJ_KERNEL_HANDLE , NULL, NULL); ZwCreateFile(&hFile, FILE_WRITE_DATA|FILE_APPEND_DATA, &ObjAttrib, &StatBlock, 0, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_WRITE, FILE_CREATE |FILE_OPEN, FILE_SEQUENTIAL_ONLY, NULL, 0 ); ZwWriteFile( hFile, NULL, NULL, NULL, &WStatBlock, L"Process Created or Terminated\n", sizeof("Process Created or Terminated\n"), NULL, NULL ); ZwClose(hFile); return WStatBlock.Status; } Thanks in advance. gabby

    L 1 Reply Last reply
    0
    • G gamitech

      I have device driver and I want to write in a file from within the driver. I am using ZwCreateFile() ZwWriteFile() ZwClose() functions. When I call the InitializeObjectAttributesfunction() i do not know how should the object name look like. I mean if my file name is: D:\\status.txt or something like this: \Device\HarddiskVolume1\status.txt I want to ask now if these are the functions I should use when writting in a file from the kernel: Here is a piece of code. Can someone tell me what I do wrong? NTSTATUS WriteStatusInFile( IN PCWSTR FileName, IN HANDLE hProcessId ) { UNICODE_STRING ObjN; OBJECT_ATTRIBUTES ObjAttrib; UNICODE_STRING ObjName; IN HANDLE hFile; IN IO_STATUS_BLOCK StatBlock,WStatBlock; RtlInitUnicodeString(&ObjN,L"\\D:\\status.txt"); InitializeObjectAttributes(&ObjAttrib,&ObjN, OBJ_KERNEL_HANDLE , NULL, NULL); ZwCreateFile(&hFile, FILE_WRITE_DATA|FILE_APPEND_DATA, &ObjAttrib, &StatBlock, 0, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_WRITE, FILE_CREATE |FILE_OPEN, FILE_SEQUENTIAL_ONLY, NULL, 0 ); ZwWriteFile( hFile, NULL, NULL, NULL, &WStatBlock, L"Process Created or Terminated\n", sizeof("Process Created or Terminated\n"), NULL, NULL ); ZwClose(hFile); return WStatBlock.Status; } Thanks in advance. gabby

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      This is how its done. Its a code chunk out of an ndis driver but its the same as what you wil want: OBJECT_ATTRIBUTES ObjectAttributes; IO_STATUS_BLOCK IoStatusBlock; UNICODE_STRING ObjectName; RtlInitUnicodeString(&ObjectName, L"\\DosDevices\\C:\\R_NDIS.txt"); InitializeObjectAttributes(&ObjectAttributes, &ObjectName, OBJ_CASE_INSENSITIVE, NULL, NULL); Status = ZwCreateFile( &hfile, GENERIC_WRITE, &ObjectAttributes, &IoStatusBlock, NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF, FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_ALERT, NULL, 0); if(IoStatusBlock.Status != STATUS_SUCCESS) { DBGPRINT(ERROR, ("Couldn't create file\n")); } else { /// write data... Nunc est bibendum!

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups