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

📄 jvhidcontrollerclass.pas

📁 通过delphi USB控件读写数据
💻 PAS
📖 第 1 页 / 共 5 页
字号:
{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.

The Original Code is: JvHidControllerClass.PAS, released on 2001-02-28.

The Initial Developer of the Original Code is Robert Marquardt [robert_marquardt@gmx.de]
Portions created by Robert Marquardt are Copyright (C) 1999-2003 Robert Marquardt.
All Rights Reserved.

Contributor(s): Michael Beck [mbeck@bigfoot.com].

Last Modified: 2003-09-20

You may retrieve the latest version of this file at the Project JEDI's JVCL home page,
located at http://jvcl.sourceforge.net

Known Issues:
  I do not clean the source style for this file ;-)
-----------------------------------------------------------------------------}

{$I jvcl.inc}
{$I windowsonly.inc}

unit JvHidControllerClass;

interface

uses
  Windows, Messages, Classes, Forms, SysUtils,
  {$IFDEF USEJVCL}
  JvComponent,
  {$ENDIF USEJVCL}
  DBT, SetupApi, Hid, ModuleLoader;

const
  // a version string for the component
  cHidControllerClassVersion = '1.0.22';

  // strings from the registry for CheckOutByClass
  cHidKeyboardClass = 'Keyboard';
  cHidMouseClass    = 'Mouse';
  cHidNoClass       = 'HIDClass';

type
  // forward declarations
  TJvHidDeviceController = class;
  TJvHidDevice           = class;

  // the Event function declarations
  TJvHidEnumerateEvent  = function (HidDev: TJvHidDevice;
                                    const Idx: Integer): Boolean of object;
  TJvHidPlugEvent       = procedure(HidDev: TJvHidDevice) of object;
  TJvHidUnplugEvent     = TJvHidPlugEvent;
  TJvHidDataEvent       = procedure(HidDev: TJvHidDevice; ReportID: Byte;
                                    const Data: Pointer; Size: Word) of object;
  TJvHidDataErrorEvent  = procedure(HidDev: TJvHidDevice; Error: DWORD) of object;

  // check out test function
  TJvHidCheckCallback = function(HidDev: TJvHidDevice): Boolean; stdcall;

  // open overlapped read or write file handle
  TJvHidOpenExMode = (omhRead, omhWrite);

  // the physical descriptor
  TJvPhysicalDescriptor = array of WORD;

  // all USB relevant driver entries in the registry
  TJvHidPnPInfo = class(TObject)
    FDeviceID: DWORD;
    FDevicePath: string;
    FCapabilities: DWORD;
    FClassDescr: string;
    FClassGUID: string;
    FCompatibleIDs: TStringList;
    FConfigFlags: DWORD;
    FDeviceDescr: string;
    FDriver: string;
    FFriendlyName: string;
    FHardwareID: TStringList;
    FLowerFilters: TStringList;
    FMfg: string;
    FUpperFilters: TStringList;
    FAddress: string;
    FBusNumber: DWORD;
    FBusType: string;
    FCharacteristics: string;
    FDevType: DWORD;
    FEnumeratorName: string;
    FExclusive: DWORD;
    FLegacyBusType: DWORD;
    FLocationInfo: string;
    FPhysDevObjName: string;
    FSecuritySDS: string;
    FService: string;
    FUINumber: DWORD;
    FUINumberFormat: string;
    function GetRegistryPropertyString(PnPHandle: HDEVINFO; const DevData: TSPDevInfoData; Prop: DWORD): string;
    function GetRegistryPropertyStringList(PnPHandle: HDEVINFO; const DevData: TSPDevInfoData; Prop: DWORD): TStringList;
    function GetRegistryPropertyDWord(PnPHandle: HDEVINFO; const DevData: TSPDevInfoData; Prop: DWORD): DWORD;
  public
    property DeviceID:        DWORD       read FDeviceID;
    property DevicePath:      string      read FDevicePath;
    // registry values
    property Capabilities:    DWORD       read FCapabilities;
    property ClassDescr:      string      read FClassDescr;
    property ClassGUID:       string      read FClassGUID;
    property CompatibleIDs:   TStringList read FCompatibleIDs;
    property ConfigFlags:     DWORD       read FConfigFlags;
    property DeviceDescr:     string      read FDeviceDescr;
    property Driver:          string      read FDriver;
    property FriendlyName:    string      read FFriendlyName;
    property HardwareID:      TStringList read FHardwareID;
    property LowerFilters:    TStringList read FLowerFilters;
    property Mfg:             string      read FMfg;
    property UpperFilters:    TStringList read FUpperFilters;
    property Address:         string      read FAddress;
    property BusNumber:       DWORD       read FBusNumber;
    property BusType:         string      read FBusType;
    property Characteristics: string      read FCharacteristics;
    property DevType:         DWORD       read FDevType;
    property EnumeratorName:  string      read FEnumeratorName;
    property Exclusive:       DWORD       read FExclusive;
    property LegacyBusType:   DWORD       read FLegacyBusType;
    property LocationInfo:    string      read FLocationInfo;
    property PhysDevObjName:  string      read FPhysDevObjName;
    property SecuritySDS:     string      read FSecuritySDS;
    property Service:         string      read FService;
    property UINumber:        DWORD       read FUINumber;
    property UINumberFormat:  string      read FUINumberFormat;
    constructor Create(APnPHandle: HDEVINFO; ADevData: TSPDevInfoData; ADevicePath: PChar);
    destructor Destroy; override;
  end;

  // a thread helper class to implement TJvHidDevice.OnData

  TJvHidDeviceReadThread = class(TThread)
  private
    FErr: DWORD;
    procedure DoData;
    procedure DoDataError;
    constructor CtlCreate(const Dev: TJvHidDevice);
  public
    Device: TJvHidDevice;
    NumBytesRead: Cardinal;
    Report: array of Byte;
    procedure Execute; override;
    constructor Create(CreateSuspended: Boolean);
  end;

  // the representation of a HID device

  TJvHidDevice = class(TObject)
  private
    // internal control variables
    FMyController:         TJvHidDeviceController;
    FIsPluggedIn:          Boolean;
    FIsCheckedOut:         Boolean;
    FIsEnumerated:         Boolean;
    FHidFileHandle:        THandle;
    FHidOverlappedRead:    THandle;
    FHidOverlappedWrite:   THandle;
    FOvlRead:              TOverlapped;
    FOvlWrite:             TOverlapped;
    // internal properties part
    FAttributes:           THIDDAttributes;
    FPnPInfo:              TJvHidPnPInfo;
    FVendorName:           WideString;
    FProductName:          WideString;
    FPhysicalDescriptor:   TJvPhysicalDescriptor;
    FPreparsedData:        PHIDPPreparsedData;
    FSerialNumber:         WideString;
    FLanguageStrings:      TStringList;
    FNumInputBuffers:      Integer;
    FNumOverlappedBuffers: Integer;
    FThreadSleepTime:      Integer;
    FLinkCollection:       array of THIDPLinkCollectionNode;
    FMaxDataListLength:    ULONG;
    FMaxUsageListLength:   ULONG;
    FMaxButtonListLength:  ULONG;
    FReportTypeParam:      THIDPReportType;
    FUsagePageParam:       TUsage;
    FLinkCollectionParam:  WORD;
    FUsageParam:           TUsage;
    FData:                 TJvHidDataEvent;
    FDataError:            TJvHidDataErrorEvent;
    FUnplug:               TJvHidUnplugEvent;
    FHasReadWriteAccess:   Boolean;
    FDataThread:           TJvHidDeviceReadThread;
    FTag:                  Integer;

    // tells if access to device is allowed
    function  IsAccessible: Boolean;
    procedure GetMax;

    // internal property implementors
    function  GetDeviceStringAnsi   (Idx: Byte): string;
    function  GetDeviceStringUnicode(Idx: Byte): WideString;
    function  GetLinkCollectionNode (Idx: WORD): THIDPLinkCollectionNode;
    function  GetConfiguration:         THIDDConfiguration;
    function  GetPreparsedData:         PHIDPPreparsedData;
    function  GetCaps:                  THIDPCaps;
    function  GetVendorName:            WideString;
    function  GetProductName:           WideString;
    function  GetSerialNumber:          WideString;
    function  GetPhysicalDescriptor:    TJvPhysicalDescriptor;
    function  GetLanguageStrings:       TStringList;
    function  GetOverlappedReadResult:  DWORD;
    function  GetOverlappedWriteResult: DWORD;
    procedure SetConfiguration       (const Config: THIDDConfiguration);
    procedure SetDataEvent           (const DataEvent: TJvHidDataEvent);
    procedure SetNumInputBuffers     (const Num: Integer);
    procedure SetNumOverlappedBuffers(const Num: Integer);
    procedure SetReportTypeParam     (const ReportType: THIDPReportType);
    procedure SetThreadSleepTime     (const SleepTime: Integer);
    procedure SetUsagePageParam      (const UsagePage: TUsage);
    procedure StartThread;
    procedure StopThread;

    // Constructor is hidden! Only a TJvHidDeviceController can create a TJvHidDevice object.
    constructor CtlCreate(const APnPInfo: TJvHidPnPInfo;
                          const Controller: TJvHidDeviceController);

  protected
    // internal event implementors
    procedure DoUnplug;

  public
    // dummy constructor
    constructor Create;
    destructor Destroy; override;

    // read only properties
    property Attributes:               THIDDAttributes       read FAttributes;
    property Caps:                     THIDPCaps             read GetCaps;
    property HasReadWriteAccess:       Boolean               read FHasReadWriteAccess;
    property HidFileHandle:            THandle               read FHidFileHandle;
    property HidOverlappedRead:        THandle               read FHidOverlappedRead;
    property HidOverlappedWrite:       THandle               read FHidOverlappedWrite;
    property HidOverlappedReadResult:  DWORD                 read GetOverlappedReadResult;
    property HidOverlappedWriteResult: DWORD                 read GetOverlappedWriteResult;
    property IsCheckedOut:             Boolean               read FIsCheckedOut;
    property IsPluggedIn:              Boolean               read FIsPluggedIn;
    property LanguageStrings:          TStringList           read GetLanguageStrings;
    property MaxButtonListLength:      ULONG                 read FMaxButtonListLength;
    property MaxDataListLength:        ULONG                 read FMaxDataListLength;
    property MaxUsageListLength:       ULONG                 read FMaxUsageListLength;
    property PhysicalDescriptor:       TJvPhysicalDescriptor read GetPhysicalDescriptor;
    property PnPInfo:                  TJvHidPnPInfo         read FPnPInfo;
    property PreparsedData:            PHIDPPreparsedData    read GetPreparsedData;
    property ProductName:              WideString            read GetProductName;
    property SerialNumber:             WideString            read GetSerialNumber;
    property VendorName:               WideString            read GetVendorName;
    // read write properties
    property Configuration:        THIDDConfiguration read GetConfiguration      write SetConfiguration;
    property LinkCollectionParam:  WORD               read FLinkCollectionParam  write FLinkCollectionParam;
    property NumInputBuffers:      Integer            read FNumInputBuffers      write SetNumInputBuffers;
    property NumOverlappedBuffers: Integer            read FNumOverlappedBuffers write SetNumOverlappedBuffers;
    property ReportTypeParam:      THIDPReportType    read FReportTypeParam      write SetReportTypeParam;
    property Tag:                  Integer            read FTag                  write FTag;
    property ThreadSleepTime:      Integer            read FThreadSleepTime      write SetThreadSleepTime;
    property UsagePageParam:       TUsage             read FUsagePageParam       write SetUsagePageParam;
    property UsageParam:           TUsage             read FUsageParam           write FUsageParam;
    // indexed properties
    property DeviceStrings       [Idx: Byte]: string                  read GetDeviceStringAnsi;
    property DeviceStringsUnicode[Idx: Byte]: WideString              read GetDeviceStringUnicode;
    property LinkCollectionNodes [Idx: WORD]: THIDPLinkCollectionNode read GetLinkCollectionNode;
    // event properties
    property OnData:               TJvHidDataEvent      read FData      write SetDataEvent;
    property OnDataError:          TJvHidDataErrorEvent read FDataError write FDataError;
    property OnUnplug:             TJvHidUnplugEvent    read FUnplug    write FUnplug;

    // methods
    function  CancelIO             (const Mode: TJvHidOpenExMode): Boolean;
    procedure CloseFile;
    procedure CloseFileEx          (const Mode: TJvHidOpenExMode);
    function  DeviceIoControl      (IoControlCode: DWORD; InBuffer: Pointer; InSize: DWORD;
                                    OutBuffer: Pointer; OutSize: DWORD;
                                    var BytesReturned: DWORD):                      Boolean;
    function  FlushQueue:                                                           Boolean;
    function  GetButtonCaps        (ButtonCaps: PHIDPButtonCaps; var Count: WORD):  NTSTATUS;
    function  GetButtons           (UsageList: PUsage; var UsageLength: ULONG;
                                    var Report; ReportLength: ULONG):               NTSTATUS;
    function  GetButtonsEx         (UsageList: PUsageAndPage; var UsageLength: ULONG;
                                    var Report; ReportLength: ULONG):               NTSTATUS;
    function  GetData              (DataList: PHIDPData; var DataLength: ULONG;
                                    var Report; ReportLength: ULONG):               NTSTATUS;
    function  GetFeature           (var Report; const Size: Integer):               Boolean;
    function  GetScaledUsageValue  (var UsageValue: Integer;
                                    var Report; ReportLength: ULONG):               NTSTATUS;
    function  GetSpecificButtonCaps(ButtonCaps: PHIDPButtonCaps; var Count: WORD):  NTSTATUS;
    function  GetSpecificValueCaps (ValueCaps:  PHIDPValueCaps;  var Count: WORD):  NTSTATUS;
    function  GetUsages            (UsageList: PUsage; var UsageLength: ULONG;
                                    var Report; ReportLength: ULONG):               NTSTATUS;
    function  GetUsagesEx          (UsageList: PUsageAndPage; var UsageLength: ULONG;
                                    var Report; ReportLength: ULONG):               NTSTATUS;
    function  GetUsageValue        (var UsageValue: ULONG;
                                    var Report; ReportLength: ULONG):               NTSTATUS;
    function  GetUsageValueArray   (UsageValue: PChar; UsageValueByteLength: WORD;
                                    var Report; ReportLength: ULONG):               NTSTATUS;
    function  GetValueCaps         (ValueCaps:  PHIDPValueCaps;  var Count: WORD):  NTSTATUS;
    function  OpenFile:                                                             Boolean;
    function  OpenFileEx           (Mode: TJvHidOpenExMode):                        Boolean;
    function  SetButtons           (UsageList: PUsage; var UsageLength: ULONG;
                                    var Report; ReportLength: ULONG):               NTSTATUS;
    function  SetData              (DataList: PHIDPData; var DataLength: ULONG;
                                    var Report; ReportLength: ULONG):               NTSTATUS;
    function  SetFeature           (var Report; const Size: Integer):               Boolean;
    function  SetScaledUsageValue  (UsageValue: Integer;
                                    var Report; ReportLength: ULONG):               NTSTATUS;
    function  SetUsages            (UsageList: PUsage; var UsageLength: ULONG;
                                    var Report; ReportLength: ULONG):               NTSTATUS;
    function  SetUsageValue        (UsageValue: ULONG;
                                    var Report; ReportLength: ULONG):               NTSTATUS;
    function  SetUsageValueArray   (UsageValue: PChar; UsageValueByteLength: WORD;
                                    var Report; ReportLength: ULONG):               NTSTATUS;
    function  UnsetButtons         (UsageList: PUsage; var UsageLength: ULONG;
                                    var Report; ReportLength: ULONG):               NTSTATUS;
    function  UnsetUsages          (UsageList: PUsage; var UsageLength: ULONG;

⌨️ 快捷键说明

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