⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gwiopm.pas

📁 在Windows NT使用I/O端口,包括装载,开始,卸载驱动程序的函数.
💻 PAS
📖 第 1 页 / 共 2 页
字号:
  serviceStatus: TServiceStatus;
  temp: LongBool;
Begin
  hService := 0;
  dwStatus := 0;

  // get a handle to the service
  hService := OpenService(hSCMan, PChar(DriverName), SERVICE_ALL_ACCESS);
  if hService <> 0 then
  Begin
     // stop the driver
     temp := ControlService(hService, SERVICE_CONTROL_STOP, serviceStatus);
     if not temp then dwStatus := GetLastError();
  end else dwStatus := GetLastError();

  if (hService <> 0) then CloseServiceHandle(hService);
  result := dwStatus;
end;

//-------------------------------------------
function TGWIOPM_Driver.Remove:  DWORD;
//-------------------------------------------
Var
  hService: SC_HANDLE;
  dwStatus: DWORD;
  temp: LongBool;
Begin
  hService := 0;
  dwStatus := 0;

  dwStatus := Stop;  // ignore result
  dwStatus := 0;

  // get a handle to the service
  hService := OpenService(hSCMan, PChar(DriverName), SERVICE_ALL_ACCESS);
  if hService <> 0 then
  Begin
     temp := DeleteService(hService);
     if not temp then dwStatus := GetLastError();
  end else dwStatus := GetLastError();

  if (hService <> 0) then CloseServiceHandle(hService);
  result := dwStatus;
end;

//=============================================================
// Device Open/Close functions
//=============================================================

//-------------------------------------------
function TGWIOPM_Driver.DeviceOpen:  DWORD;
//-------------------------------------------
Var
  dwStatus: DWORD;
Begin
  dwStatus := 0;

  if hDevice <> INVALID_HANDLE_VALUE then DeviceClose;

  // get a handle to the device
  hDevice := CreateFile(
             { lpFileName: PChar            } '\\.\'+ DEVICE_NAME_STRING,
             { dwDesiredAccess: integer     } GENERIC_READ or GENERIC_WRITE,
             { dwShareMode: Integer         } 0,
             { lpSecurityAttributes         } PSECURITY_DESCRIPTOR(nil),
             { dwCreationDisposition: DWORD } OPEN_EXISTING,
             { dwFlagsAndAttributes: DWORD  } FILE_ATTRIBUTE_NORMAL,
             { hTemplateFile: THandle       } 0);

  if hDevice = INVALID_HANDLE_VALUE then
  Begin
    dwStatus := GetLastError();
  end;

  result := dwStatus;
end;

//-------------------------------------------
function TGWIOPM_Driver.DeviceClose:  DWORD;
//-------------------------------------------
Var
  dwStatus: DWORD;
Begin
  dwStatus := 0;
  if (hDevice <> INVALID_HANDLE_VALUE) then CloseHandle(hDevice);
  hDevice := INVALID_HANDLE_VALUE;
  result := dwStatus; { assume that it went OK? }
end;

//=================================================================
// IO Permission Map functions
//=================================================================
Const
  GWIO_PARAMCOUNT     = 3;
  GWIO_BYTES_IN  = GWIO_PARAMCOUNT * 4;
  GWIO_BYTES_OUT = GWIO_PARAMCOUNT * 4;

Type
  TGWIO_PARAMS = array[0..GWIO_PARAMCOUNT-1] of longint;

//-------------------------------------------
function TGWIOPM_Driver.IOCTL_IOPMD_Misc1(var RetVal: DWORD; Cmd: integer):  DWORD;
//-------------------------------------------
Var
  dwStatus: DWORD;
  temp: LongBool;
  BytesReturned: DWORD;
  MyControlCode: DWORD;
  InBuf:  TGWIO_PARAMS;
  OutBuf: TGWIO_PARAMS;

