Пример #1
0
        public static bool UninstallModem(string COMPort)
        {
            Guid   classguid      = new Guid("4d36e96d-e325-11ce-bfc1-08002be10318");
            IntPtr hDeviceInfoSet = Win32Wrapper.SetupDiGetClassDevs(ref classguid, IntPtr.Zero, IntPtr.Zero, (uint)Win32Wrapper.DIGCF.DIGCF_PRESENT);
            bool   matchFound     = false;

            if (hDeviceInfoSet != IntPtr.Zero && hDeviceInfoSet.ToInt64() != INVALID_HANDLE_VALUE)
            {
                Win32Wrapper.SP_DEVINFO_DATA devInfoElem = new Win32Wrapper.SP_DEVINFO_DATA();
                uint index = 0;
                devInfoElem.cbSize = (uint)Marshal.SizeOf(devInfoElem);
                while (Win32Wrapper.SetupDiEnumDeviceInfo(hDeviceInfoSet, index, ref devInfoElem))
                {
                    index = index + 1;
                    IntPtr        hKeyDev   = Win32Wrapper.SetupDiOpenDevRegKey(hDeviceInfoSet, ref devInfoElem, (uint)Win32Wrapper.DICS_FLAG.DICS_FLAG_GLOBAL, 0, (uint)Win32Wrapper.DIREG.DIREG_DRV, (uint)Win32Wrapper.REGKEYSECURITY.KEY_READ);
                    int           test      = Marshal.GetLastWin32Error();
                    StringBuilder szDevDesc = new StringBuilder(20, 256);
                    if (hKeyDev.ToInt64() != INVALID_HANDLE_VALUE)
                    {
                        uint pData  = 256;
                        uint lpType = 0;
                        int  res    = Win32Wrapper.RegQueryValueEx(hKeyDev, "AttachedTo", 0, out lpType, szDevDesc, ref pData);
                        if (res == (int)Win32Wrapper.WinErrors.ERROR_SUCCESS)
                        {
                            Win32Wrapper.RegCloseKey(hKeyDev);
                            if (COMPort == szDevDesc.ToString())
                            {
                                Console.WriteLine(String.Format("Found :  {0}", COMPort));
                                matchFound = true;
                                uint DIF_REMOVE = 0x00000005;
                                if (!Win32Wrapper.SetupDiCallClassInstaller(DIF_REMOVE, hDeviceInfoSet, ref devInfoElem))
                                {
                                    Win32Wrapper.SetupDiDestroyDeviceInfoList(hDeviceInfoSet);
                                }
                                break;
                            }
                        }
                    }
                }
            }
            return(matchFound);
        }
Пример #2
0
        public static bool GetUSBDevice(UInt32 VID, UInt32 PID, ref DeviceProperties DP)
        {
            IntPtr IntPtrBuffer = Marshal.AllocHGlobal(BUFFER_SIZE);
            IntPtr h            = IntPtr.Zero;

            Win32Wrapper.WinErrors LastError;
            bool Status = false;

            try
            {
                string DevEnum          = "USB";
                string ExpectedDeviceID = "VID_" + VID.ToString("X4") + "&" + "PID_" + PID.ToString("X4");
                ExpectedDeviceID = ExpectedDeviceID.ToLowerInvariant();

                h = Win32Wrapper.SetupDiGetClassDevs(IntPtr.Zero, DevEnum, IntPtr.Zero, (int)(Win32Wrapper.DIGCF.DIGCF_PRESENT | Win32Wrapper.DIGCF.DIGCF_ALLCLASSES));
                if (h.ToInt32() != INVALID_HANDLE_VALUE)
                {
                    bool Success = true;
                    uint i       = 0;
                    while (Success)
                    {
                        if (Success)
                        {
                            UInt32 RequiredSize = 0;
                            UInt32 RegType      = 0;
                            IntPtr Ptr          = IntPtr.Zero;

                            //Create a Device Info Data structure
                            Win32Wrapper.SP_DEVINFO_DATA DevInfoData = new Win32Wrapper.SP_DEVINFO_DATA();
                            DevInfoData.cbSize = (uint)Marshal.SizeOf(DevInfoData);
                            Success            = Win32Wrapper.SetupDiEnumDeviceInfo(h, i, ref DevInfoData);

                            if (Success)
                            {
                                //Get the required buffer size
                                //First query for the size of the hardware ID, so we can know how big a buffer to allocate for the data.
                                Win32Wrapper.SetupDiGetDeviceRegistryProperty(h, ref DevInfoData, (UInt32)Win32Wrapper.SPDRP.SPDRP_HARDWAREID, ref RegType, IntPtr.Zero, 0, ref RequiredSize);

                                LastError = (Win32Wrapper.WinErrors)Marshal.GetLastWin32Error();
                                if (LastError == Win32Wrapper.WinErrors.ERROR_INSUFFICIENT_BUFFER)
                                {
                                    if (RequiredSize > BUFFER_SIZE)
                                    {
                                        Status = false;
                                    }
                                    else
                                    {
                                        if (Win32Wrapper.SetupDiGetDeviceRegistryProperty(h, ref DevInfoData, (UInt32)Win32Wrapper.SPDRP.SPDRP_HARDWAREID, ref RegType, IntPtrBuffer, BUFFER_SIZE, ref RequiredSize))
                                        {
                                            string HardwareID = Marshal.PtrToStringAuto(IntPtrBuffer);
                                            HardwareID = HardwareID.ToLowerInvariant();
                                            if (HardwareID.Contains(ExpectedDeviceID))
                                            {
                                                Status = true; //Found device
                                                if (Win32Wrapper.SetupDiGetDeviceRegistryProperty(h, ref DevInfoData, (UInt32)Win32Wrapper.SPDRP.SPDRP_FRIENDLYNAME, ref RegType, IntPtrBuffer, BUFFER_SIZE, ref RequiredSize))
                                                {
                                                    DP.FriendlyName = Marshal.PtrToStringAuto(IntPtrBuffer);
                                                }
                                                if (Win32Wrapper.SetupDiGetDeviceRegistryProperty(h, ref DevInfoData, (UInt32)Win32Wrapper.SPDRP.SPDRP_DEVTYPE, ref RegType, IntPtrBuffer, BUFFER_SIZE, ref RequiredSize))
                                                {
                                                    DP.DeviceType = Marshal.PtrToStringAuto(IntPtrBuffer);
                                                }
                                                if (Win32Wrapper.SetupDiGetDeviceRegistryProperty(h, ref DevInfoData, (UInt32)Win32Wrapper.SPDRP.SPDRP_CLASS, ref RegType, IntPtrBuffer, BUFFER_SIZE, ref RequiredSize))
                                                {
                                                    DP.DeviceClass = Marshal.PtrToStringAuto(IntPtrBuffer);
                                                }
                                                if (Win32Wrapper.SetupDiGetDeviceRegistryProperty(h, ref DevInfoData, (UInt32)Win32Wrapper.SPDRP.SPDRP_MFG, ref RegType, IntPtrBuffer, BUFFER_SIZE, ref RequiredSize))
                                                {
                                                    DP.DeviceManufacturer = Marshal.PtrToStringAuto(IntPtrBuffer);
                                                }
                                                if (Win32Wrapper.SetupDiGetDeviceRegistryProperty(h, ref DevInfoData, (UInt32)Win32Wrapper.SPDRP.SPDRP_LOCATION_INFORMATION, ref RegType, IntPtrBuffer, BUFFER_SIZE, ref RequiredSize))
                                                {
                                                    DP.DeviceLocation = Marshal.PtrToStringAuto(IntPtrBuffer);
                                                }
                                                if (Win32Wrapper.SetupDiGetDeviceRegistryProperty(h, ref DevInfoData, (UInt32)Win32Wrapper.SPDRP.SPDRP_LOCATION_PATHS, ref RegType, IntPtrBuffer, BUFFER_SIZE, ref RequiredSize))
                                                {
                                                    DP.DevicePath = Marshal.PtrToStringAuto(IntPtrBuffer);
                                                }
                                                if (Win32Wrapper.SetupDiGetDeviceRegistryProperty(h, ref DevInfoData, (UInt32)Win32Wrapper.SPDRP.SPDRP_PHYSICAL_DEVICE_OBJECT_NAME, ref RegType, IntPtrBuffer, BUFFER_SIZE, ref RequiredSize))
                                                {
                                                    DP.DevicePhysicalObjectName = Marshal.PtrToStringAuto(IntPtrBuffer);
                                                }
                                                if (Win32Wrapper.SetupDiGetDeviceRegistryProperty(h, ref DevInfoData, (UInt32)Win32Wrapper.SPDRP.SPDRP_DEVICEDESC, ref RegType, IntPtrBuffer, BUFFER_SIZE, ref RequiredSize))
                                                {
                                                    DP.DeviceDescription = Marshal.PtrToStringAuto(IntPtrBuffer);
                                                }
                                                StringBuilder sb = new StringBuilder(BUFFER_SIZE);
                                                if (Win32Wrapper.SetupDiGetDeviceInstanceId(h, ref DevInfoData, sb, BUFFER_SIZE, ref RequiredSize))
                                                {
                                                    DP.DeviceInstancePath = sb.ToString();
                                                }
                                                string path = DP.DeviceInstancePath;
                                                string key  = path.Replace("USB\\", "");
                                                key = key.Replace("\\", "+");
                                                key = key.Remove(0, 17);
                                                string deviceID = "VID_" + VID.ToString("X4") + "+" + "PID_" + PID.ToString("X4");
                                                key = deviceID + key;

                                                DP.COMPort = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\FTDIBUS\" + key + @"\0000\Device Parameters",
                                                                                       "PortName", null);

                                                break;
                                            }
                                            else
                                            {
                                                Status = false;
                                            } //End of if (HardwareID.Contains(ExpectedDeviceID))
                                        }
                                    }         //End of if (RequiredSize > BUFFER_SIZE)
                                }             //End of if (LastError == Win32Wrapper.WinErrors.ERROR_INSUFFICIENT_BUFFER)
                            }                 // End of if (Success)
                        }                     // End of if (Success)
                        else
                        {
                            LastError = (Win32Wrapper.WinErrors)Marshal.GetLastWin32Error();
                            Status    = false;
                        }
                        i++;
                    } // End of while (Success)
                }     //End of if (h.ToInt32() != INVALID_HANDLE_VALUE)

                return(Status);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                Win32Wrapper.SetupDiDestroyDeviceInfoList(h); //Clean up the old structure we no longer need.
                Marshal.FreeHGlobal(IntPtrBuffer);
            }
        }
