先建立Dailer物件
using System;
using System.Windows.Forms;
public class Dialer{
[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_DIAL_UNATTENDED = 0X8000,
INTERNET_DIAL_SHOW_OFFLINE = 0X4000,
INTERNET_DIAL_FORCE_PROMPT = 0X2000,
INTERNET_AUTODIAL_FORCE_UNATTENDED = 0x0002,
INTERNET_AUTODIAL_FORCE_ONLINE = 0x0001
}
private const int ERROR_SUCCESS = 0X0;
private const int ERROR_INVALID_PARAMETER = 0X87;
private Int32 mlConnection;
private string _撥接名稱;
public Dialer(){}
public string 撥接名稱
{
set { _撥接名稱 = value; }
get { return _撥接名稱; }
}
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, _撥接名稱, Convert.ToInt32(DialUpOptions.INTERNET_AUTODIAL_FORCE_ONLINE), ref mlConnection, 0);
if (DResult == ERROR_SUCCESS)
MessageBox.Show("Dial Up Successful");
else
MessageBox.Show("UnSuccessFull,Error Code:"+DResult.ToString());
}
public void Disconnect(){
Int32 Result = 0;
if (!(mlConnection == 0)){
Result = InternetHangUp(mlConnection, 0);
if (Result == 0)
MessageBox.Show("Hang up successful");
else
MessageBox.Show("Hang up NOT successful");
}
else
MessageBox.Show("You must dial a connection first!");
}
}
使用
private void button_Click(object sender, EventArgs e)
{
if (textBox2.Text == string.Empty)
MessageBox.Show("請填入撥接名稱"+System.Environment.NewLine+"開始工作表->連線到 其中之一的名稱");
else
{
ADSL.撥接名稱 = textBox2.Text;
ADSL.Connect();
}
}
雨季說:
其中撥接名稱,就是你建立的ADSL連線名稱,第一次使用會出現一個對話窗,只要勾選不在顯示,以後就不會在出現了...
因為幾乎大家都是申請浮動IP,所以幾乎都會自動斷線,可以利用上面Dailer物件,寫個自動撥接程式...
歡迎跟我連絡
本頁最下方有Web MSN可以直接跟我交談喔!
免安裝程式...哈哈 歡迎聊天
免安裝程式...哈哈 歡迎聊天
2008年7月22日 星期二
自動撥接ADLS (C#)
訂閱:
張貼留言 (Atom)
0 個回應:
張貼留言