ShowInfo Example

LibUsbDotNet

LibUsbDotNet 2.2.8 ShowInfo Example
LibUsbDotNet on SourceForge

MonoLibUsb Show Info: Console Application Description
  1. Initializes the usb context.

  2. Writes the device descriptor for all known usb devices to console output.

  3. Exits the usb context.

Example
CopyC#
using System;
using MonoLibUsb.Profile;
using Usb = MonoLibUsb.MonoUsbApi;

namespace MonoLibUsb.ShowInfo
{
    internal class ShowInfo
    {
        // The first time the Session property is used it creates a new session
        // handle instance in '__sessionHandle' and returns it. Subsequent 
        // request simply return '__sessionHandle'.
        private static MonoUsbSessionHandle __sessionHandle;
        public static MonoUsbSessionHandle Session
        {
            get
            {
                if (ReferenceEquals(__sessionHandle, null))
                    __sessionHandle = new MonoUsbSessionHandle();
                return __sessionHandle;
            }
        }
        public static void Main(string[] args)
        {
            int ret;
            MonoUsbProfileList profileList = null;

            // Initialize the context.
            if (Session.IsInvalid) 
                throw new Exception("Failed to initialize context.");

            MonoUsbApi.SetDebug(Session, 0);
            // Create a MonoUsbProfileList instance.
            profileList = new MonoUsbProfileList();

            // The list is initially empty.
            // Each time refresh is called the list contents are updated. 
            ret = profileList.Refresh(Session);
            if (ret < 0) throw new Exception("Failed to retrieve device list.");
            Console.WriteLine("{0} device(s) found.", ret);

            // Iterate through the profile list; write the device descriptor to
            // console output.
            foreach (MonoUsbProfile profile in profileList)
                Console.WriteLine(profile.DeviceDescriptor);

            // Since profile list, profiles, and sessions use safe handles the
            // code below is not required but it is considered good programming
            // to explicitly free and close these handle when they are no longer
            // in-use.
            profileList.Close();
            Session.Close();
        }
    }
}
CopyVB.NET
Imports System
Imports MonoLibUsb.Profile
Imports Usb = MonoLibUsb.MonoUsbApi

Namespace MonoLibUsb.ShowInfo
    Friend Class ShowInfo
        ' The first time the Session property is used it creates a new session
        ' handle instance in '__sessionHandle' and returns it. Subsequent 
        ' request simply return '__sessionHandle'.
        Private Shared __sessionHandle As MonoUsbSessionHandle
        Public Shared ReadOnly Property Session() As MonoUsbSessionHandle
            Get
                If ReferenceEquals(__sessionHandle, Nothing) Then
                    __sessionHandle = New MonoUsbSessionHandle()
                End If
                Return __sessionHandle
            End Get
        End Property
        Public Shared Sub Main(args As String())
            Dim ret As Integer
            Dim profileList As MonoUsbProfileList = Nothing

            ' Initialize the context.
            If Session.IsInvalid Then
                Throw New Exception("Failed to initialize context.")
            End If

            MonoUsbApi.SetDebug(Session, 0)
            ' Create a MonoUsbProfileList instance.
            profileList = New MonoUsbProfileList()

            ' The list is initially empty.
            ' Each time refresh is called the list contents are updated. 
            ret = profileList.Refresh(Session)
            If ret < 0 Then
                Throw New Exception("Failed to retrieve device list.")
            End If
            Console.WriteLine("{0} device(s) found.", ret)

            ' Iterate through the profile list; write the device descriptor to
            ' console output.
            For Each profile As MonoUsbProfile In profileList
                Console.WriteLine(profile.DeviceDescriptor)
            Next

            ' Since profile list, profiles, and sessions use safe handles the
            ' code below is not required but it is considered good programming
            ' to explicitly free and close these handle when they are no longer
            ' in-use.
            profileList.Close()
            Session.Close()
        End Sub
    End Class
End Namespace
Compiling the Code
  1. Create a new console application in your favorite designer.

  2. Verify your project references:

    • System.dll

    • LibUsbDotNet.dll

  3. Add/edit the main class. Copy/Paste code from one of the examples above.