Beispiel #1
0
      /// <summary>
      /// Creates an event object for the overlapped structure used with ReadFile.
      /// Called before the first call to ReadFile.
      /// </summary>
      /// <param name="hidOverlapped"></param>
      /// <param name="eventObject"></param>
      internal void PrepareForOverlappedTransfer(ref FileIOApiDeclarations.OVERLAPPED hidOverlapped, ref int eventObject)
      {
        FileIOApiDeclarations.SECURITY_ATTRIBUTES Security = new FileIOApiDeclarations.SECURITY_ATTRIBUTES();

        try
        {
          // Values for the SECURITY_ATTRIBUTES structure:
          Security.lpSecurityDescriptor = 0;
          Security.bInheritHandle = Convert.ToInt32(true);
          Security.nLength = Marshal.SizeOf(Security);

          // ***
          // API function: CreateEvent
          // Purpose: Creates an event object for the overlapped structure used with ReadFile.
          // Accepts:
          // A security attributes structure.
          // Manual Reset = False (The system automatically resets the state to nonsignaled
          // after a waiting thread has been released.)
          // Initial state = True (signaled)
          // An event object name (optional)
          // Returns: a handle to the event object
          // ***
          eventObject =
            FileIOApiDeclarations.CreateEvent(ref Security, Convert.ToInt32(false), Convert.ToInt32(true), "");
          if (Settings.Instance.ExtensiveLogging)
          {
            Log.Debug(Debugging.ResultOfAPICall("CreateEvent"));
            Log.Debug("");
          }
          // Set the members of the overlapped structure.
          hidOverlapped.Offset = 0;
          hidOverlapped.OffsetHigh = 0;
          hidOverlapped.hEvent = eventObject;
          ReadyForOverlappedTransfer = true;
        }
        catch (Exception ex)
        {
          HandleException(ModuleName + ":" + MethodBase.GetCurrentMethod(), ex);
        }
      }
