Windows NT Driver >> Pascal >> File

DO_NEITHER_IO


參考資訊:
1. Source Code
2. delphidriverdevelopmentkit

基本上,Microsoft沒有定義DO_NEITHER_IO這個Flag,而Neither I/O的意思就是Neither Buffered Nor Direct I/O,因此,當沒有設定DO_BUFFERED_IO或DO_DIRECT_IO Flag時,就會使用Neither的方式,使用這種方式時,I/O Manager會將User Buffer的指標,透過操作後,傳遞給驅動程式使用,因此,驅動程式可以直接存取User Buffer的資料,所以存取速度可以得到很大的改善,對於有速度要求的驅動程式來說,建議使用Neither的方式,不過,關於User Buffer指標的存取問題上,會遇到更多需要面對的問題,如:指標的有效性以及多次存取的同步問題,因此,在選擇使用上,更需要考量到這些額外付出的代價是否值得使用,因為,驅動程式以及User Application雙方都需要很嚴謹的看待這塊共用的Buffer。

Microsoft的說明網頁:
using-neither-buffered-nor-direct-i-o

記憶體指標:

IRP Buffer Length
IRP_MJ_READ (Irp)
UserBuffer
(IrpStack)
Parameters.Read.Length
IRP_MJ_WRITE (Irp)
UserBuffer
(IrpStack)
Parameters.Write.Length

main.pas

unit main;

interface
  uses 
    DDDK;
    
  const 
    DEV_NAME = '\Device\MyDriver';
    SYM_NAME = '\DosDevices\MyDriver';
    
  function _DriverEntry(pOurDriver:PDriverObject; pOurRegistry:PUnicodeString):NTSTATUS; stdcall;

implementation
var
  szBuf: array[0..255] of char;
  
function IrpOpen(pOurDevice:PDeviceObject; pIrp:PIrp):NTSTATUS; stdcall;
begin
  DbgPrint('IRP_MJ_CREATE', []);
  Result:= STATUS_SUCCESS;
  pIrp^.IoStatus.Information:= 0;
  pIrp^.IoStatus.Status:= Result;
  IoCompleteRequest(pIrp, IO_NO_INCREMENT);
end;

function IrpRead(pOurDevice:PDeviceObject; pIrp:PIrp):NTSTATUS; stdcall;
var
  len: ULONG;

begin
  DbgPrint('IRP_MJ_READ', []);
  
  len:= strlen(@szBuf[0])+1;
  memcpy(pIrp^.UserBuffer, @szBuf[0], len);
  Result:= STATUS_SUCCESS;
  pIrp^.IoStatus.Information:= len;
  pIrp^.IoStatus.Status:= Result;
  IoCompleteRequest(pIrp, IO_NO_INCREMENT);
end;

function IrpWrite(pOurDevice:PDeviceObject; pIrp:PIrp):NTSTATUS; stdcall;
var
  len: ULONG;
  psk: PIoStackLocation;

begin
  DbgPrint('IRP_MJ_WRITE', []);

  psk:= IoGetCurrentIrpStackLocation(pIrp);
  len:= psk.Parameters.Write.Length;
  memcpy(@szBuf[0], pIrp^.UserBuffer, len);
  DbgPrint('Address: 0x%x, Length: %d', [pIrp^.UserBuffer, len]);
  Result:= STATUS_SUCCESS;
  pIrp^.IoStatus.Information:= len;
  pIrp^.IoStatus.Status:= Result;
  IoCompleteRequest(pIrp, IO_NO_INCREMENT);
end;

function IrpClose(pOurDevice:PDeviceObject; pIrp:PIrp):NTSTATUS; stdcall;
begin
  DbgPrint('IRP_MJ_CLOSE', []);
  Result:= STATUS_SUCCESS;
  pIrp^.IoStatus.Information:= 0;
  pIrp^.IoStatus.Status:= Result;
  IoCompleteRequest(pIrp, IO_NO_INCREMENT);
end;

procedure Unload(pOurDriver:PDriverObject); stdcall;
var
  szSymName: TUnicodeString;

begin
  RtlInitUnicodeString(@szSymName, SYM_NAME);
  IoDeleteSymbolicLink(@szSymName);
  IoDeleteDevice(pOurDriver^.DeviceObject);
end;

function _DriverEntry(pOurDriver:PDriverObject; pOurRegistry:PUnicodeString):NTSTATUS; stdcall;
var
  suDevName: TUnicodeString;
  szSymName: TUnicodeString;
  pOurDevice: PDeviceObject;
  
begin
  RtlInitUnicodeString(@suDevName, DEV_NAME);
  RtlInitUnicodeString(@szSymName, SYM_NAME);
  Result:= IoCreateDevice(pOurDriver, 0, @suDevName, FILE_DEVICE_UNKNOWN, 0, FALSE, pOurDevice);

  if NT_SUCCESS(Result) then
  begin
    pOurDriver^.MajorFunction[IRP_MJ_CREATE]:= @IrpOpen;
    pOurDriver^.MajorFunction[IRP_MJ_READ]  := @IrpRead;
    pOurDriver^.MajorFunction[IRP_MJ_WRITE] := @IrpWrite;
    pOurDriver^.MajorFunction[IRP_MJ_CLOSE] := @IrpClose;
    pOurDriver^.DriverUnload := @Unload;
    pOurDevice^.Flags:= pOurDevice^.Flags and not DO_DEVICE_INITIALIZING;
    Result:= IoCreateSymbolicLink(@szSymName, @suDevName);
  end;
end;
end.

IrpFile()收到IRP_MJ_WRITE時,Driver複製User Buffer的內容到szBuffer,而收到IRP_MJ_READ時,將szBuffer內容又複製回User Buffer,完成暫存的功能,IoStatus.Information的數值就是ReadFile()或WriteFile()完成的長度。

app.pas

program main;

{$APPTYPE CONSOLE}

uses
  Windows,
  Messages,
  SysUtils,
  Variants,
  Classes,
  Graphics,
  Controls,
  Forms,
  DIALOGS;

var
  fd: DWORD;
  ret: DWORD;
  len: DWORD;
  szBuf: array[0..255] of char;

begin
  fd:= CreateFile('\\.\MyDriver', GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ, Nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  if (fd <> INVALID_HANDLE_VALUE) then
  begin
    StrCopy(szBuf, 'I am error');
    len:= strlen(szBuf)+1;
    WriteLn(Output, Format('WR: %s, %d', [szBuf, len]));
    WriteFile(fd, szBuf, len, ret, Nil);
    ReadFile(fd, szBuf, len, ret, Nil);
    WriteLn(Output, Format('RD: %s, %d', [szBuf, ret]));
    CloseHandle(fd);
  end else
  begin
    WriteLn(Output, 'failed to open mydriver');
  end;
end.

結果


返回上一頁