Retrieving a list of Dialup Connections using c#


To get a list of dialup connections in your computer, you can P/Invoke using RasApi.dll. The code below achieves this:

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
using System;
using System.Runtime.InteropServices;
class RasEnumEntriesSample
{
const int MAX_PATH = 260;
const int RAS_MaxEntryName = 256;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
struct RASENTRYNAME
{
public int dwSize;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxEntryName + 1)]
public string szEntryName;
public int dwFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH + 1)]
public string szPhonebook;
}
[DllImport(“rasapi32.dll”, SetLastError = true, CharSet = CharSet.Auto)]
static extern uint RasEnumEntries(IntPtr reserved, IntPtr lpszPhonebook,
[In, Out] RASENTRYNAME[] lprasentryname, ref int lpcb, ref int lpcEntries);
[STAThread]
static void Main(string[] args)
{
int cb = Marshal.SizeOf(typeof(RASENTRYNAME)), entries = 0;
RASENTRYNAME[] entryNames = new RASENTRYNAME[1];
entryNames[0].dwSize = Marshal.SizeOf(typeof(RASENTRYNAME));
//Get entry number
uint nRet = RasEnumEntries(IntPtr.Zero, IntPtr.Zero, entryNames, ref cb, ref entries);
if (entries == 0) return;
string[] _EntryNames = new string[entries];
entryNames = new RASENTRYNAME[entries];
for (int i = 0; i

Advertisements

One Response

  1. great article, thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: