Code for GPRS connection on the mobile phone (C #. Net Development)

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. runtime. interopservices;
Using system. Threading;
Using system. collections;

Namespace connection
{
Public class connectmanager
{
Const int s_ OK = 0;
Const uint connmgr_param_guiddestnet = 0x1;
Const uint connmgr_priority_userinteractive = 0x08000;
Const uint infinite = 0 xffffffff;
Const uint connmgr_status_connected = 0x10;
Const int connmgr_max_desc = 128; // @ constdefine Max size of a Network Description

Const int connmgr_flag_proxy_http = 0x1; // @ constdefine HTTP Proxy supported
Const int connmgr_flag_proxy_wap = 0x2; // @ constdefine WAP proxy (GATEWAY) supported
Const int connmgr_flag_proxy_socks4 = 0x4; // @ constdefine socks4 proxy supported
Const int connmgr_flag_proxy_socks5 = 0x8; // @ constdefine SOCKS5 Proxy supported

Const uint16 idc_wait = 32514;
Const uint16 idc_arrow = 32512;

Private intptr m_hconnection = intptr. zero;

Public connectmanager ()
{
}

~ Connectmanager ()
{
Releaseconnection ();
}

/// <Summary>
/// Check whether the connection is available
/// </Summary>
/// <Returns> </returns>
Public bool getconnmgravailable ()
{
Intptr hconnmgr = connmgrapireadyevent ();

Bool bavailbale = false;
Uint dwresult = waitforsingleobject (hconnmgr, 2000 );

If (dwresult = 0)
{
Bavailbale = true;
}

// Close
If (hconnmgr. toint32 ()! = 0) closehandle (hconnmgr );

Return bavailbale;
}
/// <Summary>
/// Ing URL
/// </Summary>
/// <Param name = "lpszurl"> </param>
/// <Param name = "guidnetworkobject"> </param>
/// <Param name = "pcsdesc"> </param>
/// <Returns> </returns>
Public int mapurlandguid (string lpszurl, ref guid guidnetworkobject, ref string pcsdesc)
{
If (lpszurl = NULL | lpszurl. Length <1)
Return 0;

Uint nindex = 0;
Int hresult = connmgrmapurl (lpszurl, ref guidnetworkobject, ref nindex );
If (hresult <0)
{
Throw new exception ("cocould not map a request to a network identifier ");
}
Else
{
If (pcsdesc! = NULL)
{
Connmgr_destination_info destinfo = new connmgr_destination_info ();

If (connmgrenumdestinations (INT) nindex, ref destinfo)> = 0)
{
Pcsdesc = destinfo. szdescription;
}
}
}

Return (INT) nindex;
}
/// <Summary>
/// Enumerate network identifier information
/// </Summary>
/// <Param name = "lstnetidentifiers"> </param>
Public list <connmgr_destination_info> enumnetidentifier ()
{
List <connmgr_destination_info> lstnetidentifiers = new list <connmgr_destination_info> ();
// Obtain the Network List
For (uint dwenumindex = 0; dwenumindex ++)
{
Connmgr_destination_info networkdestinfo = new connmgr_destination_info ();

If (connmgrenumdestinations (INT) dwenumindex, ref networkdestinfo )! = 0)
{
Break;
}
Lstnetidentifiers. Add (networkdestinfo );
}

Return lstnetidentifiers;
}

/// <Summary>
/// Establish a connection
/// </Summary>
/// <Param name = "nindex"> </param>
/// <Returns> </returns>
Public bool establishconnection (uint nindex)
{
Releaseconnection ();
// Obtain the correct connection information
Connmgr_destination_info destinfo = new connmgr_destination_info ();
Int hresult = connmgrenumdestinations (INT) nindex, ref destinfo );

If (hresult> = 0)
{
// Initialize the Connection Structure
Connmgr_connectioninfo conninfo = new connmgr_connectioninfo ();

Conninfo. cbsize = (uint) Marshal. sizeof (conninfo );
Conninfo. dwparams = connmgr_param_guiddestnet;
Conninfo. dwflags = connmgr_flag_proxy_http | connmgr_flag_proxy_wap | connmgr_flag_proxy_socks4 | connmgr_flag_proxy_socks5;
Conninfo. dwpriority = connmgr_priority_userinteractive;
Conninfo. guiddestnet = destinfo. guid;
Conninfo. bexclusive = 0;
Conninfo. bdisabled = 0;

Uint dwstatus = 0;
Hresult = connmgrestablishconnectionsync (ref conninfo, ref m_hconnection, 10*1000, ref dwstatus );
If (hresult <0)
{

Return false;
}
Else
{
Return true;
}
}

Return false;
}
/// <Summary>
/// Connection status
/// </Summary>
/// <Param name = "ntimeoutsec"> </param>
/// <Param name = "pdwstatus"> </param>
/// <Returns> </returns>
Public bool waitforconnected (INT ntimeoutsec, ref uint pdwstatus)
{
Uint dwstarttime = gettickcount ();
Bool Bret = false;

While (gettickcount ()-dwstarttime <(uint) ntimeoutsec * 1000)
{
If (m_hconnection.toint32 ()! = 0)
{
Uint dwstatus = 0;
Int hR = connmgrconnectionstatus (m_hconnection, ref dwstatus );
If (dwstatus! = 0) pdwstatus = dwstatus;
If (HR> = 0)
{
If (dwstatus = connmgr_status_connected)
{
Bret = true;
Break;
}
}
}
Thread. Sleep (100 );
}

Return Bret;
}

