예제 #1
0
        // Most of the following code is from Managed Library for Nintendo's Wiimote by Brian Peek (http://www.brianpeek.com/)
        //Microsoft Permissive License (Ms-PL)

        //This license governs use of the accompanying software. If you use the software,
        //you accept this license. If you do not accept the license, do not use the
        //software.

        //1. Definitions

        //The terms "reproduce," "reproduction," "derivative works," and "distribution"
        //have the same meaning here as under U.S. copyright law.

        //A "contribution" is the original software, or any additions or changes to the
        //software.

        //A "contributor" is any person that distributes its contribution under this
        //license.

        //"Licensed patents" are a contributor's patent claims that read directly on its
        //contribution.

        //2. Grant of Rights

        //(A) Copyright Grant- Subject to the terms of this license, including the license
        //conditions and limitations in section 3, each contributor grants you a
        //non-exclusive, worldwide, royalty-free copyright license to reproduce its
        //contribution, prepare derivative works of its contribution, and distribute its
        //contribution or any derivative works that you create.

        //(B) Patent Grant- Subject to the terms of this license, including the license
        //conditions and limitations in section 3, each contributor grants you a
        //non-exclusive, worldwide, royalty-free license under its licensed patents to
        //make, have made, use, sell, offer for sale, import, and/or otherwise dispose of
        //its contribution in the software or derivative works of the contribution in the
        //software.

        //3. Conditions and Limitations

        //(A) No Trademark License- This license does not grant you rights to use any
        //contributors' name, logo, or trademarks.

        //(B) If you bring a patent claim against any contributor over patents that you
        //claim are infringed by the software, your patent license from such contributor
        //to the software ends automatically.

        //(C) If you distribute any portion of the software, you must retain all
        //copyright, patent, trademark, and attribution notices that are present in the
        //software.

        //(D) If you distribute any portion of the software in source code form, you may
        //do so only under this license by including a complete copy of this license with
        //your distribution. If you distribute any portion of the software in compiled or
        //object code form, you may only do so under a license that complies with this
        //license.

        //(E) The software is licensed "as-is." You bear the risk of using it. The
        //contributors give no express warranties, guarantees or conditions. You may have
        //additional consumer rights under your local laws which this license cannot
        //change. To the extent permitted under your local laws, the contributors exclude
        //the implied warranties of merchantability, fitness for a particular purpose and
        //non-infringement.

        public static IEnumerable <string> GetDevicePaths()
        {
            int  index = 0;
            Guid guid;

            // get the GUID of the HID class
            NativeMethods.HidD_GetHidGuid(out guid);

            // get a handle to all devices that are part of the HID class
            // Fun fact:  DIGCF_PRESENT worked on my machine just fine.  I reinstalled Vista, and now it no longer finds the Wiimote with that parameter enabled...
            IntPtr hDevInfo = NativeMethods.SetupDiGetClassDevs(ref guid, null, IntPtr.Zero, NativeMethods.DIGCF_DEVICEINTERFACE);// | HIDImports.DIGCF_PRESENT);

            // create a new interface data struct and initialize its size
            NativeMethods.SP_DEVICE_INTERFACE_DATA diData = new NativeMethods.SP_DEVICE_INTERFACE_DATA();
            diData.cbSize = Marshal.SizeOf(diData);

            // get a device interface to a single device (enumerate all devices)
            while (NativeMethods.SetupDiEnumDeviceInterfaces(hDevInfo, IntPtr.Zero, ref guid, index, ref diData))
            {
                UInt32 size;

                // get the buffer size for this device detail instance (returned in the size parameter)
                NativeMethods.SetupDiGetDeviceInterfaceDetail(hDevInfo, ref diData, IntPtr.Zero, 0, out size, IntPtr.Zero);

                // create a detail struct and set its size
                NativeMethods.SP_DEVICE_INTERFACE_DETAIL_DATA diDetail = new NativeMethods.SP_DEVICE_INTERFACE_DETAIL_DATA();

                // yeah, yeah...well, see, on Win x86, cbSize must be 5 for some reason.  On x64, apparently 8 is what it wants.
                // someday I should figure this out.  Thanks to Paul Miller on this...
                if (IntPtr.Size == 8)
                {
                    diDetail.cbSize = 8;
                }
                else
                {
                    diDetail.cbSize = 5;
                }

                // actually get the detail struct
                if (NativeMethods.SetupDiGetDeviceInterfaceDetail(hDevInfo, ref diData, ref diDetail, size, out size, IntPtr.Zero))
                {
                    yield return(diDetail.DevicePath);
                }

                index++;
            }
        }
