Link to home
Start Free TrialLog in
Avatar of Petroclus
PetroclusFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Using modem to connect to internet WinXP

C# WinXP

I'm tring to write a class that connects to the internet using a modem (3G). I am using the Win32 API function called InternetDial to make the connection. This works Ok, except that if there is a failure (no dial tone etc), Windows pops up a dialog message describing the problem. I would like this class to operate in an unattended environment and therefore I don't want it to show any dialog messages - I want it to fail with no dialog/user intervention.

There is a flag called INTERNET_AUTODIAL_FORCE_UNATTENDED which appears to offer this functionality, except that annoyingly Windows still pops up a dialog when the dial-up fails.

Here is my class (from http://bytes.com/topic/c-sharp/answers/275968-internet-dial-c-code):

	public class Dialer
	{
		private IntPtr Handle;

		public Dialer(IntPtr handle)
		{
			Handle = handle;
		}

		[System.Runtime.InteropServices.DllImport("wininet.dll",
		EntryPoint="InternetGetConnectedState", ExactSpelling=true,
		CharSet=System.Runtime.InteropServices.CharSet.Ansi,
		SetLastError=true)]
		private static extern bool InternetGetConnectedState(ref Int32
		lpdwFlags, Int32 dwReserved);

		[System.Runtime.InteropServices.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);

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

		private enum Flags: int
		{
			//Local system uses a LAN to connect to the Internet.
			INTERNET_CONNECTION_LAN = 0X2,
			//Local system uses a modem to connect to the Internet.
			INTERNET_CONNECTION_MODEM = 0X1,
			//Local system uses a proxy server to connect to the Internet.
			INTERNET_CONNECTION_PROXY = 0X4,
			//Type Visual Basic 6 code here...

			//Local system has RAS installed.
			INTERNET_RAS_INSTALLED = 0X10
		}

		//Declaration Used For InternetDialUp.
		private enum DialUpOptions: int
		{
			INTERNET_AUTODIAL_FORCE_ONLINE = 0x0001,
			INTERNET_AUTODIAL_FORCE_UNATTENDED = 0x0002,
			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 mlConnection;

		public string GetConnectionType()
		{
			Int32 lngFlags = 0;

			if (InternetGetConnectedState(ref lngFlags, 0))
			{
				//connected.
				if ((lngFlags & (int)Flags.INTERNET_CONNECTION_LAN)!=0)
				{
					//LAN connection.
					return "LAN connection.";
				}
				else if ((lngFlags & (int)Flags.INTERNET_CONNECTION_MODEM)!=0)
				{
					//Modem connection.
					return "Modem connection.";
				}
				else if ((lngFlags & (int)Flags.INTERNET_CONNECTION_PROXY)!=0)
				{
					//Proxy connection.
					return "Proxy connection.";
				}
				return "Not connected.";
			}
			else
			{
				//not connected.
				return "Not connected.";
			}
		}

		public void Connect()
		{
			Int32 DResult = 0;

			DResult = InternetDial(Handle, "Uk2",
			Convert.ToInt32(DialUpOptions.INTERNET_AUTODIAL_FORCE_UNATTENDED), ref mlConnection, 0);

			if (DResult == ERROR_SUCCESS)
				Console.WriteLine("Dial Up Successful");
			else
				Console.WriteLine("UnSuccessFull Error Code");
		}

		public void Disconnect()
		{
			Int32 Result = 0;

			if (! (mlConnection == 0))
			{
				Result = InternetHangUp(mlConnection, 0);

				if (Result == 0)
					Console.WriteLine("Hang up successful");
				else
					Console.WriteLine("Hang up NOT successful");
			}
			else
				Console.WriteLine("You must dial a connection first!");
		}
	}

Open in new window



Does anyone know why setting INTERNET_AUTODIAL_FORCE_UNATTENDED still pops up a dialog when it fails - is there a bug in the code above? Is there a better way of forcing internet connection via modem?

Thanks
Avatar of puru1981
puru1981

use a try catch block and log the error in event viewer or wherever you want. This way popup message will not appear.
Avatar of Petroclus

ASKER

Thanks puru1981 - try/catch doesn't do anything because InternetDial doesn't throw any exception when it fails, it presents a dialog box to the user which I want to try and stop.
ASKER CERTIFIED SOLUTION
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I did have some trouble with the RasGetEntryDialParams(0 function as it kept returning error 632 (invalid structure size). It appears there are multiple structure sizes dependent on O.S.

Many thanks for a great solution.