473,322 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Internet Dial from C# Code

I have not found code that can successfully opena dn close a
connection, so I created the class below. It will open, disconnect and
check the type of internet connection you have.

Paul R. Johnson
using System;
using System.Collections;
using System.Data;
using System.Diagnostics;
namespace InternetDialer
{
public class Dialer
{

[System.Runtime.InteropServices.DllImport("wininet. dll",
EntryPoint="InternetGetConnectedState", ExactSpelling=true,
CharSet=System.Runtime.InteropServices.CharSet.Ans i,
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.Ans i,
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.Ans i,
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_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(IntPtr.Zero, "My Connection",
Convert.ToInt32(DialUpOptions.INTERNET_DIAL_UNATTE NDED), 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!");
}
}

}

Nov 17 '05 #1
0 3602

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Cliff Wells | last post by:
Hi, I'm writing an application that needs to know if an Internet connection is available. Basically, I want to have something similar to what a lot of email clients have, where the app can work...
0
by: po or | last post by:
Enterprise Manager wants to dial in to the Internet when I try to open up table design, and if I cancel the dial-in, the MMC window is simply blank -- no table design editing. This is rather...
0
by: sleepyant | last post by:
Hi, how can I check the internet connection and pop-up a Dial-Up Networking dialog when there isn't any? I've tried the following: Dim myReq As HttpWebRequest Try myReq =...
9
by: sleepyant | last post by:
Hi, I've posted this question on several forum but have no response. So I hope anyone who have any idea what I'm talking about please give your solution or advice. My problem is I need to check...
8
by: Jozef | last post by:
Hello, Is there a way to test that an internet connection exists and that you are able to see the webserver before performing any connections? The reason being, when I try to connect to an SQL...
3
by: Martin Ho | last post by:
My application (before is actually fully loads) should check if user is connected to internet. Is there any way to check this with certainty? Thanks a lot. Martin ----== Posted via...
8
by: Nikola Bucic | last post by:
Could someone tell me how can I constantly check if I'm connected to internet and if possible to determine what type of connection is it. My best shot wright now is to have a timer which will...
4
by: Anjan Bhowmik | last post by:
I want to query the dial up connections present in the system, which will host my application. My user want to connect automatically to the internet if the dialup connection was hanged. Ho do...
6
by: =?Utf-8?B?QnNtZW5nZW4=?= | last post by:
I am trying to make sure that the local connection is up. I have presently been using the NetworkChange.NetworkAvailabilityChanged Event for this. Is there a better way to do this? Also, I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.