コード例 #1
0
ファイル: PInvoke.cs プロジェクト: secretan/QtPrj
        internal static byte CountDevices(Guid g)
        {
            byte Devices = 0;

            IntPtr hwDeviceInfo = SetupDiGetClassDevs(ref g, null, IntPtr.Zero, CyConst.DIGCF_PRESENT | CyConst.DIGCF_INTERFACEDEVICE);
            if (IsHandleValid(hwDeviceInfo))
            {
                SP_DEVICE_INTERFACE_DATA devInterfaceData = new SP_DEVICE_INTERFACE_DATA();

                devInterfaceData.cbSize = Marshal.SizeOf(devInterfaceData);
                // if (IntPtr.Size == 8)
                //devInterfaceData.cbSize = devInterfaceData.cbSize + 4;

                //devInterfaceData.InterfaceClassGuid = g;
                // Count the number of devices
                uint i = 0;
                bool bDone = false;

                while (!bDone)
                {
                    //SetupDiEnumDeviceInterfaces (hwDeviceInfo, 0, ref g, i, devInterfaceData)
                    if (SetupDiEnumDeviceInterfaces(hwDeviceInfo, 0, ref g, i, devInterfaceData))
                        Devices++;
                    else
                    {
                        int dwLastError = Marshal.GetLastWin32Error();
                        if (dwLastError == CyConst.ERROR_NO_MORE_ITEMS) bDone = true;
                    }

                    i++;
                }

                SetupDiDestroyDeviceInfoList(hwDeviceInfo);
            }

            return Devices;
        }
コード例 #2
0
ファイル: PInvoke.cs プロジェクト: secretan/QtPrj
        internal static string GetDevicePath(Guid g, uint dev, byte[] enumer)
        {
            IntPtr hDevice = CyConst.INVALID_HANDLE;

            int predictedLength = 0;
            int actualLength = 0;

            uint flags = CyConst.DIGCF_PRESENT | (uint)((enumer == null) ? CyConst.DIGCF_INTERFACEDEVICE : CyConst.DIGCF_ALLCLASSES);

            IntPtr hwDeviceInfo = SetupDiGetClassDevs(ref g, enumer, IntPtr.Zero, flags);
            if (hwDeviceInfo.Equals(-1))
                return "";

            SP_DEVICE_INTERFACE_DATA devInterfaceData = new SP_DEVICE_INTERFACE_DATA();
            devInterfaceData.cbSize = Marshal.SizeOf(devInterfaceData);
            //if (IntPtr.Size == 8)
            //devInterfaceData.cbSize = devInterfaceData.cbSize + 4;

            int mysize = Marshal.SizeOf(devInterfaceData.Reserved);
            if (!SetupDiEnumDeviceInterfaces(hwDeviceInfo, 0, ref g, dev, devInterfaceData))
            {
                SetupDiDestroyDeviceInfoList(hwDeviceInfo);
                return "";
            }

            SetupDiGetDeviceInterfaceDetail(hwDeviceInfo, devInterfaceData, null, 0, ref predictedLength, null);
            //predictedLength = predictedLength+5;
            byte[] detailData = new byte[predictedLength];

            detailData[0] = 5;  // Set the cbSize field of what would be a SP_DEVICE_INTERFACE_DETAIL_DATA struct
            if (IntPtr.Size == 8)
                detailData[0] = 8;
            /*if (Marshal.SystemDefaultCharSize == 2)
                detailData[0] = 4 + 4;
            else
                detailData[0] = 4 + 1;*/

            if (!SetupDiGetDeviceInterfaceDetail(hwDeviceInfo, devInterfaceData, detailData,
                    predictedLength, ref actualLength, null))
            {
                int Error = Marshal.GetLastWin32Error();
                SetupDiDestroyDeviceInfoList(hwDeviceInfo);
                return "";
            }

            //
            // Convert to a string
            //
            //  DWY - Ensure null terminator for string class constructor.
            //
            char[] cData = new char[actualLength - 3];
            for (int i = 0; i < (actualLength - 4); i++)
            {
                cData[i] = (char)detailData[i + 4];
                cData[i + 1] = '\0';
            }
            string path = new string(cData);

            SetupDiDestroyDeviceInfoList(hwDeviceInfo);
            return path;
        }