unit ULDrvUtl;
interface
uses windows, winioctl, ULDrvTyp, registry;

function UL_IO(fn: DWORD): DWORD;
function UL_IOR(fn: DWORD): DWORD;
function UL_IOW(fn: DWORD): DWORD;

function UL_NEWMSG: DWORD;
function UL_TAILMSG: DWORD;
function UL_FREEMSG: DWORD;
function  UL_ACCEPTMSG: DWORD;
function UL_ACTAILMSG: DWORD;
function UL_ADDFILT: DWORD;
function UL_ABORTMSG: DWORD;
function UL_REWMSG: DWORD;
function UL_INEPOLL: DWORD;
function UL_KLOGBLL: DWORD;
function UL_STROKE: DWORD;
function UL_DEBFLG: DWORD;

{v0.11}
procedure ULDrvReqReadWrite(read:boolean; var APortAddr: integer;
  var APortIrq: integer; var APCUlanAddr: integer);
  { if read then read parameters from registry, if not found there,
    remain unchanged;
    if not read then write the parameters to registry }
{/v0.11}

implementation

function UL_IO(fn: DWORD): DWORD;
begin
  UL_IO := CTL_CODE(FILE_DEVICE_ULAN, UL_IOCTL_INDEX + fn,
    METHOD_BUFFERED, FILE_ANY_ACCESS)
end;

function UL_IOR(fn: DWORD): DWORD;
begin
  UL_IOR := CTL_CODE(FILE_DEVICE_ULAN, UL_IOCTL_INDEX + fn,
    METHOD_BUFFERED, FILE_ANY_ACCESS)
end;

function UL_IOW(fn: DWORD): DWORD;
begin
  UL_IOW := CTL_CODE(FILE_DEVICE_ULAN, UL_IOCTL_INDEX + fn,
    METHOD_BUFFERED, FILE_ANY_ACCESS)
end;

function UL_NEWMSG:DWORD;
begin
  UL_NEWMSG := UL_IOW($11);
end;

function UL_TAILMSG:DWORD;
begin
  UL_TAILMSG := UL_IOW($12)
end;

function UL_FREEMSG:DWORD;
begin
  UL_FREEMSG :=	UL_IO($13)
end;

function  UL_ACCEPTMSG:DWORD;
begin
  UL_ACCEPTMSG := UL_IOR($14);
end;

function  UL_ACTAILMSG:DWORD;
begin
  UL_ACTAILMSG := UL_IOR($15);
end;

function  UL_ADDFILT:DWORD;
begin
  UL_ADDFILT := UL_IOW($16);
end;

function UL_ABORTMSG: DWORD;
begin
  UL_ABORTMSG := UL_IO($17);
end;

function UL_REWMSG: DWORD;
begin
  UL_REWMSG := UL_IO($18);
end;

function UL_INEPOLL: DWORD;
begin
  UL_INEPOLL := UL_IO($19);
end;

function UL_KLOGBLL: DWORD;
begin
  UL_KLOGBLL := UL_IO($41);
end;

function UL_STROKE: DWORD;
begin
  UL_STROKE := UL_IO($42);
end;

function UL_DEBFLG: DWORD;
begin
  UL_DEBFLG := UL_IO($43)
   { arg = new debug mask }
end;

{v0.11}
const
  IsInRW:boolean = false;
procedure ULDrvReqReadWrite(read:boolean; var APortAddr: integer;
  var APortIrq: integer; var APCUlanAddr: integer);
const
{ RootKey = 'HKEY_LOCAL_MACHINE';}
  ServicesPath = '\SYSTEM\CurrentControlSet\Services';
  UlKey = 'ul_drv';
  ParKey = 'Parameters';

var
  tPortAddr: integer;
  tPortIrq: integer;
  tPCUlanAddr: integer;

var r: TRegistry;
begin
  if IsInRW then
    exit;
  IsInRw := true;
  r := nil;
  try
  r := TRegistry.Create;
  r.LazyWrite := false;
  r.RootKey := HKEY_LOCAL_MACHINE;
  if r.OpenKey(ServicesPath, false) then begin
    if r.OpenKey(UlKey, true) then begin
      r.WriteInteger('Type',1);
      r.WriteInteger('Start',1);
      r.WriteInteger('ErrorControl',1);
      r.WriteString('DisplayName', 'uLan driver');
      r.WriteString('Group','port');
      r.WriteInteger('Tag',1);
      r.CloseKey;

      if r.OpenKey(ServicesPath + '\' + UlKey, false) then begin
        if r.OpenKey(ParKey, true) then begin
          if read then begin
            try
              tPortAddr := r.ReadInteger('Port Address');
            except
              tPortAddr := APortAddr;
            end;
            APortAddr := tPortAddr;
          end;
          r.WriteInteger('Port Address', APortAddr);

          if read then begin
            try
              tPortIrq := r.ReadInteger('IRQ Line');
            except
              tPortIrq := APortIrq;
            end;
            APortIrq := tPortIrq;
          end;
          r.WriteInteger('IRQ Line', APortIrq);

          r.WriteInteger('Baud Rate', $2580);
          r.WriteInteger('Buffer Size', $4000);

          if read then begin
            try
              tPCUlanAddr := r.ReadInteger('My Addr');
            except
              tPCUlanAddr := APCUlanAddr;
            end;
            APCUlanAddr := tPCUlanAddr;
          end;
          r.WriteInteger('My Addr', APCUlanAddr);

          r.WriteInteger('Debug', $19);
          r.CloseKey;
        end;
      end;
    end;
  end;
{
REGEDIT4

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\ul_drv]
"Type"=dword:00000001
"Start"=dword:00000001
"ErrorControl"=dword:00000001
"DisplayName"="uLan driver"
"Group"="port"
"Tag"=dword:00000001

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\ul_drv\Parameters]
"Port Address"=dword:000002f8
"IRQ Line"=dword:00000003
"Baud Rate"=dword:00002580
"Buffer Size"=dword:00004000
"My Addr"=dword:00000002
"Debug"=dword:00000019
}
  finally
    r.Free;
    IsInRW := false;
  end;
end;
{/v0.11}


end.
