/// <summary> /// Sets the subentry properties for an existing subentry, or creates a new subentry. /// </summary> /// <param name="phoneBook">Required. The <see cref="DotRas.RasPhoneBook"/> that will contain the entry.</param> /// <param name="entry">Required. The <see cref="DotRas.RasEntry"/> whose subentry to set.</param> /// <param name="subEntryId">The zero-based index of the subentry to set.</param> /// <param name="value">An <see cref="DotRas.RasSubEntry"/> object whose properties to set.</param> /// <returns><b>true</b> if the operation was successful, otherwise <b>false</b>.</returns> /// <exception cref="System.ArgumentNullException"><paramref name="phoneBook"/> or <paramref name="entry"/> or <paramref name="value"/> is a null reference (<b>Nothing</b> in Visual Basic).</exception> /// <exception cref="System.UnauthorizedAccessException">The caller does not have the required permission to perform the action requested.</exception> public bool SetSubEntryProperties(RasPhoneBook phoneBook, RasEntry entry, int subEntryId, RasSubEntry value) { if (phoneBook == null) { ThrowHelper.ThrowArgumentNullException("phoneBook"); } if (value == null) { ThrowHelper.ThrowArgumentNullException("value"); } if (entry == null) { ThrowHelper.ThrowArgumentNullException("entry"); } bool retval = false; int size = Marshal.SizeOf(typeof(NativeMethods.RASSUBENTRY)); int lpCb = size; IntPtr lpRasSubEntry = IntPtr.Zero; try { NativeMethods.RASSUBENTRY subentry = new NativeMethods.RASSUBENTRY(); subentry.size = size; subentry.phoneNumber = value.PhoneNumber; if (value.Device != null) { TypeConverter converter = TypeDescriptor.GetConverter(typeof(RasDeviceType)); if (converter == null) { ThrowHelper.ThrowInvalidOperationException(Resources.Exception_TypeConverterNotFound, "RasDeviceType"); } subentry.deviceName = value.Device.Name; subentry.deviceType = converter.ConvertToString(value.Device.DeviceType); } int alternatesLength = 0; string alternatesList = Utilities.BuildStringList(value.AlternatePhoneNumbers, '\x00', out alternatesLength); if (alternatesLength > 0) { lpCb = size + alternatesLength; subentry.alternateOffset = size; } lpRasSubEntry = Marshal.AllocHGlobal(lpCb); Marshal.StructureToPtr(subentry, lpRasSubEntry, true); if (alternatesLength > 0) { Utilities.CopyString(lpRasSubEntry, size, alternatesList, alternatesLength); } try { int ret = UnsafeNativeMethods.Instance.SetSubEntryProperties(phoneBook.Path, entry.Name, subEntryId + 2, lpRasSubEntry, lpCb, IntPtr.Zero, 0); if (ret == NativeMethods.SUCCESS) { retval = true; } else { ThrowHelper.ThrowRasException(ret); } } catch (EntryPointNotFoundException) { ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform); } } finally { if (lpRasSubEntry != IntPtr.Zero) { Marshal.FreeHGlobal(lpRasSubEntry); } } return retval; }
/// <summary> /// Retrieves the subentry properties for an entry within a phone book. /// </summary> /// <param name="phoneBook">Required. The <see cref="DotRas.RasPhoneBook"/> containing the entry.</param> /// <param name="entry">Required. The <see cref="DotRas.RasEntry"/> containing the subentry.</param> /// <param name="subEntryId">The zero-based index of the subentry to retrieve.</param> /// <returns>A new <see cref="DotRas.RasSubEntry"/> object.</returns> /// <exception cref="System.ArgumentNullException"><paramref name="phoneBook"/> or <paramref name="entry"/> is a null reference (<b>Nothing</b> in Visual Basic).</exception> /// <exception cref="System.UnauthorizedAccessException">The caller does not have the required permission to perform the action requested.</exception> public RasSubEntry GetSubEntryProperties(RasPhoneBook phoneBook, RasEntry entry, int subEntryId) { if (phoneBook == null) { ThrowHelper.ThrowArgumentNullException("phoneBook"); } if (entry == null) { ThrowHelper.ThrowArgumentNullException("entry"); } RasSubEntry retval = null; int size = Marshal.SizeOf(typeof(NativeMethods.RASSUBENTRY)); bool retry = false; IntPtr lpCb = new IntPtr(size); do { NativeMethods.RASSUBENTRY subentry = new NativeMethods.RASSUBENTRY(); subentry.size = size; IntPtr lpRasSubEntry = IntPtr.Zero; try { lpRasSubEntry = Marshal.AllocHGlobal(lpCb); Marshal.StructureToPtr(subentry, lpRasSubEntry, true); try { int ret = UnsafeNativeMethods.Instance.GetSubEntryProperties(phoneBook.Path, entry.Name, subEntryId + 2, lpRasSubEntry, ref lpCb, IntPtr.Zero, IntPtr.Zero); if (ret == NativeMethods.SUCCESS) { subentry = Utilities.PtrToStructure<NativeMethods.RASSUBENTRY>(lpRasSubEntry); retval = new RasSubEntry(); retval.Device = RasDevice.Create(subentry.deviceName, subentry.deviceType); retval.PhoneNumber = subentry.phoneNumber; if (subentry.alternateOffset != 0) { retval.AlternatePhoneNumbers = Utilities.CreateStringCollectionByLength(lpRasSubEntry, subentry.alternateOffset, lpCb.ToInt32() - subentry.alternateOffset); } retry = false; } else if (ret == NativeMethods.ERROR_BUFFER_TOO_SMALL) { retry = true; } else { ThrowHelper.ThrowRasException(ret); } } catch (EntryPointNotFoundException) { ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform); } } finally { if (lpRasSubEntry != IntPtr.Zero) { Marshal.FreeHGlobal(lpRasSubEntry); } } } while (retry); return retval; }