Delphi驱动开发研究第九篇--文件与目录(1)

 提供对文件的读写功能是操作系统的一项重要任务。我们来看一下NT家族的操作系统都为我们提供了那些功能。
9.1 核心句柄表
在开始讨论本文的主题之前,我们先来讨论一个重要的问题,我们之前并未对其给予应有的注意。为了取得对象的句柄需要填充OBJECT_ATTRIBUTES结构体——我们已经做过很多遍了,其样子如下:
InitializeObjectAttributes(oa, @g_usName,OBJ_CASE_INSENSITIVE,0, nil);

初始化了OBJECT_ATTRIBUTES后,我们调用函数创建或打开这个对象并获得其句柄(handle)。但这个句柄进入的是得到上下文的那个进程的句柄表。因为对于进程句柄表是其特有的,所以使用这个句柄就只能在进程自己的上下文中。例如,若是试图在其它进程的上下文中打开这个句柄的话,好的情况下会操作失败,而运气不好的话,要是在这个进程句柄表中有取值相同的句柄——要知道句柄只是一个32位的数(准确地讲是一个位的结构体)——就可能关闭其它对象。甚至如果获得的句柄是驱动句柄,但是是在用户进程中,句柄就会进入这个进程的句柄表并有可能有意或无意地在用户模式下使用对象。不希望的事情却总是发生,这样的情况常常出现。正是因此,内核组件和驱动程序有其特殊性,它们不喜欢使用句柄,而是使用reference to object,这样比较好,只需简单地使用指向内存中对象结构体的指针。为了统计对对象的引用,在对象的首部保存着一个引用计数(reference count)。如果需要像本例和上例中的那样访问对象,就要设计一个循环,在不同的上下文中对其进行访问,让系统将句柄放入核心句柄表中(kernel handle table)。
从Windows 2000开始,在系统中有了专门的核心句柄表。在这个表中的句柄只能内核模式下的任意进程上下文中访问,与进程特有的句柄不同。甚至于,比如说如果在System进程的上下文、在DriverEntry函数中获得句柄,则就不能在用户进程上下文中使用对象。System进程实现了自己私有的句柄表,其与核心句柄表不同。
对于在核心句柄表中的句柄,需要在调用InitializeObjectAttributes宏时显式地设置OBJ_KERNEL_HANDLE标志,形式如下:
InitializeObjectAttributes(oa, @g_usName, OBJ_KERNEL_HANDLE, 0,nil);

9.2 FileWorks驱动程序源代码
就像上一例中的驱动程序,本例的驱动程序的代码也是由几个独立的函数构成的:CreateDirectory、CreateFile、WriteFile、MarkAsReadOnly、ReadFile、UnmarkAsReadOnly、AppendFile、TruncateFile、DeleteFile、DeleteDirectory和EnumerateFiles。几乎所有的函数都是独立工作的。
unit FileWorks;

interface

uses
  nt_status, ntoskrnl, ntifs, native, winioctl, fcall, macros;

function _DriverEntry(pDriverObject:PDRIVER_OBJECT;
                      pusRegistryPath:PUNICODE_STRING): NTSTATUS; stdcall;

implementation

var
  g_usDirName: UNICODE_STRING;
  g_usFileName: UNICODE_STRING;

procedure CreateDirectory;
var
  oa: OBJECT_ATTRIBUTES;
  iosb: IO_STATUS_BLOCK;
  hDirectory: THANDLE;
  RtnCode: NTSTATUS;
