Example #1
0
 public static extern Boolean SetupDiGetDeviceInterfaceDetail(IntPtr hDevInfo, SP_DEVICE_INTERFACE_DATA deviceInterfaceData, SP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData, uint deviceInterfaceDetailDataSize, out uint requiredSize, SP_DEVINFO_DATA deviceInfoData);
Example #2
0
        // <summary>
        /// 通过vid,pid获得串口设备号
        /// </summary>
        /// <param name="vid">vid</param>
        /// <param name="pid">pid</param>
        /// <returns>串口号</returns>
        public string GetPortNameFormVidPid(string vid, string pid)
        {
            //{4d36e978-e325-11ce-bfc1-08002be10318}

            Guid GUID_DEVINTERFACE_DFU = new Guid(0x4d36e978, 0xe325, 0x11ce, 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18);

            Guid classGuid = Guid.Empty;

            Guid   myGUID     = Guid.Empty;
            string enumerator = "USB";

            IntPtr hDevInfo = Win32.SetupDiGetClassDevs(ref classGuid, IntPtr.Zero, IntPtr.Zero, Win32.DIGCF_DEVICEINTERFACE | Win32.DIGCF_PRESENT);

            //if (hDevInfo.ToInt32() == Win32.INVALID_HANDLE_VALUE)
            if (false)
            {
                Console.WriteLine("read hardware information error");
            }
            else
            {
                SP_DEVINFO_DATA devInfoData = new SP_DEVINFO_DATA();
                devInfoData.cbSize    = 32;
                devInfoData.classGuid = Guid.Empty;
                devInfoData.devInst   = 0;
                devInfoData.reserved  = IntPtr.Zero;
                bool result = Win32.SetupDiEnumDeviceInfo(hDevInfo, 0, devInfoData);
                if (false == result)
                {
                    int error = Marshal.GetLastWin32Error();
                    //if (error != Win32.ERROR_NO_MORE_ITEMS)
                    //  throw new Win32Exception(error);
                }

                SP_DEVICE_INTERFACE_DATA ifData = new SP_DEVICE_INTERFACE_DATA();
                ifData.cbSize             = (uint)Marshal.SizeOf(ifData);
                ifData.Flags              = 0;
                ifData.InterfaceClassGuid = Guid.Empty;
                ifData.Reserved           = IntPtr.Zero;

                bool result2 = Win32.SetupDiEnumDeviceInterfaces(hDevInfo, IntPtr.Zero, ref classGuid, 0, ifData);
                if (result2 == false)
                {
                    int error = Marshal.GetLastWin32Error();
                    // if (error != Win32.ERROR_NO_MORE_ITEMS)
                    // throw new Win32Exception(error);
                }

                uint needed;

                // This returns: needed=160, result3=false and error=122 ("The data area passed to a system call is too small")
                bool result3 = Win32.SetupDiGetDeviceInterfaceDetail(hDevInfo, ifData, null, 0, out needed, null);
                if (result3 == false)
                {
                    int error = Marshal.GetLastWin32Error();
                }

                // IntPtr detailDataBuffer = IntPtr.Zero;
                SP_DEVICE_INTERFACE_DETAIL_DATA ifDetailsData = new SP_DEVICE_INTERFACE_DETAIL_DATA();
                ifDetailsData.devicePath = new byte[needed - 4];
                ifDetailsData.cbSize     = (uint)Marshal.SizeOf(ifDetailsData);

                IntPtr detailDataBuffer = Marshal.AllocHGlobal((int)needed);
                Marshal.WriteInt32(detailDataBuffer, (IntPtr.Size == 4) ? (4 + Marshal.SystemDefaultCharSize) : 8);
                uint nBytes = needed;

                bool result4 = Win32.SetupDiGetDeviceInterfaceDetail(hDevInfo, ifData, ifDetailsData, nBytes, out needed, null);
                if (result4 == false)
                {
                    int error = Marshal.GetLastWin32Error();
                    //if (error != Win32.ERROR_NO_MORE_ITEMS)
                    //    throw new Win32Exception(error);
                }

                IntPtr pDevicePathName = new IntPtr(detailDataBuffer.ToInt32() + 4);
                String devicePathName  = Marshal.PtrToStringAuto(pDevicePathName);
            }
            return(null);
        }