Windows API for dialing a network connection

2.3k views Asked by At

Which windows API tells me a dial-up connection is in connected or disconnected state? Which Windows API allows me to programatically dial a dial-up connection. I want to develop an application for my personal use in Windows 7 with C#.Net. Could someone help me?

2

There are 2 answers

0
user541686 On

I believe RasDialDlg and the related API may be what you're looking for. It's a bit complicated than just calling a single function, though.

1
Cody Gray - on strike On

Start by checking out the MSDN documentation for WinINet, specifically the section entitled Establishing a Dial-Up Connection to the Internet.

To determine whether there is an active dial-up connection, use the InternetGetConnectedState function provided in wininet.dll.

// possible values returned in lpdwFlags parameter
const int INTERNET_CONNECTION_CONFIGURED = 0x40;
const int INTERNET_CONNECTION_LAN = 0x02;
const int INTERNET_CONNECTION_MODEM = 0x01;
const int INTERNET_CONNECTION_OFFLINE = 0x20;
const int INTERNET_CONNECTION_PROXY = 0x04;
const int INTERNET_RAS_INSTALLED = 0x10;

[DllImport("wininet.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern bool InternetGetConnectedState(out int lpdwFlags, int dwReserved);

And to dial a network connection, you can use the InternetDial function.

// possible values for dwFlags parameter
const int INTERNET_AUTODIAL_FORCE_ONLINE = 0x1;
const int INTERNET_AUTODIAL_FORCE_UNATTENDED = 0x2;
const int INTERNET_DIAL_FORCE_PROMPT = 0x2000;
const int INTERNET_DIAL_UNATTENDED = 0x8000;
const int INTERNET_DIAL_SHOW_OFFLINE = 0x4000;

// possible return values
const int ERROR_SUCCESS = 0x0;
const int ERROR_INVALID_PARAMETER = 0x57;
const int ERROR_NO_CONNECTION = 0x29C;
const int ERROR_USER_DISCONNECTION = 0x277;

[DllImport("wininet.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern int InternetDial(IntPtr hWndParent, string lpszConnected,
                               int dwFlags, ref int lpdwConnection,
                               int dwReserved);

Alternatively, you can explore the DotRas project, which is an attempt to provide access to the remote access service (RAS) components in Windows to the .NET platform. This is probably easier than implementing the functionality yourself.