Windows Driver Model >> Pascal

StartIO


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

StartIO提供一個序列化的機制,讓使用者可以依照順序處理IRP,這個機制對於某些只能序列執行的硬體相當有幫助,省去用軟體實現序列化的程式碼,使用的步驟:
1. IoMarkIrpPending()
2. IoStartNextPacket()

main.pas

unit main;

interface
  uses
    DDDK;
    
  const
    DEV_NAME = '\Device\MyDriver';
    SYM_NAME = '\DosDevices\MyDriver';

  function _DriverEntry(pOurDriver:PDRIVER_OBJECT; pOurRegistry:PUNICODE_STRING):NTSTATUS; stdcall;

implementation
var
  pNextDevice: PDEVICE_OBJECT;
  szBuf: array[0..255] of char;
  
procedure Unload(pOurDriver:PDRIVER_OBJECT); stdcall;
begin
end;

function StartIo(pOurDevice:PDeviceObject; pIrp:PIrp):NTSTATUS; stdcall;
var
  len: ULONG;
  psk: PIO_STACK_LOCATION;
   
begin
  len:= 0;
  psk:= IoGetCurrentIrpStackLocation(pIrp);
  if psk^.MajorFunction = IRP_MJ_WRITE then
  begin
    DbgPrint('StartIo, IRP_MJ_WRITE', []);
    len:= psk.Parameters.Write.Length;
    memcpy(@szBuf[0], pIrp^.AssociatedIrp.SystemBuffer, len);
    DbgPrint('Buffer: %s, Length: %d', [szBuf, len]);
  end
  else if psk^.MajorFunction = IRP_MJ_READ then
  begin
    DbgPrint('StartIo, IRP_MJ_READ', []);
    len:= strlen(@szBuf[0])+1;
    memcpy(pIrp^.AssociatedIrp.SystemBuffer, @szBuf[0], len);
  end;
   
  Result:= STATUS_SUCCESS;
  pIrp^.IoStatus.Information:= len;
  pIrp^.IoStatus.Status:= Result;
  IoCompleteRequest(pIrp, IO_NO_INCREMENT);
  IoStartNextPacket(pOurDevice, FALSE);
end;

function IrpFile(pOurDevice:PDEVICE_OBJECT; pIrp:PIRP):NTSTATUS; stdcall;
var
  psk: PIO_STACK_LOCATION;
  
begin
  psk:= IoGetCurrentIrpStackLocation(pIrp);
  case psk^.MajorFunction of
  IRP_MJ_CREATE:
    DbgPrint('IRP_MJ_CREATE', []);
  IRP_MJ_READ:
    begin
      DbgPrint('IrpFile, IRP_MJ_READ', []);
      IoMarkIrpPending(pIrp);
      IoStartPacket(pOurDevice, pIrp, Nil, Nil);
      Result:= STATUS_PENDING;
      exit;
    end;
  IRP_MJ_WRITE:
    begin
      DbgPrint('IrpFile, IRP_MJ_WRITE', []);
      IoMarkIrpPending(pIrp);
      IoStartPacket(pOurDevice, pIrp, Nil, Nil);
      Result:= STATUS_PENDING;
      exit;
    end;
  IRP_MJ_CLOSE:
    DbgPrint('IRP_MJ_CLOSE', []);
  end;
  
  Result:= STATUS_SUCCESS;
  pIrp^.IoStatus.Status:= Result;
  pIrp^.IoStatus.Information:= 0;
  IoCompleteRequest(pIrp, IO_NO_INCREMENT);
end;

function IrpPnp(pOurDevice:PDEVICE_OBJECT; pIrp:PIRP):NTSTATUS; stdcall;
var
  psk: PIO_STACK_LOCATION;
  suSymName: UNICODE_STRING;
  
begin
  psk:= IoGetCurrentIrpStackLocation(pIrp);
  if psk^.MinorFunction = IRP_MN_REMOVE_DEVICE then
  begin
    RtlInitUnicodeString(@suSymName, SYM_NAME);
    IoDetachDevice(pNextDevice);
    IoDeleteDevice(pOurDevice);
    IoDeleteSymbolicLink(@suSymName);
  end;
  IoSkipCurrentIrpStackLocation(pIrp);
  Result:= IoCallDriver(pNextDevice, pIrp);
end;

function AddDevice(pOurDriver:PDRIVER_OBJECT; pPhyDevice:PDEVICE_OBJECT):NTSTATUS; stdcall;
var
  suDevName: UNICODE_STRING;
  suSymName: UNICODE_STRING;
  pOurDevice: PDEVICE_OBJECT;
  
begin
  RtlInitUnicodeString(@suDevName, DEV_NAME);
  RtlInitUnicodeString(@suSymName, SYM_NAME);
  IoCreateDevice(pOurDriver, 0, @suDevName, FILE_DEVICE_UNKNOWN, 0, FALSE, pOurDevice);
  pNextDevice:= IoAttachDeviceToDeviceStack(pOurDevice, pPhyDevice);
  pOurDevice^.Flags:= pOurDevice^.Flags or DO_BUFFERED_IO;
  pOurDevice^.Flags:= pOurDevice^.Flags and not DO_DEVICE_INITIALIZING;
  Result:= IoCreateSymbolicLink(@suSymName, @suDevName);
end;

function _DriverEntry(pOurDriver:PDRIVER_OBJECT; pOurRegistry:PUNICODE_STRING):NTSTATUS; stdcall;
begin
  pOurDriver^.MajorFunction[IRP_MJ_PNP]:= @IrpPnp;
  pOurDriver^.MajorFunction[IRP_MJ_CREATE]:= @IrpFile;
  pOurDriver^.MajorFunction[IRP_MJ_READ]:= @IrpFile;
  pOurDriver^.MajorFunction[IRP_MJ_WRITE]:= @IrpFile;
  pOurDriver^.MajorFunction[IRP_MJ_CLOSE]:= @IrpFile;
  pOurDriver^.DriverStartIo := @StartIo;
  pOurDriver^.DriverExtension^.AddDevice:=@AddDevice;
  pOurDriver^.DriverUnload:=@Unload;
  Result:=STATUS_SUCCESS;
end;
end.

IrpFile()IRP_MJ_READ和IRP_MJ_WRITE使用StartIO方式處理

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', [szBuf]));
    WriteLn(Output, Format('Length: %d', [len]));
    WriteFile(fd, szBuf, len, ret, Nil);
    ReadFile(fd, szBuf, len, ret, Nil);
    WriteLn(Output, Format('RD: %s', [szBuf]));
    WriteLn(Output, Format('Length: %d', [ret]));
    CloseHandle(fd);
  end else
  begin
    WriteLn(Output, 'failed to open mydriver');
  end;
end.

結果


返回上一頁