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
}
}
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
}
}