Begin
  dwStatus := 0;
  RetVal := 0;
  InBuf[0] := 0;
  InBuf[1] := 0;
  InBuf[2] := 0;

  if hDevice = INVALID_HANDLE_VALUE then
  Begin
    dwStatus := ERROR_NO_DEVICE_HANDLE;
  end else
  Begin
    MyControlCode := CTL_CODE(IOPMD_TYPE, Cmd , METHOD_BUFFERED, FILE_ANY_ACCESS);

    BytesReturned := 0;
    temp := DeviceIoControl(hDevice, MyControlCode ,
            { in buffer  (to driver)   }  @InBuf,  GWIO_BYTES_IN,
            { out buffer (from driver) }  @OutBuf, GWIO_BYTES_OUT,
            BytesReturned, nil);
    if temp then
    Begin
      RetVal := OutBuf[0];
    end else
    Begin
      dwStatus := GetLastError();
    end;
  end;

  result := dwStatus;
end;

//-------------------------------------------
function TGWIOPM_Driver.IOCTL_IOPMD_READ_TEST(var RetVal: DWORD):  DWORD;
//-------------------------------------------
Begin
  result := IOCTL_IOPMD_Misc1(RetVal, IOCMD_IOPMD_READ_TEST);
end;

//-------------------------------------------
function TGWIOPM_Driver.IOCTL_IOPMD_READ_VERSION(var RetVal: DWORD):  DWORD;
//-------------------------------------------
Begin
  result := IOCTL_IOPMD_Misc1(RetVal, IOCMD_IOPMD_READ_VERSION);
end;

//-------------------------------------------
function TGWIOPM_Driver.IOCTL_IOPMD_CLEAR_LIOPM:  DWORD;
//-------------------------------------------
Var
  RetVal: DWORD;
Begin
  result := IOCTL_IOPMD_Misc1(RetVal, IOCMD_IOPMD_CLEAR_LIOPM );
end;

//-------------------------------------------
function TGWIOPM_Driver.IOCTL_IOPMD_GET_SET_LIOPM(Addr: Word; var B: byte; cmd: integer): DWORD;
//-------------------------------------------
Var
  dwStatus: DWORD;
  temp: LongBool;
  BytesReturned: DWORD;
  MyControlCode: DWORD;
  InBuf:  TGWIO_PARAMS;
  OutBuf: TGWIO_PARAMS;

Begin
  dwStatus := 0;

  if hDevice = INVALID_HANDLE_VALUE then
  Begin
    dwStatus := ERROR_NO_DEVICE_HANDLE;
  end else
  Begin
    MyControlCode := CTL_CODE(IOPMD_TYPE, cmd, METHOD_BUFFERED, FILE_ANY_ACCESS);

    InBuf[0] := Addr;
    InBuf[1] := B;

    BytesReturned := 0;
    temp := DeviceIoControl(hDevice, MyControlCode ,
            { in buffer  (to driver)   }  @InBuf,  GWIO_BYTES_IN,
            { out buffer (from driver) }  @OutBuf, GWIO_BYTES_OUT,
            BytesReturned, nil);
    if temp then
    Begin
      B := Lo(OutBuf[1]);
    end else dwStatus := GetLastError();
  end;

  result := dwStatus;
end;

//-------------------------------------------
function TGWIOPM_Driver.IOCTL_IOPMD_SET_LIOPM(Addr: Word; B: byte):  DWORD;
//-------------------------------------------
Var
  RetVal: DWORD;
Begin
  result := IOCTL_IOPMD_GET_SET_LIOPM(Addr, B, IOCMD_IOPMD_SET_LIOPM);
end;

//-------------------------------------------
function TGWIOPM_Driver.IOCTL_IOPMD_GET_LIOPMB(Addr: Word; var B: byte):  DWORD;
//-------------------------------------------
Var
  RetVal: DWORD;
Begin
  result := IOCTL_IOPMD_GET_SET_LIOPM(Addr, B, IOCMD_IOPMD_GET_LIOPMB);
end;

//-------------------------------------------
function TGWIOPM_Driver.IOCTL_IOPMD_GET_LIOPMA(var A: TIOPM): DWORD;
//-------------------------------------------
// get entire LIOPM array (diagnostic)
Var
  dwStatus: DWORD;
  temp: LongBool;
  BytesReturned: DWORD;
  MyControlCode: DWORD;
  InBuf:  TGWIO_PARAMS;
