Winform C # dialing program code

Source: Internet
Author: User

ThisProgramYou can implement system dialing to find the dialing settings of the system. After successful dialing, the dialing status is displayed, which is not original. C # is used for porting. For more information, see "Web-based dialing" in codeproject.Article.

The following is the file used for dialing. A Ras custom class is available when it is added to the project file.

////////////////////

Using system;
Using system. runtime. interopservices;
Using system. text;

namespace Dialup. ras
{< br> Public Enum rasfieldsizeconstants
{< br> ras_maxdevicetype = 16,
ras_maxphonenumber = 128,
ras_maxipaddress = 15,
ras_maxipxaddress = 21,
# If winver4
ras_maxentryname = 256,
ras_maxdevicename = 128,
ras_maxcallbacknumber = ras_maxphonenumber,
# else
ras_maxentryname = 20,
ras_maxdevicename = 32,
ras_maxcallbacknumber = 48,
# endif

ras_maxareacode = 10,
ras_maxpadtype = 32,
ras_maxx25address = 200,
ras_maxfacilities = 200,
ras_maxuserdata = 200,
ras_maxreplymessage = 1024,
ras_maxdnssuffix = 256,
unlen = 256,
pwlen = 256,
dnlen = 15
}

[Structlayout (layoutkind. Sequential, charset = charset. Auto)]
Public struct guid
{
Public uint data1;
Public ushort data2;
Public ushort data3;
[Financialas (unmanagedtype. byvalarray, sizeconst = 8)]
Public byte [] data4;
}

[Structlayout (layoutkind. Sequential, charset = charset. Auto)]
Public struct rasconn
{
Public int dwsize;
Public intptr hrasconn;
[Financialas (unmanagedtype. byvaltstr, sizeconst = (INT) rasfieldsizeconstants. ras_maxentryname + 1)]
Public String szentryname;
[Financialas (unmanagedtype. byvaltstr, sizeconst = (INT) rasfieldsizeconstants. ras_maxdevicetype + 1)]
Public String szdevicetype;
[Financialas (unmanagedtype. byvaltstr, sizeconst = (INT) rasfieldsizeconstants. ras_maxdevicename + 1)]
Public String szdevicename;
[Financialas (unmanagedtype. byvaltstr, sizeconst = 260)] // max_papth = 260
Public String szphonebook;
Public int dwsubentry;
Public guid guidentry;
# If (winver501)
Int dwflags;
Public luid;
# Endif
}

[Structlayout (layoutkind. Sequential, charset = charset. Auto)]
Public struct luid
{
Int lowpart;
Int highpart;
}

[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
}

[Structlayout (layoutkind. Sequential, charset = charset. Auto)]
Public class rasstats
{
Public int dwsize = marshal. sizeof (typeof (rasstats ));
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 dwconnectduration;
}

Public class RAS
{

[Dllimport ("rasapi32.dll", entrypoint = "rasenumconnectionsa ",
Setlasterror = true)]

Public 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 writons written to buffer
);

[dllimport ("rasapi32.dll", charset = charset. auto)]
Public 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 class rasdisplay
{
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;

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)
{
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. dwconnectduration/1000)/3600);
minutes = (stats. dwconnectduration/1000)/60)-(hours * 60);
seconds = (stats. dwconnectduration/1000)-(minutes * 60)-(hours * 3600);

m_duration = hours + "hours" + minutes + "Minutes" + seconds + "secs";
m_tx = stats. dwbytesxmited;
m_rx = stats. dwbytesrcved;
}< br> else
{< br> 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)
{< br> names = new rasentryname [lpnames];
for (INT I = 0; I {< br> Names [I]. dwsize = entrynamesize;
}

Retval = RAS. rasenumentries (null, null, names, ref lpsize, out lpnames );

}
M_connectionnames = new string [names. Length];

If (lpnames> 0)
{< br> for (INT I = 0; I {< br> m_connectionnames [I] = Names [I]. szentryname;
}< BR >}

Public String duration
{< br> Get
{< br> return m_connected? M_duration: "";
}< BR >}

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)
{< br> 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;
}< br> Public void disconnect ()
{< br> Ras. rashangup (m_connectedrashandle);
}< BR >}

Below is the reference of thisCodeAdd a ComboBox to display the dialing settings, use two buttons to control the connection status, and use eight labels to display the status information after the connection. For the original web application, see the article in codeproject.

//////////////////////////////////////// //////////////////////////////

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using Dialup. RAS;

Namespace dialup
{
Public partial class frmmain: Form
{
Public frmmain ()
{
Initializecomponent ();

Binddata ();
}

Private void binddata ()
{
Try
{
Rasdisplay display = new rasdisplay ();

Lblname. Text = display. connectionname;
Lblduration. Text = display. duration;
Lblreceived. Text = display. bytesreceived. tostring ();
Lbltransmitted. Text = display. bytestransmitted. tostring ();

If (! Display. isconnected)
{
Tblcurrentconnection. Visible = false;
Cboallconnections. Visible = true;
Timer1.stop ();
}
Else
{
Tblcurrentconnection. Visible = true;
Cboallconnections. Visible = false;
Timer1.start ();
}

Cboallconnections. datasource = display. connections;
}
Catch (exception E)
{
MessageBox. Show (E. Message );
}
}

Protected void btnconnect_click (Object sender, eventargs E)
{
Rasdisplay = new rasdisplay ();

Int errorval = rasdisplay. Connect (cboallconnections. Text );

If (errorval! = 0)
MessageBox. Show (errorval. tostring ());
Else
Timer1.start ();
}
Protected void btndisconnect_click (Object sender, eventargs E)
{
Rasdisplay = new rasdisplay ();
Rasdisplay. Disconnect ();
Binddata ();
}

Private void timereffectick (Object sender, eventargs E)
{
Binddata ();
}
}
}

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/bukater/archive/2007/04/01/1548545.aspx

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.