begin
  { 还记得吧, 传递给DbgPrint函数的用于格式化Unicode的代码(%C, %S, %lc, %ls, %wc, %ws, %wZ)只能在
    IRQL = PASSIVE_LEVEL下调用! }
  DbgPrint(#13#10'FileWorks: Creating %ws directory'#13#10, g_usDirName.Buffer);

  InitializeObjectAttributes(oa, @g_usDirName,
                             OBJ_CASE_INSENSITIVE + OBJ_KERNEL_HANDLE,
                             0, nil);
  RtnCode := ZwCreateFile(@hDirectory, SYNCHRONIZE, @oa, @iosb, nil,
                          FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN_IF,
                          FILE_DIRECTORY_FILE + FILE_SYNCHRONOUS_IO_NONALERT,
                          nil, 0);
  if RtnCode = STATUS_SUCCESS then
  begin
    if iosb.Information = FILE_CREATED then
    begin
      DbgPrint('FileWorks: Directory created'#13#10);
    end else if iosb.Information = FILE_OPENED then
    begin
      DbgPrint('FileWorks: Directory exists and was opened'#13#10);
    end;
    ZwClose(hDirectory);
  end else
  begin
    DbgPrint('FileWorks: Can''t create directory. Status: %08X'#13#10, RtnCode);
  end;
end;

procedure CreateFile;
var
  oa:OBJECT_ATTRIBUTES;
  iosb:IO_STATUS_BLOCK;
  hFile:THANDLE;
  RtnCode: NTSTATUS;
begin
  DbgPrint(#13#10'FileWorks: Creating %ws file'#13#10, g_usFileName.Buffer);
  InitializeObjectAttributes(oa, @g_usFileName,
                             OBJ_CASE_INSENSITIVE + OBJ_KERNEL_HANDLE,
                             0, nil);

  RtnCode := ZwCreateFile(@hFile, SYNCHRONIZE, @oa, @iosb, nil,
                          FILE_ATTRIBUTE_NORMAL, 0,
                          FILE_CREATE,
                          FILE_SYNCHRONOUS_IO_NONALERT, nil, 0);
  if RtnCode = STATUS_SUCCESS then
  begin
    DbgPrint('FileWorks: File created'#13#10);
    ZwClose(hFile);
  end else
  begin
    DbgPrint('FileWorks: Can''t create file. Status: %08X'#13#10, RtnCode);
  end;
end;

procedure WriteFile;
var
  oa:OBJECT_ATTRIBUTES;
  iosb:IO_STATUS_BLOCK;
  hFile:THANDLE;
  RtnCode: NTSTATUS;
  g_szData: PAnsiChar;
begin
  DbgPrint(#13#10'FileWorks: Opening file for writing'#13#10);

  InitializeObjectAttributes(oa, @g_usFileName,
                             OBJ_CASE_INSENSITIVE + OBJ_KERNEL_HANDLE,
                             0, nil);
  RtnCode := ZwCreateFile(@hFile, FILE_WRITE_DATA + SYNCHRONIZE,
                          @oa, @iosb, nil, 0, FILE_SHARE_READ,
                          FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT,
                          nil, 0);
  if RtnCode = STATUS_SUCCESS then
  begin
    DbgPrint('FileWorks: File openeded'#13#10);
    g_szData := 'Data can be written to an open file';
    //RtlInitUnicodeString(g_szData, 'Data can be written to an open file');
    RtnCode := ZwWriteFile(hFile, 0, nil, nil, @iosb, g_szData,
                           strlen(g_szData), nil, nil);
    if RtnCode = STATUS_SUCCESS then
    begin
      DbgPrint('FileWorks: File was written'#13#10);
    end else
    begin
      DbgPrint('FileWorks: Can''t write to the file. Status: %08X\n', RtnCode);
    end;
    ZwClose(hFile);
  end else
  begin
    DbgPrint('FileWorks: Can''t open file. Status: %08X'#13#10, RtnCode);
  end;
end;

procedure MarkAsReadOnly;
var
  oa:OBJECT_ATTRIBUTES;
  iosb:IO_STATUS_BLOCK;
  hFile:THandle;
  fbi:FILE_BASIC_INFORMATION;
  RtnCode:NTSTATUS;
begin
  DbgPrint(#13#10'FileWorks: Opening file for changing attributes'#13#10);
  InitializeObjectAttributes(oa, @g_usFileName,
                             OBJ_CASE_INSENSITIVE + OBJ_KERNEL_HANDLE,
                             0, nil);
  RtnCode := ZwCreateFile(@hFile, FILE_READ_ATTRIBUTES + FILE_WRITE_ATTRIBUTES + SYNCHRONIZE,
                          @oa, @iosb, nil, 0, FILE_SHARE_READ,
                          FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT,
                          nil, 0);
  if RtnCode = STATUS_SUCCESS then
  begin
    DbgPrint('FileWorks: File openeded'#13#10);
    RtnCode := ZwQueryInformationFile(hFile, @iosb, @fbi,
                                      sizeof(fbi),
                                      FileBasicInformation);
    if RtnCode = STATUS_SUCCESS then
    begin
      DbgPrint('FileWorks: File attributes were: %08X'#13#10, fbi.FileAttributes);
      fbi.FileAttributes := fbi.FileAttributes or FILE_ATTRIBUTE_READONLY;
      RtnCode := ZwSetInformationFile(hFile, @iosb, @fbi,
                                      sizeof(fbi), FileBasicInformation);
      if RtnCode = STATUS_SUCCESS then
      begin
        DbgPrint('FileWorks: Now file marked as read-only'#13#10);
      end else
      begin
        DbgPrint('FileWorks: Can''t change file attributes. Status: %08X'#13#10, RtnCode);
      end;
    end else
    begin
      DbgPrint('FileWorks: Can''t query file attributes. Status: %08X'#13#10, RtnCode);
    end;
    ZwClose(hFile);
  end else
  begin
    DbgPrint('FileWorks: Can''t open file. Status: %08X\n', RtnCode);
  end;
end;

procedure ReadFile;
var
  oa:OBJECT_ATTRIBUTES;
  iosb:IO_STATUS_BLOCK;
  hFile:THANDLE;
  p:PVOID;
  cb:DWORD;
  fsi:FILE_STANDARD_INFORMATION;
  RtnCode: NTSTATUS;
begin
  DbgPrint(#13#10'FileWorks: Opening file for reading'#13#10);
  InitializeObjectAttributes(oa, @g_usFileName,
                             OBJ_CASE_INSENSITIVE + OBJ_KERNEL_HANDLE,
                             0, nil);
  RtnCode := ZwOpenFile(@hFile, FILE_READ_DATA + SYNCHRONIZE,
                        @oa, @iosb, FILE_SHARE_READ + FILE_SHARE_WRITE + FILE_SHARE_DELETE,
                        FILE_SYNCHRONOUS_IO_NONALERT);
  if RtnCode = STATUS_SUCCESS then
  begin
    DbgPrint('FileWorks: File openeded'#13#10);
    RtnCode := ZwQueryInformationFile(hFile, @iosb, @fsi,
                                      sizeof(fsi),
                                      FileStandardInformation);
    if RtnCode = STATUS_SUCCESS then
    begin
      cb := fsi.EndOfFile.LowPart + 1;
      p := ExAllocatePool(PagedPool, cb);
      if p <> nil then
      begin
        memset(p, 0, cb);
        RtnCode := ZwReadFile(hFile, 0, nil, nil, @iosb, p, cb, nil, nil);
        if RtnCode = STATUS_SUCCESS then
        begin
          DbgPrint('FileWorks: File content: \=%s\='#13#10, p);
        end else
        begin
          DbgPrint('FileWorks: Can''t read from the file. Status: %08X'#13#10, RtnCode);
        end;
        ExFreePool(p);
      end else
      begin
        DbgPrint('FileWorks: Can''t allocate memory. Status: %08X'#13#10, RtnCode);
      end;
    end else
    begin
      DbgPrint('FileWorks: Can''t query file size. Status: %08X'#13#10, RtnCode);
    end;
    ZwClose(hFile);
  end else
  begin
    DbgPrint('FileWorks: Can''t open file. Status: %08X'#13#10, RtnCode);
  end;
end;

procedure UnmarkAsReadOnly;
var
  oa:OBJECT_ATTRIBUTES;
  iosb:IO_STATUS_BLOCK;
  hFile:THANDLE;
  fbi:FILE_BASIC_INFORMATION;
  RtnCode:NTSTATUS;
begin
  DbgPrint(#13#10'FileWorks: Opening file for changing attributes'#13#10);
  InitializeObjectAttributes(oa, @g_usFileName,
                             OBJ_CASE_INSENSITIVE + OBJ_KERNEL_HANDLE,
                             0, nil);
  RtnCode := ZwCreateFile(@hFile, FILE_READ_ATTRIBUTES + FILE_WRITE_ATTRIBUTES + SYNCHRONIZE,
                          @oa, @iosb, nil, 0, FILE_SHARE_READ,
                          FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT,
                          nil, 0);
  if RtnCode = STATUS_SUCCESS then
  begin
    DbgPrint('FileWorks: File openeded'#13#10);
    RtnCode := ZwQueryInformationFile(hFile, @iosb, @fbi,
                                      sizeof(fbi), FileBasicInformation);
    if RtnCode = STATUS_SUCCESS then
    begin
      DbgPrint('FileWorks: File attributes were: %08X'#13#10, fbi.FileAttributes);
      fbi.FileAttributes := fbi.FileAttributes and (not FILE_ATTRIBUTE_READONLY);
      RtnCode := ZwSetInformationFile(hFile, @iosb, @fbi, sizeof(fbi), FileBasicInformation);
      if RtnCode = STATUS_SUCCESS then
      begin
        DbgPrint('FileWorks: Now file can be written or deleted'#13#10);
      end else
      begin
        DbgPrint('FileWorks: Can''t change file attributes. Status: %08X'#13#10, RtnCode);
      end;
    end else
    begin
      DbgPrint('FileWorks: Can''t query file attributes. Status: %08X'#13#10, RtnCode);
    end;
    ZwClose(hFile);
  end else
  begin
    DbgPrint('FileWorks: Can''t open file. Status: %08X'#13#10, RtnCode);
  end;
end;

procedure AppendFile;
var
  oa:OBJECT_ATTRIBUTES;
  iosb:IO_STATUS_BLOCK;
  hFile:THANDLE;
  g_szDataToAppend:PAnsiChar;
  RtnCode:NTSTATUS;
begin
  DbgPrint(#13#10'FileWorks: Opening file to append data'#13#10);
  InitializeObjectAttributes(oa, @g_usFileName,
                             OBJ_CASE_INSENSITIVE + OBJ_KERNEL_HANDLE,
                             0, nil);
  RtnCode := ZwOpenFile(@hFile, FILE_APPEND_DATA + SYNCHRONIZE,
                        @oa, @iosb, FILE_SHARE_READ,
                        FILE_SYNCHRONOUS_IO_NONALERT);
  if RtnCode = STATUS_SUCCESS then
  begin
    DbgPrint('FileWorks: File openeded'#13#10);
    g_szDataToAppend := ' using ZwWriteFile';
    RtnCode := ZwWriteFile(hFile, 0, nil, nil, @iosb, g_szDataToAppend,
                           strlen(g_szDataToAppend), nil, nil);
    if RtnCode = STATUS_SUCCESS then
    begin
      DbgPrint('FileWorks: Data appended to the file'#13#10);
    end else
    begin
      DbgPrint('FileWorks: Can''t append data to file. Status: %08X'#13#10, RtnCode);
    end;
    ZwClose(hFile);
  end else
  begin
    DbgPrint('FileWorks: Can''t open file. Status: %08X'#13#10, RtnCode);
  end;
end;

procedure TruncateFile;
var
  oa:OBJECT_ATTRIBUTES;
  iosb:IO_STATUS_BLOCK;
  hFile:THANDLE;
  fsi:FILE_STANDARD_INFORMATION;
  feofi:FILE_END_OF_FILE_INFORMATION;
  RtnCode:NTSTATUS;
begin
  DbgPrint(#13#10'FileWorks: Opening file to truncate'#13#10);
  InitializeObjectAttributes(oa, @g_usFileName,
                             OBJ_CASE_INSENSITIVE + OBJ_KERNEL_HANDLE,
                             0, nil);
  RtnCode := ZwOpenFile(@hFile, FILE_WRITE_DATA + SYNCHRONIZE,
                        @oa, @iosb, FILE_SHARE_READ,
                        FILE_SYNCHRONOUS_IO_NONALERT);
  if RtnCode = STATUS_SUCCESS then
  begin
    DbgPrint('FileWorks: File openeded'#13#10);
    RtnCode := ZwQueryInformationFile(hFile, @iosb, @fsi,
                                      sizeof(fsi),
                                      FileStandardInformation);
    if RtnCode = STATUS_SUCCESS then
    begin
      DbgPrint('FileWorks: EOF was: %08X'#13#10, fsi.EndOfFile.LowPart);
      feofi.EndOfFile.HighPart := 0;
      feofi.EndOfFile.LowPart := (fsi.EndOfFile.LowPart) shr 1;
      RtnCode := ZwSetInformationFile(hFile, @iosb, @feofi,
                                      sizeof(feofi),
                                      FileEndOfFileInformation);
      if RtnCode = STATUS_SUCCESS then
      begin
        DbgPrint('FileWorks: File truncated to its half size'#13#10);
      end else
      begin
        DbgPrint('FileWorks: Can''t truncate file. Status: %08X'#13#10, RtnCode);
      end;
    end else
    begin
      DbgPrint('FileWorks: Can''t query file info. Status: %08X'#13#10, RtnCode);
    end;
    ZwClose(hFile);
  end else
  begin
    DbgPrint('FileWorks: Can''t open file. Status: %08X'#13#10, RtnCode);
  end;
end;

procedure DeleteFile;
var
  oa:OBJECT_ATTRIBUTES;
  iosb:IO_STATUS_BLOCK;
  hFile:THANDLE;
  fdi:FILE_DISPOSITION_INFORMATION;
  RtnCode:NTSTATUS;
begin
  DbgPrint(#13#10'FileWorks: Opening file for deletion'#13#10);
  InitializeObjectAttributes(oa, @g_usFileName,
                             OBJ_CASE_INSENSITIVE + OBJ_KERNEL_HANDLE,
                             0, nil);
  RtnCode := ZwCreateFile(@hFile, _DELETE + SYNCHRONIZE,
                          @oa, @iosb, nil, 0, FILE_SHARE_DELETE,
                          FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT,
                          nil, 0);
  if RtnCode = STATUS_SUCCESS then
  begin
    DbgPrint('FileWorks: File openeded'#13#10);
    fdi.DeleteFile := True;
    RtnCode := ZwSetInformationFile(hFile, @iosb, @fdi,
                                    sizeof(fdi),
                                    FileDispositionInformation);
    if RtnCode = STATUS_SUCCESS then
    begin
      DbgPrint('FileWorks: File has been marked for deletion'#13#10);
      DbgPrint('FileWorks: It should be deleted when the last open handle is closed'#13#10);
    end else
    begin
      DbgPrint('FileWorks: Can''t mark file for deletion. Status: %08X'#13#10, RtnCode);
    end;
    ZwClose(hFile);
  end else
  begin
    DbgPrint('FileWorks: Can''t open file. Status: %08X'#13#10, RtnCode);
  end;
end;

procedure DeleteDirectory;
var
  oa:OBJECT_ATTRIBUTES;
  RtnCode:NTSTATUS;
begin
  InitializeObjectAttributes(oa, @g_usDirName,
                             OBJ_CASE_INSENSITIVE + OBJ_KERNEL_HANDLE,
                             0, nil);
  RtnCode := ZwDeleteFile(@oa);
  if RtnCode = STATUS_SUCCESS then
  begin
    DbgPrint(#13#10'FileWorks: Directory should be deleted'#13#10);
  end else
  begin
    DbgPrint(#13#10'FileWorks: Can''t delete directory. Status: %08X'#13#10, RtnCode);
  end;
end;

procedure EnumerateFiles;
var
  oa:OBJECT_ATTRIBUTES;
  hSystemRootDirectory:THANDLE;
  hDriversDirectory:THANDLE;
  _as:ANSI_STRING;
  us:UNICODE_STRING;
  iosb:IO_STATUS_BLOCK;
  tf:TIME_FIELDS;
  cb:DWORD;
  pfdi:PFILE_DIRECTORY_INFORMATION;
  RtnCode:NTSTATUS;
  g_usTemp:UNICODE_STRING;
begin
  DbgPrint(#13#10'FileWorks: Opening directory to enumerate files'#13#10);
  RtlInitUnicodeString(g_usTemp, '\SystemRoot');
  InitializeObjectAttributes(oa, @g_usTemp,
                             OBJ_CASE_INSENSITIVE + OBJ_KERNEL_HANDLE,
                             0, nil);
  RtnCode := ZwOpenFile(@hSystemRootDirectory, FILE_LIST_DIRECTORY + SYNCHRONIZE,
                        @oa, @iosb, FILE_SHARE_READ + FILE_SHARE_WRITE + FILE_SHARE_DELETE,
                        FILE_DIRECTORY_FILE + FILE_SYNCHRONOUS_IO_NONALERT);
  if RtnCode = STATUS_SUCCESS then
  begin
    RtlInitUnicodeString(g_usTemp, 'system32\drivers');
    InitializeObjectAttributes(oa, @g_usTemp,
                               OBJ_CASE_INSENSITIVE + OBJ_KERNEL_HANDLE,
                               hSystemRootDirectory, nil);
    RtnCode := ZwOpenFile(@hDriversDirectory, FILE_LIST_DIRECTORY + SYNCHRONIZE,
                          @oa, @iosb, FILE_SHARE_READ + FILE_SHARE_WRITE + FILE_SHARE_DELETE,
                          FILE_DIRECTORY_FILE + FILE_SYNCHRONOUS_IO_NONALERT);
    if RtnCode = STATUS_SUCCESS then
    begin
      cb := sizeof(FILE_DIRECTORY_INFORMATION) + 256;
      pfdi := ExAllocatePool(PagedPool, cb);
      if pfdi <> nil then
      begin
        RtlInitUnicodeString(g_usTemp, 'c*');
        DbgPrint(#13#10'FileWorks: ---------- Starting enumerate files ----------'#13#10);
        RtnCode := ZwQueryDirectoryFile(hDriversDirectory, 0, nil, nil, @iosb,
                                        pfdi, cb, FileDirectoryInformation,
                                        true, @g_usTemp, true);
        while RtnCode <> STATUS_NO_MORE_FILES do
        begin
          if RtnCode = STATUS_SUCCESS then
          begin
            us.Length := pfdi^.FileNameLength;
            us.MaximumLength := pfdi^.FileNameLength;
            us.Buffer := PWideChar(@pfdi^.FileName);
            if RtlUnicodeStringToAnsiString(@_as, @us, true) = STATUS_SUCCESS then
            begin
              RtlTimeToTimeFields(@pfdi^.CreationTime, @tf);
              DbgPrint(' %s size=%d created on %d.%02d.%04d'#13#10,
                       _as.Buffer, pfdi^.EndOfFile.LowPart,
                       BYTE(tf.Day), BYTE(tf.Month), WORD(tf.Year));
              RtlFreeAnsiString(@_as);
            end;
          end;
          RtnCode := ZwQueryDirectoryFile(hDriversDirectory, 0, nil, nil, @iosb,
                                          pfdi, cb, FileDirectoryInformation,
                                          true, nil, false);
        end; {End While}
        DbgPrint('FileWorks: ------------------------------------------------'#13#10);
        ExFreePool(pfdi);
      end;
      ZwClose(hDriversDirectory);
    end else
    begin
      DbgPrint('FileWorks: Can''t open drivers directory. Status: %08X', RtnCode);
    end;
    ZwClose(hSystemRootDirectory);
  end else
  begin
    DbgPrint('FileWorks: Can''t open system root directory. Status: %08X'#13#10, RtnCode);
  end;
end;

function _DriverEntry(pDriverObject:PDRIVER_OBJECT;
                      pusRegistryPath:PUNICODE_STRING): NTSTATUS; stdcall;
begin
  DbgPrint(#13#10'FileWorks: Entering DriverEntry'#13#10);
  RtlInitUnicodeString(g_usFileName, '\??\c:\FileWorks\test.txt');
  RtlInitUnicodeString(g_usDirName, '\??\c:\FileWorks');
  CreateDirectory;
  CreateFile;
  WriteFile;
  MarkAsReadOnly;
  ReadFile;
  UnmarkAsReadOnly;
  AppendFile;
  ReadFile;
  TruncateFile;
  ReadFile;
  DeleteFile;
  DeleteDirectory;
  EnumerateFiles;
  DbgPrint(#13#10'FileWorks: Leaving DriverEntry'#13#10);
  result := STATUS_DEVICE_CONFIGURATION_ERROR;
end;

end.


9.3 创建目录与文件
InitializeObjectAttributes(oa, @g_usDirName, OBJ_CASE_INSENSITIVE + OBJ_KERNEL_HANDLE, 0, nil);
填充OBJECT_ATTRIBUTES结构体,不要忘了OBJ_KERNEL_HANDLE标志。我想要强调的是,在本例中这个操作并不是必需的,因为我们不会在其它进程中使用这些句柄。但是因为FileWorks驱动程序的函数可以很容易的移植到使用任意进程上下文的程序中,所以我决定设置这个标志。如果在您的程序中有驱动和用户进程的共用的句柄,则不应设置此标志。如果只在自己一个进程上下文中使用对象,也不必设置OBJ_KERNEL_HANDLE标志。
创建目录和文件用的都是ZwCreateFile函数。从系统角度看,目录也是文件,所以创建目录的函数与创建文件的函数没有本质上的差别。所以,创建目录与创建文件使用同一个函数,只是创建目录要用FILE_DIRECTORY_FILE标志。
RtnCode := ZwCreateFile(@hDirectory, SYNCHRONIZE, @oa, @iosb, nil,FILE_ATTRIBUTE_NORMAL, 0, 
                                    FILE_OPEN_IF,FILE_DIRECTORY_FILE+FILE_SYNCHRONOUS_IO_NONALERT,
                                    nil, 0);

ZwCreateFile函数有相当多的参数,所以我在下面给出了它的原型,而且不得不使用了C语言的语义,以显示出输入(IN)、输出(OUT)和可选的参数。
NTSTATUS 
ZwCreateFile(
OUT PHANDLE FileHandle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes,
OUT PIO_STATUS_BLOCK IoStatusBlock,
IN PLARGE_INTEGER AllocationSize OPTIONAL,
IN ULONG FileAttributes,
IN ULONG ShareAccess,
IN ULONG CreateDisposition,
IN ULONG CreateOptions,
IN PVOID EaBuffer OPTIONAL,
IN ULONG EaLength
);

函数成功完成后,参数FileHandle取得所建立的目录的句柄。DesiredAccess定义了对目录的三种访问请求。我们用标志SYNCHRONIZE来定义,这个值的含义在后面会清楚。ObjectAttributes想必您已经知道了。函数成功完成后,参数IoStatusBlock就是指向IO_STATUS_BLOCK结构体的指针,从中可以取出相关的的信息。参数FileAttributes定义了所创建目录的属性(只读、隐藏等)。我们使用FILE_ATTRIBUTE_NORMAL,本例中不需要为目录指定什么特别的属性。可选参数AllocationSize为0定义了所创建文件的大小为0。对于目录来说这是很自然的。参数ShareAccess定义为什么样的值,就要以什么样的权限访问目录。在本例中,我们不允许其它程序访问目录,故将此参数设为0。参数CreateDisposition定义了该目录已经存在,或者相反,文件不存在时系统的行为。我们使用标志FILE_OPEN_IF,这个标志表示如果目录已经存在,就将其打开。我们向参数CreateOptions传递的是标志FILE_DIRECTORY_FILE和FILE_SYNCHRONOUS_IO_NONALERT的组合。第一个表示要建立的是目录而非文件,无需特别解释。FILE_SYNCHRONOUS_IO_NONALERT定义了()对文件所有的操作都要是同步的,例如,调用ZwReadFile后在没有实际读取完文件数据时函数不会返回。在I/O管理器中为文件维护了一个当前文件位置上下文(file position context)。如果设置了标志FILE_SYNCHRONOUS_IO_NONALERT,则在参数DesiredAccess里应该定义为标志SYNCHRONIZE。最后两个参数不用于驱动程序。
if RtnCode = STATUS_SUCCESS then
begin
    if iosb.Information = FILE_CREATED then
    begin
      ……      
    end else if iosb.Information = FILE_OPENED then
    begin
      ……      
end;

正如我所讲的,在IO_STATUS_BLOCK结构体中会有额外的信息。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值