unit DriverIOCtl; interface uses Windows, Classes, SysUtils; procedure WritePortB(Port: DWord; Value: byte); function ReadPortB(Port: DWord): byte; implementation const GPD_TYPE = 40000; METHOD_BUFFERED = 0; FILE_READ_ACCESS = 1; FILE_WRITE_ACCESS = 2; var HMassSpFileDevice: THandle; function CTL_CODE(ADeviceType, AFunction, AMethod, AAccess:DWord ):DWord; begin Result := (ADeviceType shl 16) or (AAccess shl 14) or (AFunction shl 2) or AMethod; end; {Writes one byte to absolute I/O port.} procedure WritePortB(Port: DWord; Value: byte); type WriteBufferB = record Port: DWord; Data: byte; end; var buf: WriteBufferB; ret: Cardinal; begin buf.Port := Port; buf.Data := Value; if (not DeviceIOControl(HMassSpFileDevice, CTL_CODE(GPD_TYPE, $910, METHOD_BUFFERED, FILE_WRITE_ACCESS), @buf, 5, nil, 0, ret, nil)) or (ret <> 0) then raise Exception.Create('Cannot write to device'); end; {Reads one byte from absolute I/O port.} function ReadPortB(Port: DWord): byte; var tmpport: DWord; ret: Cardinal; begin tmpport := Port; if (not DeviceIOControl(HMassSpFileDevice, CTL_CODE(GPD_TYPE, $900, METHOD_BUFFERED, FILE_READ_ACCESS), @tmpport, 4, @Result, 1, ret, nil)) or (ret <> 1) then raise Exception.Create('Cannot read from device'); end; initialization {Get handle of file object created by kernel mode driver} begin HMassSpFileDevice := CreateFile('\\.\PortIODev', GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0); if HMassSpFileDevice = INVALID_HANDLE_VALUE then raise Exception.Create('Cannot open device driver'); end; finalization {Release handle} begin CloseHandle(HMassSpFileDevice); end; end.