📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.Status;
using System.Runtime.InteropServices;
using System.Net.Sockets;
using Microsoft.Win32;
namespace GPRSState
{
public partial class Form1 : Form
{
[DllImport("cellcore.dll")]
private static extern int ConnMgrEstablishConnectionSync(ref ConnectionInfo ci, ref IntPtr phConnection, uint dwTimeout, ref uint pdwStatus);
const int S_OK = 0;
const uint CONNMGR_PARAM_GUIDDESTNET = 0x1;
const uint CONNMGR_FLAG_PROXY_HTTP = 0x1;
const uint CONNMGR_PRIORITY_USERINTERACTIVE = 0x08000;
const uint INFINITE = 0xffffffff;
const uint CONNMGR_STATUS_CONNECTED = 0x10;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
ConnectionInfo ci = new ConnectionInfo();
IntPtr phConnection = IntPtr.Zero;
uint dwTimeout = 0xffffffff;
uint statu = 0;
ci.cbSize = (uint)Marshal.SizeOf(ci);
ci.dwParams = CONNMGR_PARAM_GUIDDESTNET;
ci.dwFlags = CONNMGR_FLAG_PROXY_HTTP;
ci.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;
ci.bExclusive = 0;
ci.bDisabled = 0;
ci.guidDestNet = new Guid("436ef144-b4fb-4863-a041-8f905a62c572");
ci.hWnd = IntPtr.Zero;
ci.uMsg = 0;
ci.lParam = 0;
if (ConnMgrEstablishConnectionSync(ref ci, ref phConnection, dwTimeout, ref statu) != S_OK && statu != CONNMGR_STATUS_CONNECTED)
MessageBox.Show("NO");
else
MessageBox.Show("OK");
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
//ConnMgrEstablishConnectionSync(ref ci, ref phConnection, dwTimeout, ref statu);
}
private void button2_Click(object sender, EventArgs e)
{
//Application.Exit();
this.Hide();
}
private void button3_Click(object sender, EventArgs e)
{
string[] connNames = GetAllConnNames();
string s = "";
int i = 1;
foreach(string connName in connNames)
{
s += i.ToString() + ":" + connName + "\r\n";
i++;
}
MessageBox.Show(s);
}
public void DoTcpConnection()
{
string url = "www.msn.com";
bool res = GPRSConnection.Setup("http://" + url + "/");
}
private void button4_Click(object sender, EventArgs e)
{
string pszWXMLin =
"<wap-provisioningdoc>" +
"<characteristic type=\"CM_GPRSEntries\">" +
"<characteristic type=\"CMCC\">" +
"<parm name=\"DestId\" value=\"{436EF144-B4FB-4863-A041-8F905A62C572}\" />" +
"<characteristic type=\"DevSpecificCellular\">" +
"<parm name=\"BearerInfoValid\" value=\"1\" />" +
"<parm name=\"GPRSInfoValid\" value=\"1\" />" +
"<parm name=\"GPRSInfoProtocolType\" value=\"2\" />" +
"<parm name=\"GPRSInfoL2ProtocolType\" value=\"PPP\" />" +
"<parm name=\"GPRSInfoAccessPointName\" value=\"cmnet\" />" +
"<parm name=\"GPRSInfoAddress\" value=\"\" />" +
"<parm name=\"GPRSInfoDataCompression\" value=\"1\" />" +
"<parm name=\"GPRSInfoHeaderCompression\" value=\"1\" />" +
"<parm name=\"GPRSInfoParameters\" value=\"\" />" +
"</characteristic>" +
"</characteristic>" +
"</characteristic>" +
"</wap-provisioningdoc>";
MessageBox.Show(ConfigWrapper.ProcessXml(pszWXMLin));
}
private void button5_Click(object sender, EventArgs e)
{
//查看是否在GPRS的覆盖范围内
MessageBox.Show(SystemState.PhoneGprsCoverage.ToString());
}
private void button6_Click(object sender, EventArgs e)
{
object o = GetGPRSIsConned();
MessageBox.Show(o.ToString());
}
private void button7_Click(object sender, EventArgs e)
{
MessageBox.Show(DeleteOneConn("aaa"));
}
private void button8_Click(object sender, EventArgs e)
{
if (DeleteAllConn(GetAllConnNames()))
{
MessageBox.Show("OK");
}
else
{
MessageBox.Show("NO");
}
}
/// <summary>
/// 删除所有的GPRS连接
/// </summary>
/// <param name="connNames"></param>
/// <returns></returns>
private bool DeleteAllConn(string[] connNames)
{
try
{
foreach (string connName in connNames)
{
DeleteOneConn(connName);
}
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 删除一个GPRS连接
/// </summary>
/// <param name="connName"></param>
/// <returns></returns>
private string DeleteOneConn(string connName)
{
string pszWXMLin = "<wap-provisioningdoc>" +
"<characteristic type=\"CM_GPRSEntries\">" +
"<nocharacteristic type=\"" + connName + "\"/>" +
"</characteristic>" +
"</wap-provisioningdoc>";
return ConfigWrapper.ProcessXml(pszWXMLin);
}
/// <summary>
/// 读取注册表,获取GPRS是否已连接,返回0表示未连接,返回1表示已连接。
/// </summary>
/// <returns></returns>
private object GetGPRSIsConned()
{
string ss = @"HKEY_LOCAL_MACHINE\System\State\Phone";
return Registry.GetValue(ss, "Cellular System Connected", "Error");
}
/// <summary>
/// 获取所有GPRS的连接名
/// </summary>
/// <returns></returns>
private string[] GetAllConnNames()
{
RegistryKey rk = Registry.LocalMachine;
RegistryKey rkConn = rk.OpenSubKey(@"Comm\ConnMgr\Providers\{7C4B7A38-5FF7-4bc1-80F6-5DA7870BB1AA}\Connections");
return rkConn.GetSubKeyNames();
}
}
[StructLayout(LayoutKind.Sequential)]
public struct ConnectionInfo
{
public uint cbSize;
public uint dwParams;
public uint dwFlags;
public uint dwPriority;
public uint bExclusive;
public uint bDisabled;
public Guid guidDestNet;
public IntPtr hWnd;
public uint uMsg;
public uint lParam;
public uint ulMaxCost;
public uint ulMinRcvBw;
public uint ulMaxConnLatency;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -