/// <summary> /// Lists all addresses in the AutoDial mapping database. /// </summary> /// <param name="value">An <see cref="StructBufferedPInvokeParams"/> containing call data.</param> /// <returns>If the function succeeds, the return value is zero.</returns> public int EnumAutodialAddresses(StructBufferedPInvokeParams value) { IntPtr bufferSize = value.BufferSize; IntPtr count = value.Count; int ret = UnsafeNativeMethods.RasEnumAutodialAddresses(value.Address, ref bufferSize, ref count); value.BufferSize = bufferSize; value.Count = count; return ret; }
/// <summary> /// Lists all available remote access capable devices. /// </summary> /// <returns>A new collection of <see cref="DotRas.RasDevice"/> objects.</returns> public ReadOnlyCollection<RasDevice> GetDevices() { ReadOnlyCollection<RasDevice> retval = null; int size = Marshal.SizeOf(typeof(NativeMethods.RASDEVINFO)); StructBufferedPInvokeParams info = new StructBufferedPInvokeParams(); info.BufferSize = new IntPtr(size); info.Count = IntPtr.Zero; bool retry = false; do { NativeMethods.RASDEVINFO device = new NativeMethods.RASDEVINFO(); device.size = size; try { info.Address = Marshal.AllocHGlobal(info.BufferSize); Marshal.StructureToPtr(device, info.Address, true); int ret = SafeNativeMethods.Instance.EnumDevices(info); if (ret == NativeMethods.ERROR_BUFFER_TOO_SMALL) { retry = true; } else if (ret == NativeMethods.SUCCESS) { retry = false; NativeMethods.RASDEVINFO[] devices = Utilities.CreateArrayOfType<NativeMethods.RASDEVINFO>(info.Address, size, info.Count.ToInt32()); RasDevice[] tempArray = null; if (devices == null || devices.Length == 0) { tempArray = new RasDevice[0]; } else { tempArray = new RasDevice[devices.Length]; for (int index = 0; index < devices.Length; index++) { NativeMethods.RASDEVINFO current = devices[index]; tempArray[index] = RasDevice.Create( current.name, current.type); } } retval = new ReadOnlyCollection<RasDevice>(tempArray); } else { ThrowHelper.ThrowRasException(ret); } } catch (EntryPointNotFoundException) { ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform); } finally { if (info.Address != IntPtr.Zero) { Marshal.FreeHGlobal(info.Address); } } } while (retry); return retval; }
/// <summary> /// Retrieves a collection of addresses in the AutoDial mapping database. /// </summary> /// <returns>A new collection of <see cref="DotRas.RasAutoDialAddress"/> objects, or an empty collection if no addresses were found.</returns> public Collection<string> GetAutoDialAddresses() { Collection<string> retval = null; StructBufferedPInvokeParams info = new StructBufferedPInvokeParams(); info.BufferSize = IntPtr.Zero; info.Count = IntPtr.Zero; bool retry = false; do { try { info.Address = Marshal.AllocHGlobal(info.BufferSize); int ret = UnsafeNativeMethods.Instance.EnumAutodialAddresses(info); if (ret == NativeMethods.SUCCESS) { if (info.Count.ToInt32() > 0) { retval = Utilities.CreateStringCollectionByCount(info.Address, info.Count.ToInt32() * IntPtr.Size, info.Count.ToInt32()); } 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 (info.Address != IntPtr.Zero) { Marshal.AllocHGlobal(info.Address); } } } while (retry); return retval; }
/// <summary> /// Retrieves a read-only list of active connections. /// </summary> /// <returns>A new read-only collection of <see cref="DotRas.RasConnection"/> objects, or an empty collection if no active connections were found.</returns> public ReadOnlyCollection<RasConnection> GetActiveConnections() { ReadOnlyCollection<RasConnection> retval = null; int size = Marshal.SizeOf(typeof(NativeMethods.RASCONN)); StructBufferedPInvokeParams info = new StructBufferedPInvokeParams(); info.BufferSize = new IntPtr(size); info.Count = IntPtr.Zero; bool retry = false; do { NativeMethods.RASCONN conn = new NativeMethods.RASCONN(); conn.size = size; try { info.Address = Marshal.AllocHGlobal(info.BufferSize); Marshal.StructureToPtr(conn, info.Address, true); int ret = SafeNativeMethods.Instance.EnumConnections(info); if (ret == NativeMethods.ERROR_BUFFER_TOO_SMALL) { retry = true; } else if (ret == NativeMethods.SUCCESS) { retry = false; NativeMethods.RASCONN[] connections = Utilities.CreateArrayOfType<NativeMethods.RASCONN>( info.Address, size, info.Count.ToInt32()); RasConnection[] tempArray = null; if (connections == null || connections.Length == 0) { tempArray = new RasConnection[0]; } else { tempArray = new RasConnection[connections.Length]; for (int index = 0; index < connections.Length; index++) { NativeMethods.RASCONN current = connections[index]; RasConnection item = new RasConnection(); item.Handle = new RasHandle(current.handle, current.subEntryId > 1); item.EntryName = current.entryName; item.Device = RasDevice.Create(current.deviceName, current.deviceType); item.PhoneBookPath = current.phoneBook; item.SubEntryId = current.subEntryId; item.EntryId = current.entryId; #if (WINXP || WIN2K8 || WIN7 || WIN8) item.SessionId = current.sessionId; Utilities.SetRasConnectionOptions(item, current.connectionOptions); #endif #if (WIN2K8 || WIN7 || WIN8) item.CorrelationId = current.correlationId; #endif tempArray[index] = item; } } retval = new ReadOnlyCollection<RasConnection>(tempArray); } else { ThrowHelper.ThrowRasException(ret); } } catch (EntryPointNotFoundException) { ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform); } finally { if (info.Address != IntPtr.Zero) { Marshal.FreeHGlobal(info.Address); } } } while (retry); return retval; }
/// <summary> /// Lists all available remote access capable devices. /// </summary> /// <param name="value">An <see cref="StructBufferedPInvokeParams"/> containing call data.</param> /// <returns>If the function succeeds, the return value is zero.</returns> /// <exception cref="System.ArgumentNullException"><paramref name="value"/> is a null reference (<b>Nothing</b> in Visual Basic).</exception> public int EnumDevices(StructBufferedPInvokeParams value) { if (value == null) { ThrowHelper.ThrowArgumentNullException("value"); } IntPtr bufferSize = value.BufferSize; IntPtr count = value.Count; int ret = SafeNativeMethods.RasEnumDevices(value.Address, ref bufferSize, ref count); value.BufferSize = bufferSize; value.Count = count; return ret; }
/// <summary> /// Lists all available remote access capable devices. /// </summary> /// <param name="value">An <see cref="StructBufferedPInvokeParams"/> containing call data.</param> /// <returns>If the function succeeds, the return value is zero.</returns> /// <exception cref="System.ArgumentNullException"><paramref name="value"/> is a null reference (<b>Nothing</b> in Visual Basic).</exception> public int EnumDevices(StructBufferedPInvokeParams value) { if (value == null) { ThrowHelper.ThrowArgumentNullException("value"); } IntPtr bufferSize = value.BufferSize; IntPtr count = value.Count; PInvokeCallTraceEvent evt = new PInvokeCallTraceEvent(NativeMethods.RasApi32Dll, "RasEnumDevices"); evt.Data.Add("address", value.Address); evt.Data.Add("bufferSize-IN", bufferSize); evt.Data.Add("count-IN", count); int result = 0; try { result = SafeNativeMethods.RasEnumDevices(value.Address, ref bufferSize, ref count); evt.ResultCode = result; evt.Data.Add("bufferSize-OUT", bufferSize); evt.Data.Add("count-OUT", count); value.BufferSize = bufferSize; value.Count = count; } finally { DiagnosticTrace.Default.TraceEvent(TraceEventType.Verbose, evt); } return result; }