//  OutBuf: TGWIO_PARAMS;

Begin
  dwStatus := 0;

  if hDevice = INVALID_HANDLE_VALUE then
  Begin
    dwStatus := ERROR_NO_DEVICE_HANDLE;
  end else
  Begin
    MyControlCode := CTL_CODE(IOPMD_TYPE, IOCMD_IOPMD_GET_LIOPMA, METHOD_BUFFERED, FILE_ANY_ACCESS);

    InBuf[0] := 0;
    InBuf[1] := 0;
    InBuf[2] := 0;

    BytesReturned := 0;
    temp := DeviceIoControl(hDevice, MyControlCode ,
            { in buffer  (to driver)   }  @InBuf,  GWIO_BYTES_IN,
            { out buffer (from driver) }  @A,      IOPM_SIZE,
            BytesReturned, nil);
    if temp then
    Begin
      // do nothing
    end else dwStatus := GetLastError();
  end;
  result := dwStatus;
end;

//-------------------------------------------
function TGWIOPM_Driver.IOCTL_IOPMD_ACTIVATE_KIOPM:  DWORD;
//-------------------------------------------
Var
  RetVal: DWORD;
Begin
  result := IOCTL_IOPMD_Misc1(RetVal, IOCMD_IOPMD_ACTIVATE_KIOPM );
end;

//-------------------------------------------
function TGWIOPM_Driver.IOCTL_IOPMD_DEACTIVATE_KIOPM:  DWORD;
//-------------------------------------------
Var
  RetVal: DWORD;
Begin
  result := IOCTL_IOPMD_Misc1(RetVal, IOCMD_IOPMD_DEACTIVATE_KIOPM );
end;

//-------------------------------------------
function TGWIOPM_Driver.IOCTL_IOPMD_QUERY_KIOPM:  DWORD;
//-------------------------------------------
Var
  RetVal: DWORD;
Begin
  result := IOCTL_IOPMD_Misc1(RetVal, IOCMD_IOPMD_QUERY_KIOPM);
end;

//-------------------------------------------
function TGWIOPM_Driver.LIOPM_Set_Ports(BeginPort: word; EndPort: word; Enable: Boolean): DWORD;
//-------------------------------------------
Var
  PortNum: word;
  IOPM_Ix, IOPM_BitNum, Last_IOPM_Ix: integer;
  IOPM_Byte, Mask_Byte: byte;
  DriverResult: DWORD;
Label the_end;
Begin
  DriverResult := ERROR_SUCCESS;
  IOPM_Byte    := $FF;

  For PortNum := BeginPort to EndPort do
  Begin
    IOPM_Ix      := PortNum shr 3;  // 8 bits per byte;
    IOPM_BitNum  := PortNum and 7;  // lowest 3 bits;
    Mask_Byte     := 1 shl IOPM_BitNum;

    If (PortNum = BeginPort) or (IOPM_BitNum = 0) then
    Begin  // get IOPM byte
      DriverResult := IOCTL_IOPMD_GET_LIOPMB(IOPM_Ix, IOPM_Byte);
      if DriverResult <> ERROR_SUCCESS then goto the_end;
    end;

    If Enable then
    Begin   // set the bit to 0
      IOPM_Byte := IOPM_Byte and ($FF xor Mask_Byte);
    end else
    Begin   // set the bit to 1
      IOPM_Byte := IOPM_Byte or Mask_Byte;
    end;

    If (PortNum = EndPort) or (IOPM_BitNum = 7) then
    Begin    // Write out IOPM_Byte
      DriverResult := IOCTL_IOPMD_SET_LIOPM(IOPM_Ix, IOPM_Byte);
      if DriverResult <> ERROR_SUCCESS then goto the_end;
    end;
  end;
the_end:
  Result := DriverResult;
end;

//-------------------------------------------
initialization
//-------------------------------------------
  GWIOPM_Driver := TGWIOPM_Driver.Create;

//-------------------------------------------
finalization
//-------------------------------------------

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -