📄 jvhidcontrollerclass.pas
字号:
BytesReturned: DWORD;
RegDataType: DWORD;
Buffer: array[0..16383] of Char;
P: PChar;
begin
BytesReturned := 0;
RegDataType := 0;
Buffer[0] := #0;
SetupDiGetDeviceRegistryProperty(PnPHandle, DevData, Prop,
RegDataType, PBYTE(@Buffer[0]), SizeOf(Buffer), BytesReturned);
Result := TStringList.Create;
P := @Buffer[0];
while P[0] <> #0 do
begin
Result.Add(P);
P := P + StrLen(P) + 1;
end;
end;
//------------------------------------------------------------------------------
function TJvHidPnPInfo.GetRegistryPropertyDWord(PnPHandle: HDEVINFO; const DevData: TSPDevInfoData; Prop: DWORD): DWORD;
var
BytesReturned: DWORD;
RegDataType: DWORD;
begin
BytesReturned := 0;
RegDataType := 0;
Result := 0;
SetupDiGetDeviceRegistryProperty(PnPHandle, DevData, Prop,
RegDataType, PBYTE(@Result), SizeOf(Result), BytesReturned);
end;
//== TJvHidDevice ==============================================================
// dummy constructor to catch invalid Creates
constructor TJvHidDevice.Create;
begin
FHidFileHandle := INVALID_HANDLE_VALUE;
FHidOverlappedRead := INVALID_HANDLE_VALUE;
FHidOverlappedWrite := INVALID_HANDLE_VALUE;
raise EControllerError.Create(RsEDirectHidDeviceCreationNotAllowed);
end;
//------------------------------------------------------------------------------
// create and fill in a HidDevice object
// the constructor is only accessible from TJvHidController
// PnPInfo contains all info the JvHidDeviceController collected
// Controller is the devices controller object to be referenced
// internally
constructor TJvHidDevice.CtlCreate(const APnPInfo: TJvHidPnPInfo; const Controller: TJvHidDeviceController);
begin
inherited Create;
// initialize private data
FPnPInfo := APnPInfo;
FMyController := Controller;
FIsPluggedIn := True;
FIsCheckedOut := False;
FIsEnumerated := False;
FHidOverlappedRead := INVALID_HANDLE_VALUE;
FHidOverlappedWrite := INVALID_HANDLE_VALUE;
FVendorName := '';
FProductName := '';
FPreparsedData := nil;
SetLength(FPhysicalDescriptor, 0);
FSerialNumber := '';
FLanguageStrings := TStringList.Create;
FNumInputBuffers := 0;
FNumOverlappedBuffers := 0;
SetLength(FLinkCollection, 0);
FMaxDataListLength := 0;
FMaxUsageListLength := 0;
FMaxButtonListLength := 0;
FReportTypeParam := HIDP_Input;
FThreadSleepTime := 100;
FUsagePageParam := 0;
FLinkCollectionParam := 0;
FUsageParam := 0;
FDataThread := nil;
OnData := Controller.OnDeviceData;
OnUnplug := Controller.OnDeviceUnplug;
FHidFileHandle := CreateFile(PChar(PnPInfo.DevicePath), GENERIC_READ or GENERIC_WRITE,
FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);
FHasReadWriteAccess := HidFileHandle <> INVALID_HANDLE_VALUE;
// Win2000 hack
if not HasReadWriteAccess then
FHidFileHandle := CreateFile(PChar(PnPInfo.DevicePath), 0,
FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);
if HidFileHandle <> INVALID_HANDLE_VALUE then
begin
FAttributes.Size := SizeOf(THIDDAttributes);
if not HidD_GetAttributes(HidFileHandle, FAttributes) then
raise EControllerError.Create(RsEDeviceCannotBeIdentified);
end
else
raise EControllerError.Create(RsEDeviceCannotBeOpened);
// the file is closed to stop using up resources
CloseFile;
end;
//------------------------------------------------------------------------------
// If a TJvHidDevice is destroyed the TJvHidController has to be informed.
// If the device is plugged in this TJvHidDevice instance is destroyed,
// but another instance is created in the controller list
destructor TJvHidDevice.Destroy;
var
I: Integer;
TmpOnData: TJvHidDataEvent;
TmpOnUnplug: TJvHidUnplugEvent;
Dev: TJvHidDevice;
begin
// if we need to clone the object
TmpOnData := OnData;
TmpOnUnplug := OnUnplug;
// to prevent strange problems
OnData := nil;
OnUnplug := nil;
// free the data which needs special handling
CloseFile;
CloseFileEx(omhRead);
CloseFileEx(omhWrite);
if FPreparsedData <> nil then
HidD_FreePreparsedData(FPreparsedData);
FLanguageStrings.Free;
// if controller exists
if FMyController <> nil then
with FMyController do
begin
// delete device from controller list
for I := 0 to FList.Count - 1 do
if FList.Items[I] = Self then
begin
// if device is plugged in create a checked in copy
if IsPluggedIn then
begin
Dev := TJvHidDevice.CtlCreate(FPnPInfo, FMyController);
// make it a complete clone
Dev.OnData := TmpOnData;
Dev.OnUnplug := TmpOnUnplug;
Dev.ThreadSleepTime := ThreadSleepTime;
FList.Items[I] := Dev;
// the FPnPInfo has been handed over to the new object
FPnPInfo := nil;
if IsCheckedOut then
begin
Dec(FNumCheckedOutDevices);
Inc(FNumCheckedInDevices);
end;
end
else
begin
FList.Delete(I);
Dec(FNumUnpluggedDevices);
end;
Break;
end;
end;
PnPInfo.Free;
inherited Destroy;
end;
//------------------------------------------------------------------------------
// if check changes change check only here
function TJvHidDevice.IsAccessible: Boolean;
begin
Result := IsPluggedIn and (IsCheckedOut or FIsEnumerated);
end;
//------------------------------------------------------------------------------
// open the device "file" (for the other methods)
function TJvHidDevice.OpenFile: Boolean;
begin
// check if open allowed (propagates this state)
if IsAccessible then
if HidFileHandle = INVALID_HANDLE_VALUE then // if not already opened
begin
FHidFileHandle := CreateFile(PChar(PnPInfo.DevicePath), GENERIC_READ or GENERIC_WRITE,
FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);
FHasReadWriteAccess := HidFileHandle <> INVALID_HANDLE_VALUE;
// Win2000 hack
if not HasReadWriteAccess then
FHidFileHandle := CreateFile(PChar(PnPInfo.DevicePath), 0,
FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);
if HidFileHandle <> INVALID_HANDLE_VALUE then
begin
if NumInputBuffers <> 0 then
HidD_SetNumInputBuffers(HidFileHandle, NumInputBuffers);
HidD_GetNumInputBuffers(HidFileHandle, FNumInputBuffers);
end;
end;
Result := HidFileHandle <> INVALID_HANDLE_VALUE;
end;
//------------------------------------------------------------------------------
// open second device "file" for ReadFileEx and WriteFileEx
function TJvHidDevice.OpenFileEx(Mode: TJvHidOpenExMode): Boolean;
begin
Result := False;
// check if open allowed (propagates this state)
if IsAccessible then
if Mode = omhRead then
begin
if HidOverlappedRead = INVALID_HANDLE_VALUE then // if not already opened
begin
FHidOverlappedRead := CreateFile(PChar(PnPInfo.DevicePath), GENERIC_READ,
FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
if FHidOverlappedRead <> INVALID_HANDLE_VALUE then
begin
if NumOverlappedBuffers <> 0 then
HidD_SetNumInputBuffers(FHidOverlappedRead, NumOverlappedBuffers);
HidD_GetNumInputBuffers(FHidOverlappedRead, FNumOverlappedBuffers);
end;
end;
Result := FHidOverlappedRead <> INVALID_HANDLE_VALUE;
end
else
begin
if HidOverlappedWrite = INVALID_HANDLE_VALUE then // if not already opened
FHidOverlappedWrite := CreateFile(PChar(PnPInfo.DevicePath), GENERIC_WRITE,
FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
Result := FHidOverlappedWrite <> INVALID_HANDLE_VALUE;
end;
end;
//------------------------------------------------------------------------------
// implement OnUnplug event
procedure TJvHidDevice.DoUnplug;
begin
CloseFile;
CloseFileEx(omhRead);
CloseFileEx(omhWrite);
FIsPluggedIn := False;
// event even for checked in devices
if Assigned(FUnplug) then
FUnplug(Self);
// guarantees that event is only called once
OnUnplug := nil;
end;
//------------------------------------------------------------------------------
// implementing indexed properties read
function TJvHidDevice.GetDeviceStringAnsi(Idx: Byte): string;
begin
Result := WideCharToString(PWideChar(GetDeviceStringUnicode(Idx)));
end;
//------------------------------------------------------------------------------
function TJvHidDevice.GetDeviceStringUnicode(Idx: Byte): WideString;
var
Buffer: array [0..253] of WideChar;
begin
Result := '';
if Idx <> 0 then
if OpenFile then
if HidD_GetIndexedString(HidFileHandle, Idx, Buffer, SizeOf(Buffer)) then
Result := Buffer;
end;
//------------------------------------------------------------------------------
function TJvHidDevice.GetLinkCollectionNode(Idx: WORD): THIDPLinkCollectionNode;
var
Siz: ULONG;
begin
if Length(FLinkCollection) = 0 then
begin
Siz := Caps.NumberLinkCollectionNodes;
SetLength(FLinkCollection, Siz);
HidP_GetLinkCollectionNodes(@FLinkCollection[0], Siz, PreparsedData);
end;
FillChar(Result, SizeOf(THIDPLinkCollectionNode), #0);
if Idx < Length(FLinkCollection) then
Result := FLinkCollection[Idx];
end;
//------------------------------------------------------------------------------
// implementing properties write
procedure TJvHidDevice.SetNumInputBuffers(const Num: Integer);
begin
if (Num <> FNumInputBuffers) and OpenFile then
begin
HidD_SetNumInputBuffers(HidFileHandle, Num);
HidD_GetNumInputBuffers(HidFileHandle, FNumInputBuffers);
end;
end;
//------------------------------------------------------------------------------
procedure TJvHidDevice.SetNumOverlappedBuffers(const Num: Integer);
begin
if (Num <> FNumOverlappedBuffers) and OpenFileEx(omhRead) then
begin
HidD_SetNumInputBuffers(HidOverlappedRead, Num);
HidD_GetNumInputBuffers(HidOverlappedRead, FNumOverlappedBuffers);
end;
end;
//------------------------------------------------------------------------------
// internal helper for the following functions
procedure TJvHidDevice.GetMax;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -