Example #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);
        }