1,183
社区成员
发帖
与我相关
我的任务
分享
function TJvHidDevice.WriteFile(var Report; ToWrite: DWORD; var BytesWritten: DWORD): Boolean;
begin
Result := False;
if OpenFile then
Result := Windows.WriteFile(HidFileHandle, Report, ToWrite, BytesWritten, nil);
end;
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;
constructor TJvHidDevice.CtlCreate(const APnPInfo: TJvHidPnPInfo; const Controller: TJvHidDeviceController);
begin
inherited Create;
。。。
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;