In network programming, you sometimes need to re-dial to establish a network connection (such as Ad click Software to form a valid click through re-dial). The following describes the two dialing methods in the program.
1. simplest method: Use the RasDial command
RasDial is a Windows Command that is easy to use. In practice, you can save the following code as a batch file and then run it directly or call it in a program.
Rasdial.exe/disconnect
Del "C: \ Documents ents and Settings \ hyuqin \ Cookies \ *. *"/Q' clear Cookie
Rasdial.exe connection name connection account connection password 'redialing
2. encapsulated as a class for flexible calling
Although the method mentioned above is very simple, it is not a standard implementation method and will be subject to some restrictions during use. The best way is to encapsulate the implementation code as a class library. The following class library is provided by a friend on the Internet and can be called directly.
Using System;
Using System. Runtime. InteropServices;
Public struct RASCONN
{
Public int dwSize;
Public IntPtr hrasconn;
[Financialas (UnmanagedType. ByValTStr, SizeConst = 257)]
Public string szEntryName;
[Financialas (unmanagedtype. byvaltstr, sizeconst = 17)]
Public String szdevicetype;
[Financialas (unmanagedtype. byvaltstr, sizeconst = 129)]
Public String szdevicename;
}
[Structlayout (layoutkind. Sequential, charset = charset. Auto)]
Public struct rasstats
{
Public int dwsize;
Public int dwbytesxmited;
Public int dwbytesrcved;
Public int dwframesxmited;
Public int dwframesrcved;
Public int dwcrcerr;
Public int dwtimeouterr;
Public int dwalignmenterr;
Public int dwHardwareOverrunErr;
Public int dwFramingErr;
Public int dwBufferOverrunErr;
Public int dwCompressionRatioIn;
Public int dwCompressionRatioOut;
Public int dwBps;
Public int dwConnectionDuration;
}
[StructLayout (LayoutKind. Sequential, CharSet = CharSet. Auto)]
Public struct RasEntryName
{
Public int dwSize;
// [Financialas (UnmanagedType. ByValTStr, SizeConst = (int) RasFieldSizeConstants. RAS_MaxEntryName + 1)]
Public string szEntryName;
// # If WINVER5
// Public int dwFlags;
// [Financialas (UnmanagedType. ByValTStr, SizeConst = 260 + 1)]
// Public string szPhonebookPath;
// # Endif
}
Public class RAS
{
[DllImport ("Rasapi32.dll", EntryPoint = "RasEnumConnectionsA ",
SetLastError = true)]
Internal static extern int RasEnumConnections
(
Ref RASCONN lprasconn, // buffer to receive connections data
Ref int lpcb, // size in bytes of buffer
Ref int lpcconnections // Number of connections written to buffer
);
[Dllimport ("rasapi32.dll", charset = charset. Auto)]
Internal static extern uint rasgetconnectionstatistics (
Intptr hrasconn, // handle to the connection
[In, out] rasstats lpstatistics // buffer to receive statistics
);
[Dllimport ("rasapi32.dll", charset = charset. Auto)]
Public extern static uint rashangup (
IntPtr hrasconn // handle to the RAS connection to hang up
);
[DllImport ("rasapi32.dll", CharSet = CharSet. Auto)]
Public extern static uint RasEnumEntries (
String reserved, // reserved, must be NULL
String lpszPhonebook, // pointer to full path and
// File name of phone-book file
[In, Out] RasEntryName [] lprasentryname, // buffer to receive
// Phone-book entries
Ref int lpcb, // size in bytes of buffer
Out int maid // number of entries written
// To buffer
);
[DllImport ("wininet. dll", CharSet = CharSet. Auto)]
Public extern static int InternetDial (
IntPtr hwnd,
[In] string lpszConnectoid,
Uint dwflags,
Ref int lpdwconnection,
Uint dwreserved
);
Public RAS ()
{
}
}
Public Enum del_cache_type // type to be deleted.
{
File, // indicates a Temporary Internet File
Cookie // indicates cookie
}
Public class rasdisplay
{
[Dllimport ("wininet. dll", charset = charset. Auto)]
Public static extern bool deleteurlcacheentry (
DEL_CACHE_TYPE type
);
* ***** String m_duration;
* ***** String m_ConnectionName;
* ***** String [] m_ConnectionNames;
* ***** Double m_TX;
* ***** Double m_RX;
* ***** Bool m_connected;
* ***** IntPtr m_ConnectedRasHandle;
RasStats status = new RasStats ();
Public RASDisplay ()
{
M_connected = true;
RAS lpras = new RAS ();
RASCONN lprasConn = new RASCONN ();
LprasConn. dwSize = Marshal. SizeOf (typeof (RASCONN ));
LprasConn. hrasconn = IntPtr. Zero;
Int lpcb = 0;
Int lpcConnections = 0;
Int nRet = 0;
Lpcb = Marshal. SizeOf (typeof (RASCONN ));
NRet = RAS. RasEnumConnections (ref lprasConn, ref lpcb, ref
LpcConnections );
If (nRet! = 0)
{
M_connected = false;
Return;
}
If (lpcConnections> 0)
{
// For (int I = 0; I <lpcConnections; I ++)
//{
RasStats stats = new RasStats ();
M_ConnectedRasHandle = lprasConn. hrasconn;
RAS. RasGetConnectionStatistics (lprasConn. hrasconn, stats );
M_ConnectionName = lprasConn. szEntryName;
Int Hours = 0;
Int Minutes = 0;
Int Seconds = 0;
Hours = (stats. dwConnectionDuration/1000)/3600 );
Minutes = (stats. dwConnectionDuration/1000)/60)-(Hours * 60 );
Seconds = (stats. dwConnectionDuration/1000)-(Minutes * 60)-(Hours * 3600 );
M_duration = Hours + "hours" + Minutes + "minutes" + Seconds + "secs ";
M_TX = stats. dwBytesXmited;
M_RX = stats. dwBytesRcved;
//}
}
Else
{
M_connected = false;
}
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 = RAS. 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 [I]. dwsize = entrynamesize;
}
Retval = RAS. RasEnumEntries (null, null, names, ref lpSize, out lpNames );
}
M_ConnectionNames = new string [names. Length];
If (lpNames> 0)
{
For (int I = 0; I <names. Length; I ++)
{
M_ConnectionNames [I] = names [I]. szEntryName;
}
}
}
Public string Duration
{
Get
{
Return m_connected? M_duration :"";
}
}
Public string [] Connections
{
Get
{
Return m_ConnectionNames;
}
}
Public double bytestransmitted
{
Get
{
Return m_connected? M_tx: 0;
}
}
Public double bytesreceived
{
Get
{
Return m_connected? M_rx: 0;
}
}
Public string ConnectionName
{
Get
{
Return m_connected? M_ConnectionName :"";
}
}
Public bool IsConnected
{
Get
{
Return m_connected;
}
}
Public int connect (string connection)
{
Int temp = 0;
Uint internet_auto_dial_unattended = 2;
Int retval = RAS. internetdial (intptr. Zero, connection, internet_auto_dial_unattended, ref temp, 0 );
Return retval;
}
Public void disconnect ()
{
RAS. rashangup (m_connectedrashandle );
}
}
Call method:
Rasdisplay Ras = new rasdisplay ();
RAS. Disconnect (); // disconnect
RAS. Connect ("ADSL"); // dial again