Exemple #1
0
        private static bool RegisterModem(IntPtr hDeviceInfoSet, ref Win32Wrapper.SP_DEVINFO_DATA devInfoElem, string PortName, ref Win32Wrapper.SP_DRVINFO_DATA drvData)
        {
            if (!Win32Wrapper.SetupDiRegisterDeviceInfo(hDeviceInfoSet, ref devInfoElem, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero))
            {
                return(false);
            }
            IntPtr hKeyDev = Win32Wrapper.SetupDiOpenDevRegKey(hDeviceInfoSet, ref devInfoElem, (uint)Win32Wrapper.DICS_FLAG.DICS_FLAG_GLOBAL, 0, (uint)Win32Wrapper.DIREG.DIREG_DEV, (uint)Win32Wrapper.REGKEYSECURITY.KEY_READ);

            if (hKeyDev.ToInt64() == INVALID_HANDLE_VALUE && Marshal.GetLastWin32Error() == (int)Win32Wrapper.WinErrors.ERROR_KEY_DOES_NOT_EXIST)
            {
                hKeyDev = Win32Wrapper.SetupDiCreateDevRegKey(hDeviceInfoSet, ref devInfoElem, (int)Win32Wrapper.DICS_FLAG.DICS_FLAG_GLOBAL, 0, (int)Win32Wrapper.DIREG.DIREG_DRV, IntPtr.Zero, IntPtr.Zero);
                if (hKeyDev.ToInt64() == INVALID_HANDLE_VALUE)
                {
                    return(false);
                }
            }
            bool check = Win32Wrapper.SetupDiRegisterDeviceInfo(hDeviceInfoSet, ref devInfoElem, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);

            if (!Win32Wrapper.SetupDiGetSelectedDriver(hDeviceInfoSet, ref devInfoElem, ref drvData))
            {
                int err = Marshal.GetLastWin32Error();
                Console.WriteLine("No driver selected");
            }
            Microsoft.Win32.RegistryValueKind RegType = Microsoft.Win32.RegistryValueKind.String;
            int size = (PortName.Length + 1) * Marshal.SystemDefaultCharSize;
            int ret  = Win32Wrapper.RegSetValueEx(hKeyDev, "AttachedTo", 0, (uint)RegType, PortName, (uint)size);

            Win32Wrapper.RegCloseKey(hKeyDev);
            return(true);
        }
Exemple #2
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);
        }