예제 #2
0
        // Most of the following code is from Managed Library for Nintendo's Wiimote by Brian Peek (http://www.brianpeek.com/)
        //Microsoft Permissive License (Ms-PL)
        //This license governs use of the accompanying software. If you use the software,
        //you accept this license. If you do not accept the license, do not use the
        //software.
        //1. Definitions
        //The terms "reproduce," "reproduction," "derivative works," and "distribution"
        //have the same meaning here as under U.S. copyright law.
        //A "contribution" is the original software, or any additions or changes to the
        //software.
        //A "contributor" is any person that distributes its contribution under this
        //license.
        //"Licensed patents" are a contributor's patent claims that read directly on its
        //contribution.
        //2. Grant of Rights
        //(A) Copyright Grant- Subject to the terms of this license, including the license
        //conditions and limitations in section 3, each contributor grants you a
        //non-exclusive, worldwide, royalty-free copyright license to reproduce its
        //contribution, prepare derivative works of its contribution, and distribute its
        //contribution or any derivative works that you create.
        //(B) Patent Grant- Subject to the terms of this license, including the license
        //conditions and limitations in section 3, each contributor grants you a
        //non-exclusive, worldwide, royalty-free license under its licensed patents to
        //make, have made, use, sell, offer for sale, import, and/or otherwise dispose of
        //its contribution in the software or derivative works of the contribution in the
        //software.
        //3. Conditions and Limitations
        //(A) No Trademark License- This license does not grant you rights to use any
        //contributors' name, logo, or trademarks.
        //(B) If you bring a patent claim against any contributor over patents that you
        //claim are infringed by the software, your patent license from such contributor
        //to the software ends automatically.
        //(C) If you distribute any portion of the software, you must retain all
        //copyright, patent, trademark, and attribution notices that are present in the
        //software.
        //(D) If you distribute any portion of the software in source code form, you may
        //do so only under this license by including a complete copy of this license with
        //your distribution. If you distribute any portion of the software in compiled or
        //object code form, you may only do so under a license that complies with this
        //license.
        //(E) The software is licensed "as-is." You bear the risk of using it. The
        //contributors give no express warranties, guarantees or conditions. You may have
        //additional consumer rights under your local laws which this license cannot
        //change. To the extent permitted under your local laws, the contributors exclude
        //the implied warranties of merchantability, fitness for a particular purpose and
        //non-infringement.
        public static IEnumerable<string> GetDevicePaths()
        {
            int index = 0;
            Guid guid;

            // get the GUID of the HID class
            NativeMethods.HidD_GetHidGuid(out guid);

            // get a handle to all devices that are part of the HID class
            // Fun fact:  DIGCF_PRESENT worked on my machine just fine.  I reinstalled Vista, and now it no longer finds the Wiimote with that parameter enabled...
            IntPtr hDevInfo = NativeMethods.SetupDiGetClassDevs(ref guid, null, IntPtr.Zero, NativeMethods.DIGCF_DEVICEINTERFACE);// | HIDImports.DIGCF_PRESENT);

            // create a new interface data struct and initialize its size
            NativeMethods.SP_DEVICE_INTERFACE_DATA diData = new NativeMethods.SP_DEVICE_INTERFACE_DATA();
            diData.cbSize = Marshal.SizeOf(diData);

            // get a device interface to a single device (enumerate all devices)
            while (NativeMethods.SetupDiEnumDeviceInterfaces(hDevInfo, IntPtr.Zero, ref guid, index, ref diData))
            {
                UInt32 size;

                // get the buffer size for this device detail instance (returned in the size parameter)
                NativeMethods.SetupDiGetDeviceInterfaceDetail(hDevInfo, ref diData, IntPtr.Zero, 0, out size, IntPtr.Zero);

                // create a detail struct and set its size
                NativeMethods.SP_DEVICE_INTERFACE_DETAIL_DATA diDetail = new NativeMethods.SP_DEVICE_INTERFACE_DETAIL_DATA();

                // yeah, yeah...well, see, on Win x86, cbSize must be 5 for some reason.  On x64, apparently 8 is what it wants.
                // someday I should figure this out.  Thanks to Paul Miller on this...
                if (IntPtr.Size == 8)
                    diDetail.cbSize = 8;
                else
                    diDetail.cbSize = 5;

                // actually get the detail struct
                if (NativeMethods.SetupDiGetDeviceInterfaceDetail(hDevInfo, ref diData, ref diDetail, size, out size, IntPtr.Zero))
                {
                    yield return diDetail.DevicePath;
                }

                index++;
            }
        }