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