Opening a dial-up connection.

F

Frank Rizzo

This is driving me nuts. I call the InternetDial API to open a dial-up
connection and it just returns 0 (success) without actually making a
call. Am I missing something really simple? I've tried disabling the
lan connection, but all to no avail. Thanks.




public class WinInetController
{
#region Interop Declarations
[DllImport("wininet.dll")]
private static extern bool InternetGetConnectedState(ref Int32
lpdwFlags, Int32 dwReserved);

[DllImport("Wininet.dll", EntryPoint = "InternetDial",
ExactSpelling = true, CharSet =
System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
private static extern Int32 InternetDial(IntPtr hwndParent,
string lpszConnectoid, Int32 dwFlags, ref Int32 lpdwConnection, Int32
dwReserved);

[DllImport("Wininet.dll", EntryPoint = "InternetHangUp",
ExactSpelling = true, CharSet =
System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
private static extern Int32 InternetHangUp(Int32
lpdwConnection, Int32 dwReserved);

[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
private static extern IntPtr GetDesktopWindow();

[DllImport("rasapi32.dll", CharSet = CharSet.Auto)]
private extern static uint RasEnumEntries
(
string reserved,
string lpszPhonebook,
[In, Out]RasEntryName[] lprasentryname,
ref int lpcb,
out int lpcEntries
);

private enum RasFieldSizeConstants
{
RAS_MaxDeviceType = 16,
RAS_MaxPhoneNumber = 128,
RAS_MaxIpAddress = 15,
RAS_MaxIpxAddress = 21,
#if WINVER4
RAS_MaxEntryName =256,
RAS_MaxDeviceName =128,
RAS_MaxCallbackNumber =RAS_MaxPhoneNumber,
#else
RAS_MaxEntryName = 20,
RAS_MaxDeviceName = 32,
RAS_MaxCallbackNumber = 48,
#endif

RAS_MaxAreaCode = 10,
RAS_MaxPadType = 32,
RAS_MaxX25Address = 200,
RAS_MaxFacilities = 200,
RAS_MaxUserData = 200,
RAS_MaxReplyMessage = 1024,
RAS_MaxDnsSuffix = 256,
UNLEN = 256,
PWLEN = 256,
DNLEN = 15
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct RasEntryName
{
public int dwSize;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
(int)RasFieldSizeConstants.RAS_MaxEntryName + 1)]
public string szEntryName;
#if WINVER5
public int dwFlags;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=260+1)]
public string szPhonebookPath;
#endif
}

#endregion

#region Private Variables
private enum Flags : int
{
INTERNET_CONNECTION_LAN = 2,
INTERNET_CONNECTION_MODEM = 1,
INTERNET_CONNECTION_PROXY = 4,
INTERNET_RAS_INSTALLED = 16
}

//Declaration Used For InternetDialUp.
private enum DialUpOptions : int
{
INTERNET_DIAL_UNATTENDED = 0X8000,
INTERNET_DIAL_SHOW_OFFLINE = 0X4000,
INTERNET_DIAL_FORCE_PROMPT = 0X2000
}

private const int ERROR_SUCCESS = 0X0;
private const int ERROR_INVALID_PARAMETER = 0X87;


private Int32 _ConnectionHandle = 0;
private WinConnectionType _ConnectionType =
WinConnectionType.Not_Connected;
private string _LastError = string.Empty;
private string _ConnectionName = string.Empty;
#endregion

#region Public Enums
public enum WinConnectionType : int
{
Via_Lan = 2,
Via_Modem = 1,
Via_Proxy = 4,
Via_Ras = 16,
Not_Connected = 0
}
#endregion

#region Constructors
public WinInetController(string connectionName)
{
_ConnectionName = connectionName;
}
#endregion

#region Public Methods
public bool Dial()
{
IntPtr handle = GetDesktopWindow();
Int32 dialResult;
Int32 flags = (int)DialUpOptions.INTERNET_DIAL_FORCE_PROMPT;
//| (int)DialUpOptions.INTERNET_DIAL_UNATTENDED;

dialResult = InternetDial(handle, _ConnectionName, flags,
ref _ConnectionHandle, 0);

if ((dialResult == ERROR_SUCCESS))
return true;
else
{
_LastError = "Failed to dial. Error code " +
dialResult.ToString();
return false;
}
}

public bool HangUp()
{
Int32 dialResult;
if (_ConnectionHandle != 0)
{
dialResult = InternetHangUp(_ConnectionHandle, 0);
if (dialResult == ERROR_SUCCESS)
return true;

else
{
_LastError = "Hang up did not succeed. Error code
" + dialResult.ToString();
return false;
}
}
else
{
_LastError = "The connection must be dialed first";
return false;
}
}
#endregion

#region Public Properties
public string ConnectionName
{
get { return _ConnectionName; }
}

public WinConnectionType ConnectionType
{
get { return _ConnectionType; }
}

public string LastError
{
get { return _LastError; }
}

public bool IsConnected
{
get
{
int lngFlags = 0;
bool connectionState = true;

if (InternetGetConnectedState(ref lngFlags, (int)0))
{
if ((lngFlags & (int)Flags.INTERNET_CONNECTION_LAN)
== (int)Flags.INTERNET_CONNECTION_LAN)
_ConnectionType = WinConnectionType.Via_Lan;
else if ((lngFlags &
(int)Flags.INTERNET_CONNECTION_MODEM) == (int)Flags.INTERNET_CONNECTION_LAN)
_ConnectionType = WinConnectionType.Via_Modem;
else if ((lngFlags &
(int)Flags.INTERNET_CONNECTION_PROXY) == (int)Flags.INTERNET_CONNECTION_LAN)
_ConnectionType = WinConnectionType.Via_Ras;
}
else
connectionState = false;

return connectionState;
}
}

public static List<string> Connections
{
get
{
List<String> connectionList = new List<String>();
//int lpcb = 0;
//int lpcConnections = 0;
//int nRet = 0;
int lpNames = 1;
int entryNameSize = 0;
int lpSize = 0;
RasEntryName[] names = null;

entryNameSize = Marshal.SizeOf(typeof(RasEntryName));
lpSize = lpNames * entryNameSize;

names = new RasEntryName[lpNames];
names[0].dwSize = entryNameSize;

uint retval = RasEnumEntries(null, null, names, ref
lpSize, out lpNames);

//if we have more than one connection, we need to do it
again
if (lpNames > 1)
{
names = new RasEntryName[lpNames];
for (int i = 0; i < names.Length; i++)
{
names.dwSize = entryNameSize;
}

retval = RasEnumEntries(null, null, names, ref
lpSize, out lpNames);
}

if (lpNames > 0)
{
for (int i = 0; i < names.Length; i++)
connectionList.Add(names.szEntryName);
}

return connectionList;
}
}
#endregion
}
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Did you write the RAS interop code?

I tried once and it was a nightmare, I ended using a code posted online by
somebody. I don't have the code available with me right now but if you email
me I can send it to you from my house.

Cheers,

--
Ignacio Machin
machin AT laceupsolutions com


Frank Rizzo said:
This is driving me nuts. I call the InternetDial API to open a dial-up
connection and it just returns 0 (success) without actually making a call.
Am I missing something really simple? I've tried disabling the lan
connection, but all to no avail. Thanks.




public class WinInetController
{
#region Interop Declarations
[DllImport("wininet.dll")]
private static extern bool InternetGetConnectedState(ref Int32
lpdwFlags, Int32 dwReserved);

[DllImport("Wininet.dll", EntryPoint = "InternetDial",
ExactSpelling = true, CharSet =
System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
private static extern Int32 InternetDial(IntPtr hwndParent, string
lpszConnectoid, Int32 dwFlags, ref Int32 lpdwConnection, Int32
dwReserved);

[DllImport("Wininet.dll", EntryPoint = "InternetHangUp",
ExactSpelling = true, CharSet =
System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
private static extern Int32 InternetHangUp(Int32 lpdwConnection,
Int32 dwReserved);

[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
private static extern IntPtr GetDesktopWindow();

[DllImport("rasapi32.dll", CharSet = CharSet.Auto)]
private extern static uint RasEnumEntries
(
string reserved,
string lpszPhonebook,
[In, Out]RasEntryName[] lprasentryname,
ref int lpcb,
out int lpcEntries
);

private enum RasFieldSizeConstants
{
RAS_MaxDeviceType = 16,
RAS_MaxPhoneNumber = 128,
RAS_MaxIpAddress = 15,
RAS_MaxIpxAddress = 21,
#if WINVER4
RAS_MaxEntryName =256,
RAS_MaxDeviceName =128,
RAS_MaxCallbackNumber =RAS_MaxPhoneNumber,
#else
RAS_MaxEntryName = 20,
RAS_MaxDeviceName = 32,
RAS_MaxCallbackNumber = 48,
#endif

RAS_MaxAreaCode = 10,
RAS_MaxPadType = 32,
RAS_MaxX25Address = 200,
RAS_MaxFacilities = 200,
RAS_MaxUserData = 200,
RAS_MaxReplyMessage = 1024,
RAS_MaxDnsSuffix = 256,
UNLEN = 256,
PWLEN = 256,
DNLEN = 15
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct RasEntryName
{
public int dwSize;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
(int)RasFieldSizeConstants.RAS_MaxEntryName + 1)]
public string szEntryName;
#if WINVER5
public int dwFlags;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=260+1)]
public string szPhonebookPath;
#endif
}

#endregion

#region Private Variables
private enum Flags : int
{
INTERNET_CONNECTION_LAN = 2,
INTERNET_CONNECTION_MODEM = 1,
INTERNET_CONNECTION_PROXY = 4,
INTERNET_RAS_INSTALLED = 16
}

//Declaration Used For InternetDialUp.
private enum DialUpOptions : int
{
INTERNET_DIAL_UNATTENDED = 0X8000,
INTERNET_DIAL_SHOW_OFFLINE = 0X4000,
INTERNET_DIAL_FORCE_PROMPT = 0X2000
}

private const int ERROR_SUCCESS = 0X0;
private const int ERROR_INVALID_PARAMETER = 0X87;


private Int32 _ConnectionHandle = 0;
private WinConnectionType _ConnectionType =
WinConnectionType.Not_Connected;
private string _LastError = string.Empty;
private string _ConnectionName = string.Empty;
#endregion

#region Public Enums
public enum WinConnectionType : int
{
Via_Lan = 2,
Via_Modem = 1,
Via_Proxy = 4,
Via_Ras = 16,
Not_Connected = 0
}
#endregion

#region Constructors
public WinInetController(string connectionName)
{
_ConnectionName = connectionName;
}
#endregion

#region Public Methods
public bool Dial()
{
IntPtr handle = GetDesktopWindow();
Int32 dialResult;
Int32 flags = (int)DialUpOptions.INTERNET_DIAL_FORCE_PROMPT;
//| (int)DialUpOptions.INTERNET_DIAL_UNATTENDED;

dialResult = InternetDial(handle, _ConnectionName, flags, ref
_ConnectionHandle, 0);

if ((dialResult == ERROR_SUCCESS))
return true;
else
{
_LastError = "Failed to dial. Error code " +
dialResult.ToString();
return false;
}
}

public bool HangUp()
{
Int32 dialResult;
if (_ConnectionHandle != 0)
{
dialResult = InternetHangUp(_ConnectionHandle, 0);
if (dialResult == ERROR_SUCCESS)
return true;

else
{
_LastError = "Hang up did not succeed. Error code " +
dialResult.ToString();
return false;
}
}
else
{
_LastError = "The connection must be dialed first";
return false;
}
}
#endregion

#region Public Properties
public string ConnectionName
{
get { return _ConnectionName; }
}

public WinConnectionType ConnectionType
{
get { return _ConnectionType; }
}

public string LastError
{
get { return _LastError; }
}

public bool IsConnected
{
get
{
int lngFlags = 0;
bool connectionState = true;

if (InternetGetConnectedState(ref lngFlags, (int)0))
{
if ((lngFlags & (int)Flags.INTERNET_CONNECTION_LAN) ==
(int)Flags.INTERNET_CONNECTION_LAN)
_ConnectionType = WinConnectionType.Via_Lan;
else if ((lngFlags &
(int)Flags.INTERNET_CONNECTION_MODEM) ==
(int)Flags.INTERNET_CONNECTION_LAN)
_ConnectionType = WinConnectionType.Via_Modem;
else if ((lngFlags &
(int)Flags.INTERNET_CONNECTION_PROXY) ==
(int)Flags.INTERNET_CONNECTION_LAN)
_ConnectionType = WinConnectionType.Via_Ras;
}
else
connectionState = false;

return connectionState;
}
}

public static List<string> Connections
{
get
{
List<String> connectionList = new List<String>();
//int lpcb = 0;
//int lpcConnections = 0;
//int nRet = 0;
int lpNames = 1;
int entryNameSize = 0;
int lpSize = 0;
RasEntryName[] names = null;

entryNameSize = Marshal.SizeOf(typeof(RasEntryName));
lpSize = lpNames * entryNameSize;

names = new RasEntryName[lpNames];
names[0].dwSize = entryNameSize;

uint retval = RasEnumEntries(null, null, names, ref
lpSize, out lpNames);

//if we have more than one connection, we need to do it
again
if (lpNames > 1)
{
names = new RasEntryName[lpNames];
for (int i = 0; i < names.Length; i++)
{
names.dwSize = entryNameSize;
}

retval = RasEnumEntries(null, null, names, ref lpSize,
out lpNames);
}

if (lpNames > 0)
{
for (int i = 0; i < names.Length; i++)
connectionList.Add(names.szEntryName);
}

return connectionList;
}
}
#endregion
}
}
 
L

Luke Zhang [MSFT]

Hello Frank,

Here is a sample about calling InternetDial to build a connection, you may
try it to see if can help:

How to determine the connection state of your local system and how to
initiate or end an Internet connection by using Visual Basic .NET or Visual
Basic 2005
http://support.microsoft.com/default.aspx/kb/821770

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi FRank,

I sent you the code yesterday, did u get it?


--
Ignacio Machin
machin AT laceupsolutions com

Frank Rizzo said:
This is driving me nuts. I call the InternetDial API to open a dial-up
connection and it just returns 0 (success) without actually making a call.
Am I missing something really simple? I've tried disabling the lan
connection, but all to no avail. Thanks.




public class WinInetController
{
#region Interop Declarations
[DllImport("wininet.dll")]
private static extern bool InternetGetConnectedState(ref Int32
lpdwFlags, Int32 dwReserved);

[DllImport("Wininet.dll", EntryPoint = "InternetDial",
ExactSpelling = true, CharSet =
System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
private static extern Int32 InternetDial(IntPtr hwndParent, string
lpszConnectoid, Int32 dwFlags, ref Int32 lpdwConnection, Int32
dwReserved);

[DllImport("Wininet.dll", EntryPoint = "InternetHangUp",
ExactSpelling = true, CharSet =
System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
private static extern Int32 InternetHangUp(Int32 lpdwConnection,
Int32 dwReserved);

[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
private static extern IntPtr GetDesktopWindow();

[DllImport("rasapi32.dll", CharSet = CharSet.Auto)]
private extern static uint RasEnumEntries
(
string reserved,
string lpszPhonebook,
[In, Out]RasEntryName[] lprasentryname,
ref int lpcb,
out int lpcEntries
);

private enum RasFieldSizeConstants
{
RAS_MaxDeviceType = 16,
RAS_MaxPhoneNumber = 128,
RAS_MaxIpAddress = 15,
RAS_MaxIpxAddress = 21,
#if WINVER4
RAS_MaxEntryName =256,
RAS_MaxDeviceName =128,
RAS_MaxCallbackNumber =RAS_MaxPhoneNumber,
#else
RAS_MaxEntryName = 20,
RAS_MaxDeviceName = 32,
RAS_MaxCallbackNumber = 48,
#endif

RAS_MaxAreaCode = 10,
RAS_MaxPadType = 32,
RAS_MaxX25Address = 200,
RAS_MaxFacilities = 200,
RAS_MaxUserData = 200,
RAS_MaxReplyMessage = 1024,
RAS_MaxDnsSuffix = 256,
UNLEN = 256,
PWLEN = 256,
DNLEN = 15
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct RasEntryName
{
public int dwSize;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
(int)RasFieldSizeConstants.RAS_MaxEntryName + 1)]
public string szEntryName;
#if WINVER5
public int dwFlags;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=260+1)]
public string szPhonebookPath;
#endif
}

#endregion

#region Private Variables
private enum Flags : int
{
INTERNET_CONNECTION_LAN = 2,
INTERNET_CONNECTION_MODEM = 1,
INTERNET_CONNECTION_PROXY = 4,
INTERNET_RAS_INSTALLED = 16
}

//Declaration Used For InternetDialUp.
private enum DialUpOptions : int
{
INTERNET_DIAL_UNATTENDED = 0X8000,
INTERNET_DIAL_SHOW_OFFLINE = 0X4000,
INTERNET_DIAL_FORCE_PROMPT = 0X2000
}

private const int ERROR_SUCCESS = 0X0;
private const int ERROR_INVALID_PARAMETER = 0X87;


private Int32 _ConnectionHandle = 0;
private WinConnectionType _ConnectionType =
WinConnectionType.Not_Connected;
private string _LastError = string.Empty;
private string _ConnectionName = string.Empty;
#endregion

#region Public Enums
public enum WinConnectionType : int
{
Via_Lan = 2,
Via_Modem = 1,
Via_Proxy = 4,
Via_Ras = 16,
Not_Connected = 0
}
#endregion

#region Constructors
public WinInetController(string connectionName)
{
_ConnectionName = connectionName;
}
#endregion

#region Public Methods
public bool Dial()
{
IntPtr handle = GetDesktopWindow();
Int32 dialResult;
Int32 flags = (int)DialUpOptions.INTERNET_DIAL_FORCE_PROMPT;
//| (int)DialUpOptions.INTERNET_DIAL_UNATTENDED;

dialResult = InternetDial(handle, _ConnectionName, flags, ref
_ConnectionHandle, 0);

if ((dialResult == ERROR_SUCCESS))
return true;
else
{
_LastError = "Failed to dial. Error code " +
dialResult.ToString();
return false;
}
}

public bool HangUp()
{
Int32 dialResult;
if (_ConnectionHandle != 0)
{
dialResult = InternetHangUp(_ConnectionHandle, 0);
if (dialResult == ERROR_SUCCESS)
return true;

else
{
_LastError = "Hang up did not succeed. Error code " +
dialResult.ToString();
return false;
}
}
else
{
_LastError = "The connection must be dialed first";
return false;
}
}
#endregion

#region Public Properties
public string ConnectionName
{
get { return _ConnectionName; }
}

public WinConnectionType ConnectionType
{
get { return _ConnectionType; }
}

public string LastError
{
get { return _LastError; }
}

public bool IsConnected
{
get
{
int lngFlags = 0;
bool connectionState = true;

if (InternetGetConnectedState(ref lngFlags, (int)0))
{
if ((lngFlags & (int)Flags.INTERNET_CONNECTION_LAN) ==
(int)Flags.INTERNET_CONNECTION_LAN)
_ConnectionType = WinConnectionType.Via_Lan;
else if ((lngFlags &
(int)Flags.INTERNET_CONNECTION_MODEM) ==
(int)Flags.INTERNET_CONNECTION_LAN)
_ConnectionType = WinConnectionType.Via_Modem;
else if ((lngFlags &
(int)Flags.INTERNET_CONNECTION_PROXY) ==
(int)Flags.INTERNET_CONNECTION_LAN)
_ConnectionType = WinConnectionType.Via_Ras;
}
else
connectionState = false;

return connectionState;
}
}

public static List<string> Connections
{
get
{
List<String> connectionList = new List<String>();
//int lpcb = 0;
//int lpcConnections = 0;
//int nRet = 0;
int lpNames = 1;
int entryNameSize = 0;
int lpSize = 0;
RasEntryName[] names = null;

entryNameSize = Marshal.SizeOf(typeof(RasEntryName));
lpSize = lpNames * entryNameSize;

names = new RasEntryName[lpNames];
names[0].dwSize = entryNameSize;

uint retval = RasEnumEntries(null, null, names, ref
lpSize, out lpNames);

//if we have more than one connection, we need to do it
again
if (lpNames > 1)
{
names = new RasEntryName[lpNames];
for (int i = 0; i < names.Length; i++)
{
names.dwSize = entryNameSize;
}

retval = RasEnumEntries(null, null, names, ref lpSize,
out lpNames);
}

if (lpNames > 0)
{
for (int i = 0; i < names.Length; i++)
connectionList.Add(names.szEntryName);
}

return connectionList;
}
}
#endregion
}
}
 
F

Frank Rizzo

Nice, even supports Vista. I am getting a VPN hooked up, so will see
whether it works for me.

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top