/// <Summary>
/// Release all connections
/// </Summary>
Public void releaseconnection ()
{

If (m_hconnection.toint32 ()! = 0)
{
Connmgrreleaseconnection (m_hconnection, 0 );
M_hconnection = intptr. zero;
}
}

[Structlayout (layoutkind. Sequential)]
Public struct connmgr_connectioninfo
{
Public uint cbsize;
Public uint dwparams;
Public uint dwflags;
Public uint dwpriority;
Public int bexclusive;
Public int bdisabled;
Public guid guiddestnet;
Public intptr hwnd;
Public uint umsg;
Public uint lparam;
Public uint ulmaxcost;
Public uint ulminrcvbw;
Public uint ulmaxconnlatency;
}

[Structlayout (layoutkind. Sequential, charset = charset. Unicode)]
Public struct connmgr_destination_info
{
Public guid; // @ field guid associated with network
[Financialas (unmanagedtype. byvaltstr, sizeconst = connmgr_max_desc)]
Public String szdescription; // @ field description of Network
Public int fsecure; // @ field is it OK to allow multi-homing on the network
}

Public struct guid
{// Size is 16
Public uint data1;
Public uint16 data2;
Public uint16 data3;
[Financialas (unmanagedtype. byvalarray, sizeconst = 8)]
Public byte [] data4;
}

[Dllimport ("coredll. dll")]
Public static extern uint gettickcount ();

[Dllimport ("coredll. dll")]
Public static extern uint waitforsingleobject (intptr hhandle, uint dwmilliseconds );

[Dllimport ("cellcore. dll")]
Public static extern int connmgrmapurl (string pwszurl, ref guid pguid, ref uint pdwindex );

[Dllimport ("cellcore. dll")]
Public static extern int connmgrestablishconnectionsync (ref connmgr_connectioninfo CI, ref intptr phconnection, uint dwtimeout, ref uint pdwstatus );

[Dllimport ("cellcore. dll")]
Private Static extern intptr connmgrapireadyevent ();

[Dllimport ("cellcore. dll")]
Public static extern int connmgrreleaseconnection (intptr hconnection, int bcache );

[Dllimport ("cellcore. dll")]
Public static extern int connmgrenumdestinations (INT nindex, ref connmgr_destination_info pdestinfo );

[Dllimport ("cellcore. dll")]
Public static extern int connmgrconnectionstatus (intptr hconnection, // @ parm handle to connection, returned from connmgrestablishconnection
Ref uint pdwstatus // @ parm returns current connection status, one of connmgr_status _*
);

[Dllimport ("coredll. dll")]
Private Static extern int closehandle (intptr hobject );
}
}

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.