关于c#:创建GPRS连接

Create GPRS connection

我有一台运行 Windows Mobile 5.0 的设备。它有一个程序,可以在运行时即时创建和删除手持设备的 GPRS 连接。

我正在尝试构建一个可以做类似事情的小程序。可以通过参数调用它来启动连接,但是连接需要保持,以便之后即使我的程序没有运行也可以使用手机。

使用这篇 MSDN 文章,我编写了一些代码,可以很好地创建 GPRS 连接(连接成功)但是,它似乎不是手机可以使用的连接。

在我的程序运行后,我有什么方法可以使设备连接可用?如果是这样,那是特定于设备的吗?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const int _syncConnectTimeOut = 60000;

[DllImport("CellCore.dll")]
static extern int ConnMgrMapURL(string url, ref Guid networkGuid, int passZero);
[DllImport("CellCore.dll")]
static extern int ConnMgrEstablishConnection(ConnMgrConnectionInfo connectionInfo, ref IntPtr connectionHandle);
[DllImport("CellCore.dll")]
static extern int ConnMgrEstablishConnectionSync(ConnMgrConnectionInfo connectionInfo, ref IntPtr connectionHandle, uint dwTimeout, ref ConnMgrStatus dwStatus);
[DllImport("CellCore.dll")]
static extern int ConnMgrReleaseConnection(IntPtr connectionHandle, int cache);
[DllImport("CellCore.dll")]
static extern int ConnMgrConnectionStatus(IntPtr connectionHandle, ref ConnMgrStatus status);

public void Start(string name)
{
    string url ="http://internet.com/";
    IntPtr _connectionHandle = IntPtr.Zero;          

    Guid networkGuid = Guid.Empty;
    ConnMgrStatus status = ConnMgrStatus.Unknown;
    ConnMgrMapURL(url, ref networkGuid, 0);
    ConnMgrConnectionInfo info = new ConnMgrConnectionInfo(networkGuid, ConnMgrPriority.HighPriorityBackground);
    ConnMgrEstablishConnectionSync(info, ref _connectionHandle, _syncConnectTimeOut, ref status);

    if (status == ConnMgrStatus.Connected) {
        Debug.WriteLine("Connect Succeeded");
    } else {
        Debug.WriteLine("Connect failed:" + status.ToString());
    }
}

[Flags]
enum ConnMgrParam : int
{
    GuidDestNet = 0x1,
    MaxCost = 0x2,
    MinRcvBw = 0x4,
    MaxConnLatency = 0x8
}

[Flags]
enum ConnMgrProxy : int
{
    NoProxy = 0x0,
    Http = 0x1,
    Wap = 0x2,
    Socks4 = 0x4,
    Socks5 = 0x8
}

enum ConnMgrPriority
{
    UserInteractive = 0x8000,
    HighPriorityBackground = 0x0200,
    LowPriorityBackground = 0x0008
}

enum ConnMgrStatus
{
    Unknown = 0x00,
    Connected = 0x10,
    Suspended = 0x11,
    Disconnected = 0x20,
    ConnectionFailed = 0x21,
    ConnectionCanceled = 0x22,
    ConnectionDisabled = 0x23,
    NoPathToDestination = 0x24,
    WaitingForPath = 0x25,
    WaitingForPhone = 0x26,
    PhoneOff = 0x27,
    ExclusiveConflict = 0x28,
    NoResources = 0x29,
    ConnectionLinkFailed = 0x2a,
    AuthenticationFailed = 0x2b,
    NoPathWithProperty = 0x2c,
    WaitingConnection = 0x40,
    WaitingForResource = 0x41,
    WaitingForNetwork = 0x42,
    WaitingDisconnection = 0x80,
    WaitingConnectionAbort = 0x81
}

[StructLayout(LayoutKind.Sequential)]
class ConnMgrConnectionInfo
{
    Int32 cbSize;                          // DWORD
    public ConnMgrParam dwParams = 0;      // DWORD
    public ConnMgrProxy dwFlags = 0;       // DWORD
    public ConnMgrPriority dwPriority = 0; // DWORD
    public Int32 bExclusive = 0;           // BOOL
    public Int32 bDisabled = 0;            // BOOL
    public Guid guidDestNet = Guid.Empty;  // GUID
    public IntPtr hWnd = IntPtr.Zero;      // HWND
    public UInt32 uMsg = 0;                // UINT
    public Int32 lParam = 0;               // LPARAM
    public UInt32 ulMaxCost = 0;           // ULONG
    public UInt32 ulMinRcvBw = 0;          // ULONG
    public UInt32 ulMaxConnLatency = 0;    // ULONG

    public ConnMgrConnectionInfo()
    {
        cbSize = Marshal.SizeOf(typeof(ConnMgrConnectionInfo));
    }

    public ConnMgrConnectionInfo(Guid destination, ConnMgrPriority priority, ConnMgrProxy proxy)
        : this()
    {
        guidDestNet = destination;
        dwParams = ConnMgrParam.GuidDestNet;
        dwPriority = priority;
        dwFlags = proxy;
    }

    public ConnMgrConnectionInfo(Guid destination, ConnMgrPriority priority)
        : this(destination, priority, ConnMgrProxy.NoProxy) { }

    public ConnMgrConnectionInfo(Guid destination)
        : this(destination, ConnMgrPriority.UserInteractive) { }
}


约瑟夫让我走上了正确的道路。我找到了这个答案和后续链接,这些链接解释了如何使用 OMA 客户端配置来更改设备配置并最终允许我创建 GPRS 连接。

http://msdn.microsoft.com/en-us/bb737297

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/// <summary>
/// Creates a GPRS Connection with the specified name.
/// </summary>
/// <param name="name">The name of the connection to create.</param>
public void Start(string name)
{
    string xml ="<wap-provisioningdoc>" +
                    "<characteristic type="CM_GPRSEntries">" +
                        "<characteristic type="" + name +"">" +
                           "<parm name="DestId" value="{436EF144-B4FB-4863-A041-8F905A62C572}" />" +
                           "<parm name="UserName" value="" />" +
                           "<parm name="Password" value="" />" +
                           "<parm name="Domain" value="" />" +
                           "<characteristic type="DevSpecificCellular">" +
                               "<parm name="GPRSInfoValid" value="1" />" +
                               "<parm name="GPRSInfoAccessPointName" value="internet.com" />" +
                           "</characteristic>" +
                       "</characteristic>" +
                   "</characteristic>" +
               "</wap-provisioningdoc>";

        XmlDocument configDoc = new XmlDocument();
        configDoc.LoadXml(xml);

        XmlDocument result = ConfigurationManager.ProcessConfiguration(configDoc, true);
    }

要创建或删除连接,请使用带有 wap 配置 xml 文件的 dmprocessconfig。 msdn 有这方面的例子


我没有任何GPRS设备,但它可以像关闭手柄和释放你打开的资源一样简单。

查看类似的问题:关闭 Windows Mobile 上的 GPRS 连接