constructor TJvHidDevice.CtlCreate(
const APnPInfo: TJvHidPnPInfo;
const Controller: TJvHidDeviceController);
var
LDevicePath:
string;
// für Workaround
begin
inherited Create;
// initialize private data
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;
FPollingDelayTime := Controller.DevPollingDelayTime;
FThreadSleepTime := Controller.DevThreadSleepTime;
FUsagePageParam := 0;
FLinkCollectionParam := 0;
FUsageParam := 0;
FDataThread :=
nil;
OnData := Controller.OnDeviceData;
OnUnplug := Controller.OnDeviceUnplug;
// https://stackoverflow.com/questions/53761417/createfile-over-usb-hid-device-fails-with-access-denied-5-since-windows-10-180
// Beginn des Workarounds
if APnPInfo.DevicePath.EndsWith('
\kbd')
then
begin
LDevicePath := APnPInfo.DevicePath.Remove(APnPInfo.DevicePath.IndexOf('
\kbd'));
end else
LDevicePath := APnPInfo.DevicePath;
// Ende des Workarounds
FHidFileHandle := CreateFile(PChar(LDevicePath), 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(LDevicePath), 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.CreateRes(@RsEDeviceCannotBeIdentified);
end
else
raise EControllerError.CreateRes(@RsEDeviceCannotBeOpened);
FPnPInfo := APnPInfo;
// the file is closed to stop using up resources
CloseFile;
end;