internal void CancelTransfer(int readHandle, int hidHandle, int writeHandle) { try { FileIOApiDeclarations.CancelIo(readHandle); if (hidHandle != 0) { FileIOApiDeclarations.CloseHandle(hidHandle); } if (readHandle != 0) { FileIOApiDeclarations.CloseHandle(readHandle); } if (writeHandle == 0) { return; } FileIOApiDeclarations.CloseHandle(writeHandle); } catch (Exception ex) { ProjectData.SetProjectError(ex); Hid.HandleException("Hid", ex); ProjectData.ClearProjectError(); } }
protected override bool ProtectedWrite(int writeHandle, byte[] outputReportBuffer) { bool flag1 = false; try { int lpNumberOfBytesWritten = 0; bool flag2 = FileIOApiDeclarations.WriteFile(writeHandle, ref outputReportBuffer[0], checked (Information.UBound((Array)outputReportBuffer, 1) + 1), ref lpNumberOfBytesWritten, 0); if (!flag2 && writeHandle != 0) { FileIOApiDeclarations.CloseHandle(writeHandle); } flag1 = flag2; } catch (Exception ex) { ProjectData.SetProjectError(ex); Hid.HandleException("Hid", ex); ProjectData.ClearProjectError(); } return(flag1); }
//TODO: Refatorar método public bool Conectado() { if (UpdatedAt == null || (DateTime.Now - UpdatedAt).Seconds > 2) { string[] devicePathName = new string[128]; try { Guid empty = Guid.Empty; MyDeviceDetected = false; FileIOApiDeclarations.SECURITY_ATTRIBUTES lpSecurityAttributes = new FileIOApiDeclarations.SECURITY_ATTRIBUTES(); lpSecurityAttributes.lpSecurityDescriptor = 0; lpSecurityAttributes.bInheritHandle = -1; lpSecurityAttributes.nLength = Strings.Len(lpSecurityAttributes); //short num1 = checked((short)Math.Round(Conversion.Val("&h" + vid))); //short num2 = checked((short)Math.Round(Conversion.Val("&h" + pid))); short num1 = checked ((short)Math.Round(Conversion.Val("&h0483"))); short num2 = checked ((short)Math.Round(Conversion.Val("&h0035"))); HidApiDeclarations.HidD_GetHidGuid(ref empty); empty.ToString(); if (MyDeviceManagement.FindDeviceFromGuid(empty, ref devicePathName)) { int index = 0; do { HIDHandle = FileIOApiDeclarations.CreateFile(devicePathName[index], 0, 3, ref lpSecurityAttributes, 3, 0, 0); if (HIDHandle != -1) { MyHID.DeviceAttributes.Size = Marshal.SizeOf((object)MyHID.DeviceAttributes); bool flag; if (HidApiDeclarations.HidD_GetAttributes(HIDHandle, ref MyHID.DeviceAttributes)) { if ((int)MyHID.DeviceAttributes.VendorId == (int)num1 & (int)MyHID.DeviceAttributes.ProductId == (int)num2) { MyDeviceDetected = true; MyDevicePathName = devicePathName[index]; NobreakInfo.StatusComunicacaoOk = true; } else { MyDeviceDetected = false; flag = FileIOApiDeclarations.CloseHandle(HIDHandle); } } else { MyDeviceDetected = false; flag = FileIOApiDeclarations.CloseHandle(HIDHandle); } } checked { ++index; } }while (!(MyDeviceDetected | index == checked (Information.UBound((Array)devicePathName, 1) + 1))); } lastStatusConectado = MyDeviceDetected; UpdatedAt = DateTime.Now; return(MyDeviceDetected); } catch (Exception ex) { Console.WriteLine("ERRO!!! " + ex.Message); //throw new NobreakNotConnectedException("Não foi possível conectar-se ao Nobreak"); } } return(lastStatusConectado); }