Beispiel #2
0
    /// <summary>
    /// Uses a series of API calls to locate a HID-class device
    /// by its Vendor ID and Product ID.
    /// </summary>
    /// <returns>True if the device is detected, False if not detected.</returns>
    private bool FindTheHid()
    {
      string[] DevicePathName = new string[128];
      FileIOApiDeclarations.SECURITY_ATTRIBUTES Security = new FileIOApiDeclarations.SECURITY_ATTRIBUTES();

      try
      {
        if (Settings.Instance.ExtensiveLogging)
        {
          Log.Debug("MiniDisplay.VFD_Control: Searching for display with VendorID:{0:X} & ProductID:{1:X}", _VendorID,
                    _ProductID);
        }

        Guid HidGuid = Guid.Empty;
        _MyDeviceDetected = false;

        // Values for the SECURITY_ATTRIBUTES structure:
        Security.lpSecurityDescriptor = 0;
        Security.bInheritHandle = Convert.ToInt32(true);
        Security.nLength = Marshal.SizeOf(Security);


        /*
                  API function: 'HidD_GetHidGuid
                  Purpose: Retrieves the interface class GUID for the HID class.
                  Accepts: 'A System.Guid object for storing the GUID.
                  */

        HidApiDeclarations.HidD_GetHidGuid(ref HidGuid);
        if (Settings.Instance.ExtensiveLogging)
        {
          Log.Debug("MiniDisplay.VFD_Control: " + Debugging.ResultOfAPICall("GetHidGuid"));
        }

        // Display the GUID.
        string GUIDString = HidGuid.ToString();
        if (Settings.Instance.ExtensiveLogging)
        {
          Log.Debug("MiniDisplay.VFD_Control: GUID for system HIDs: " + GUIDString);
        }

        // Fill an array with the device path names of all attached HIDs.
        bool DeviceFound = DeviceManagement.FindDeviceFromGuid(HidGuid, ref DevicePathName);

        // If there is at least one HID, attempt to read the Vendor ID and Product ID
        // of each device until there is a match or all devices have been examined.

        if (DeviceFound)
        {
          int MemberIndex = 0;
          do
          {
            // ***
            // API function:
            // CreateFile
            // Purpose:
            // Retrieves a handle to a device.
            // Accepts:
            // A device path name returned by SetupDiGetDeviceInterfaceDetail
            // The type of access requested (read/write).
            // FILE_SHARE attributes to allow other processes to access the device while this handle is open.
            // A Security structure. Using Null for this may cause problems under Windows XP.
            // A creation disposition value. Use OPEN_EXISTING for devices.
            // Flags and attributes for files. Not used for devices.
            // Handle to a template file. Not used.
            // Returns: a handle that enables reading and writing to the device.
            // ***

            _HIDHandle = FileIOApiDeclarations.CreateFile
              (DevicePathName[MemberIndex],
               FileIOApiDeclarations.GENERIC_READ | FileIOApiDeclarations.GENERIC_WRITE,
               FileIOApiDeclarations.FILE_SHARE_READ | FileIOApiDeclarations.FILE_SHARE_WRITE,
               ref Security,
               FileIOApiDeclarations.OPEN_EXISTING, 0, 0);

            if (Settings.Instance.ExtensiveLogging)
            {
              Log.Debug("MiniDisplay.VFD_Control: " + Debugging.ResultOfAPICall("CreateFile"));
            }
            if (Settings.Instance.ExtensiveLogging)
            {
              Log.Debug("MiniDisplay.VFD_Control: Returned handle: " + _HIDHandle.ToString("x") + "h");
            }

            if (_HIDHandle != FileIOApiDeclarations.INVALID_HANDLE_VALUE)
            {
              // The returned handle is valid,
              // so find out if this is the device we're looking for.

              // Set the Size property of DeviceAttributes to the number of bytes in the structure.
              _MyHID.DeviceAttributes.Size = Marshal.SizeOf(_MyHID.DeviceAttributes);

              // ***
              // API function:
              // HidD_GetAttributes
              // Purpose:
              // Retrieves a HIDD_ATTRIBUTES structure containing the Vendor ID,
              // Product ID, and Product Version Number for a device.
              // Accepts:
              // A handle returned by CreateFile.
              // A pointer to receive a HIDD_ATTRIBUTES structure.
              // Returns:
              // True on success, False on failure.
              // ***

              int Result = HidApiDeclarations.HidD_GetAttributes(_HIDHandle, ref _MyHID.DeviceAttributes);


              if (Settings.Instance.ExtensiveLogging)
              {
                Log.Debug("MiniDisplay.VFD_Control: " + Debugging.ResultOfAPICall("HidD_GetAttributes"));
              }

              if (Result != 0)
              {
                if (Settings.Instance.ExtensiveLogging)
                {
                  Log.Debug("MiniDisplay.VFD_Control: HIDD_ATTRIBUTES structure filled without error.");
                }

                if (Settings.Instance.ExtensiveLogging)
                {
                  Log.Debug(
                    "MiniDisplay.VFD_Control: Vendor ID: {0:X}, Product ID: {1:X}, Version {2:X}",
                    _MyHID.DeviceAttributes.VendorID, _MyHID.DeviceAttributes.ProductID,
                    _MyHID.DeviceAttributes.VersionNumber);
                }

                // Find out if the device matches the one we're looking for.
                if ((_MyHID.DeviceAttributes.VendorID == _VendorID) &
                    (_MyHID.DeviceAttributes.ProductID == _ProductID))
                {
                  // It's the desired device.
                  if (Settings.Instance.ExtensiveLogging)
                  {
                    Log.Debug("MiniDisplay.VFD_Control: My device detected");
                  }

                  _MyDeviceDetected = true;

                  // Save the DevicePathName so OnDeviceChange() knows which name is my device.
                }
                else
                {
                  // It's not a match, so close the handle.
                  _MyDeviceDetected = false;

                  FileIOApiDeclarations.CloseHandle(_HIDHandle);

                  if (Settings.Instance.ExtensiveLogging)
                  {
                    Log.Debug("MiniDisplay.VFD_Control: " + Debugging.ResultOfAPICall("CloseHandle"));
                  }
                }
              }
              else
              {
                // There was a problem in retrieving the information.
                if (Settings.Instance.ExtensiveLogging)
                {
                  Log.Debug("MiniDisplay.VFD_Control: Error in filling HIDD_ATTRIBUTES structure.");
                }
                _MyDeviceDetected = false;
                FileIOApiDeclarations.CloseHandle(_HIDHandle);
              }
            }

            // Keep looking until we find the device or there are no more left to examine.
            MemberIndex = MemberIndex + 1;
          } while (!(_MyDeviceDetected || (MemberIndex == DevicePathName.Length) || DevicePathName[MemberIndex] == null));
        }

        if (_MyDeviceDetected)
        {
          // The device was detected.
          // Learn the capabilities of the device.
          _MyHID.Capabilities = _MyHID.GetDeviceCapabilities(_HIDHandle);
        }
      }
      catch (Exception ex)
      {
        Log.Error(ex);
        //HandleException(this.Name + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
      }
      return _MyDeviceDetected;
    }