Пример #3
0
        private static bool FindExistingDevice(string HardwareID, out int lastError)
        {
            const int ERROR_INVALID_DATA        = 13;
            const int ERROR_INSUFFICIENT_BUFFER = 122;

            lastError = 0;
            Guid   id             = Guid.Empty;
            IntPtr hDeviceInfoSet = Win32Wrapper.SetupDiGetClassDevs(ref id, IntPtr.Zero, IntPtr.Zero, (uint)(Win32Wrapper.DIGCF.DIGCF_ALLCLASSES | Win32Wrapper.DIGCF.DIGCF_PRESENT));

            if (hDeviceInfoSet.ToInt64() == INVALID_HANDLE_VALUE)
            {
                return(false);
            }
            Win32Wrapper.SP_DEVINFO_DATA DeviceInfoData = new Win32Wrapper.SP_DEVINFO_DATA();
            DeviceInfoData.cbSize = (uint)Marshal.SizeOf(DeviceInfoData);

            uint i        = 0;
            uint DataType = 0;

            byte[] buffer    = new byte[4];
            IntPtr bufferPtr = Marshal.AllocHGlobal(buffer.Length);

            Marshal.Copy(buffer, 0, bufferPtr, buffer.Length);

            uint requiredSize = 0;

            bool error = false;

            while (Win32Wrapper.SetupDiEnumDeviceInfo(hDeviceInfoSet, i, ref DeviceInfoData))
            {
                i = i + 1;
                while (!Win32Wrapper.SetupDiGetDeviceRegistryProperty(hDeviceInfoSet, ref DeviceInfoData, (uint)Win32Wrapper.SPDRP.SPDRP_HARDWAREID,
                                                                      ref DataType, bufferPtr, (uint)buffer.Length, ref requiredSize))
                {
                    if (Marshal.GetLastWin32Error() == ERROR_INVALID_DATA)
                    {
                        break;
                    }
                    else if (Marshal.GetLastWin32Error() == ERROR_INSUFFICIENT_BUFFER)
                    {
                        //resize buffer
                        Array.Resize(ref buffer, (int)requiredSize);
                        bufferPtr = Marshal.AllocHGlobal(buffer.Length);
                    }
                    else
                    {
                        error = true;
                        break;
                    }
                }

                if (Marshal.GetLastWin32Error() == ERROR_INVALID_DATA)
                {
                    continue;
                }
                if (error)
                {
                    break;
                }

                if (buffer.Length > 0)
                {
                    string str = Marshal.PtrToStringUni(bufferPtr);
                    if (str.ToUpper() == HardwareID.ToUpper())
                    {
                        return(true);
                    }
                }
            }

            Marshal.FreeHGlobal(bufferPtr);
            lastError = Marshal.GetLastWin32Error();

            Win32Wrapper.SetupDiDestroyDeviceInfoList(hDeviceInfoSet);

            return(false);
        }