sl@0: /// sl@0: /// Project: GenericHid sl@0: /// sl@0: /// *********************************************************************** sl@0: /// Software License Agreement sl@0: /// sl@0: /// Licensor grants any person obtaining a copy of this software ("You") sl@0: /// a worldwide, royalty-free, non-exclusive license, for the duration of sl@0: /// the copyright, free of charge, to store and execute the Software in a sl@0: /// computer system and to incorporate the Software or any portion of it sl@0: /// in computer programs You write. sl@0: /// sl@0: /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR sl@0: /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, sl@0: /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE sl@0: /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER sl@0: /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, sl@0: /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN sl@0: /// THE SOFTWARE. sl@0: /// *********************************************************************** sl@0: /// sl@0: /// Author sl@0: /// Jan Axelson sl@0: /// sl@0: /// This software was written using Visual Studio Express 2012 for Windows sl@0: /// Desktop building for the .NET Framework v4.5. sl@0: /// sl@0: /// Purpose: sl@0: /// Demonstrates USB communications with a generic HID-class device sl@0: /// sl@0: /// Requirements: sl@0: /// Windows Vista or later and an attached USB generic Human Interface Device (HID). sl@0: /// (Does not run on Windows XP or earlier because .NET Framework 4.5 will not install on these OSes.) sl@0: /// sl@0: /// Description: sl@0: /// Finds an attached device that matches the vendor and product IDs in the form's sl@0: /// text boxes. sl@0: /// sl@0: /// Retrieves the device's capabilities. sl@0: /// Sends and requests HID reports. sl@0: /// sl@0: /// Uses the System.Management class and Windows Management Instrumentation (WMI) to detect sl@0: /// when a device is attached or removed. sl@0: /// sl@0: /// A list box displays the data sent and received along with error and status messages. sl@0: /// You can select data to send and 1-time or periodic transfers. sl@0: /// sl@0: /// You can change the size of the host's Input report buffer and request to use control sl@0: /// transfers only to exchange Input and Output reports. sl@0: /// sl@0: /// To view additional debugging messages, in the Visual Studio development environment, sl@0: /// from the main menu, select Build > Configuration Manager > Active Solution Configuration sl@0: /// and select Configuration > Debug and from the main menu, select View > Output. sl@0: /// sl@0: /// The application uses asynchronous FileStreams to read Input reports and write Output sl@0: /// reports so the application's main thread doesn't have to wait for the device to retrieve a sl@0: /// report when the HID driver's buffer is empty or send a report when the device's endpoint is busy. sl@0: /// sl@0: /// For code that finds a device and opens handles to it, see the FindTheHid routine in frmMain.cs. sl@0: /// For code that reads from the device, see GetInputReportViaInterruptTransfer, sl@0: /// GetInputReportViaControlTransfer, and GetFeatureReport in Hid.cs. sl@0: /// For code that writes to the device, see SendInputReportViaInterruptTransfer, sl@0: /// SendInputReportViaControlTransfer, and SendFeatureReport in Hid.cs. sl@0: /// sl@0: /// This project includes the following modules: sl@0: /// sl@0: /// GenericHid.cs - runs the application. sl@0: /// FrmMain.cs - routines specific to the form. sl@0: /// Hid.cs - routines specific to HID communications. sl@0: /// DeviceManagement.cs - routine for obtaining a handle to a device from its GUID. sl@0: /// Debugging.cs - contains a routine for displaying API error messages. sl@0: /// HidDeclarations.cs - Declarations for API functions used by Hid.cs. sl@0: /// FileIODeclarations.cs - Declarations for file-related API functions. sl@0: /// DeviceManagementDeclarations.cs - Declarations for API functions used by DeviceManagement.cs. sl@0: /// DebuggingDeclarations.cs - Declarations for API functions used by Debugging.cs. sl@0: /// sl@0: /// Companion device firmware for several device CPUs is available from www.Lvr.com/hidpage.htm sl@0: /// You can use any generic HID (not a system mouse or keyboard) that sends and receives reports. sl@0: /// This application will not detect or communicate with non-HID-class devices. sl@0: /// sl@0: /// For more information about HIDs and USB, and additional example device firmware to use sl@0: /// with this application, visit Lakeview Research at http://Lvr.com sl@0: /// Send comments, bug reports, etc. to jan@Lvr.com or post on my PORTS forum: http://www.lvr.com/forum sl@0: /// sl@0: /// V6.2 sl@0: /// 11/12/13 sl@0: /// Disabled form buttons when a transfer is in progress. sl@0: /// Other minor edits for clarity and readability. sl@0: /// Will NOT run on Windows XP or earlier, see below. sl@0: /// sl@0: /// V6.1 sl@0: /// 10/28/13 sl@0: /// Uses the .NET System.Management class to detect device arrival and removal with WMI instead of Win32 RegisterDeviceNotification. sl@0: /// Other minor edits. sl@0: /// Will NOT run on Windows XP or earlier, see below. sl@0: /// sl@0: /// V6.0 sl@0: /// 2/8/13 sl@0: /// This version will NOT run on Windows XP or earlier because the code uses .NET Framework 4.5 to support asynchronous FileStreams. sl@0: /// The .NET Framework 4.5 redistributable is compatible with Windows 8, Windows 7 SP1, Windows Server 2008 R2 SP1, sl@0: /// Windows Server 2008 SP2, Windows Vista SP2, and Windows Vista SP3. sl@0: /// For compatibility, replaced ToInt32 with ToInt64 here: sl@0: /// IntPtr pDevicePathName = new IntPtr(detailDataBuffer.ToInt64() + 4); sl@0: /// and here: sl@0: /// if ((deviceNotificationHandle.ToInt64() == IntPtr.Zero.ToInt64())) sl@0: /// For compatibility if the charset isn't English, added System.Globalization.CultureInfo.InvariantCulture here: sl@0: /// if ((String.Compare(DeviceNameString, mydevicePathName, true, System.Globalization.CultureInfo.InvariantCulture) == 0)) sl@0: /// Replaced all Microsoft.VisualBasic namespace code with other .NET equivalents. sl@0: /// Revised user interface for more flexibility. sl@0: /// Moved interrupt-transfer and other HID-specific code to Hid.cs. sl@0: /// Used JetBrains ReSharper to clean up the code: http://www.jetbrains.com/resharper/ sl@0: /// sl@0: /// V5.0 sl@0: /// 3/30/11 sl@0: /// Replaced ReadFile and WriteFile with FileStreams. Thanks to Joe Dunne and John on my Ports forum for tips on this. sl@0: /// Simplified Hid.cs. sl@0: /// Replaced the form timer with a system timer. sl@0: /// sl@0: /// V4.6 sl@0: /// 1/12/10 sl@0: /// Supports Vendor IDs and Product IDs up to FFFFh. sl@0: /// sl@0: /// V4.52 sl@0: /// 11/10/09 sl@0: /// Changed HIDD_ATTRIBUTES to use UInt16 sl@0: /// sl@0: /// V4.51 sl@0: /// 2/11/09 sl@0: /// Moved Free_ and similar to Finally blocks to ensure they execute. sl@0: /// sl@0: /// V4.5 sl@0: /// 2/9/09 sl@0: /// Changes to support 64-bit systems, memory management, and other corrections. sl@0: /// Big thanks to Peter Nielsen. sl@0: /// sl@0: ///