<! -- Startfragment --> ADSL automatic dialing class, provided that there is a broadband dialing connection in the system
Call Code :
Rasdisplay Ras = new rasdisplay ();
RAS. Disconnect (); // disconnected
RAS. Connect ("ADSL"); // dialing
Copy codeThe Code is as follows: 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
{< br> 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 ("Ra <a href =" http://dev.21tx.com/corp/sap/ "target =" _ blank "> sap </a> i32.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
);
Private string m_duration;
Private string m_connectionname;
Private string [] m_connectionnames;
Private double m_tx;
Private double m_rx;
Private bool m_connected;
Private 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 );
}
}