troubleshooting Question

Using modem to connect to internet WinXP

Avatar of Petroclus
PetroclusFlag for United Kingdom of Great Britain and Northern Ireland asked on
Networking Hardware-OtherC#System Programming
4 Comments1 Solution1833 ViewsLast Modified:
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!");
		}
	}


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
ASKER CERTIFIED SOLUTION
Todd Gerbert
Senior Engineer
Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros