Ejemplo n.º 1
0
        private Native.SP_DEVINFO_DATA GetDeviceData(Native.SP_DEVICE_INTERFACE_DATA interfaceData, out int size)
        {
            Native.SP_DEVINFO_DATA devData = new Native.SP_DEVINFO_DATA();
            size = 0;
            if (!Native.SetupDiGetDeviceInterfaceDetail(_deviceInfoSet, interfaceData, IntPtr.Zero, 0, ref size, devData))
            {
                int error = Marshal.GetLastWin32Error();
                if (error != Native.ERROR_INSUFFICIENT_BUFFER)
                {
                    Exception ex = Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
                    Logger.Log(LogLevel.Error, ex);
                    throw ex;
                }
            }

            return(devData);
        }
Ejemplo n.º 2
0
        private Native.SP_DEVICE_INTERFACE_DATA GetInterfaceData(int index)
        {
            Native.SP_DEVICE_INTERFACE_DATA interfaceData = new Native.SP_DEVICE_INTERFACE_DATA();

            if (!Native.SetupDiEnumDeviceInterfaces(_deviceInfoSet, null, ref _classGuid, index, interfaceData))
            {
                int error = Marshal.GetLastWin32Error();
                if (error != Native.ERROR_NO_MORE_ITEMS)
                {
                    Exception ex = Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
                    Logger.Log(LogLevel.Error, ex);
                    throw ex;
                }
                return(null);
            }

            return(interfaceData);
        }
Ejemplo n.º 3
0
        private string GetDevicePath(Native.SP_DEVICE_INTERFACE_DATA interfaceData, Native.SP_DEVINFO_DATA devData, int size)
        {
            IntPtr buffer = Marshal.AllocHGlobal(size);

            try
            {
                Native.SP_DEVICE_INTERFACE_DETAIL_DATA detailData = new Native.SP_DEVICE_INTERFACE_DETAIL_DATA();
                Marshal.StructureToPtr(detailData, buffer, false);

                if (!Native.SetupDiGetDeviceInterfaceDetail(_deviceInfoSet, interfaceData, buffer, size, ref size, devData))
                {
                    Exception ex = Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
                    Logger.Log(LogLevel.Error, ex);
                    throw ex;
                }

                IntPtr pDevicePath = new IntPtr(buffer.ToInt64() + Marshal.SizeOf(typeof(int)));
                return(Marshal.PtrToStringAuto(pDevicePath));
            }
            finally
            {
                Marshal.FreeHGlobal(buffer);
            }
        }