WAP 的访问需要通过 Wireless Datagram Protocol (WDP) 协议来实现。前提是 WinCE OS 在定制时需要选择 WAP 相应的组件,否则在使用相应的 SDK 编译时,对应的 WAP 功能函数无法编译通过。
因前公司项目需要,写了如下的代码。但未经过测试(项目所需功能确认无法通过 WAP 实现),也未提供此部分代码给公司,所以在网上放出来供大家参考。
以下是在 WinCE6.0 下,使用 VS2008 开发环境的示例代码:
1 // WapTestDlg.cpp : 实现文件 2 // 3 4 #include "stdafx.h" 5 #include "WapTest.h" 6 #include "WapTestDlg.h" 7 8 #include "wap.h" 9 10 #ifdef _DEBUG 11 #define new DEBUG_NEW 12 #endif 13 14 // http://technet.microsoft.com/zh-cn/windows/ee497490(en-us).aspx 15 // http://msdn.microsoft.com/zh-cn/library/ee497490(v=WinEmbedded.60) 16 // Use UDP bearer if bUDP = true, use SMS bearer if bUDP = false; 17 bool WdpSendAndReadEcho( bool bUDP ) 18 { 19 20 bool bSendReadOK = false; 21 22 HRESULT hResult; 23 24 // Open WAP for Sending WDP UnitData 25 const WAP_LAYER wlLayer = WAP_LAYER_WDP; 26 const DWORD dwSendPort = 3000L; 27 WAP_HANDLE hWapSend = 0L; 28 HANDLE hWapSendMsgEvent= NULL; 29 30 // Open WAP for reading WDP UnitData 31 const DWORD dwRecvPort = 3001L; 32 WAP_HANDLE hWapRecv= 0L; 33 HANDLE hWapRecvMsgEvent = NULL; 34 35 // Create WDP UnitData structure with user data 36 WAP_ADDRESS waSendRecv; 37 const TCHAR *const tszIpLoopbackAddr = _T("127.0.0.1");// For UDP bearer 38 const TCHAR *const tszTelNum = _T("8009352663"); // Sample Phone number, used for SMS bearer 39 40 WDP_UNITDATA Wdpunitdata; 41 42 DWORD dwWdpRecvDataSize = 0L; 43 44 WDP_UNITDATA *pcbWdpRecvData = NULL; 45 46 // Use temporary buffer because pbUserData is const BYTE * 47 BYTE *pbBuffer = NULL; 48 49 hResult = WapOpen( wlLayer, dwSendPort, &hWapSend, &hWapSendMsgEvent ); 50 if ( FAILED(hResult) || !hWapSend || !hWapSendMsgEvent ) 51 { 52 OutputDebugString( _T("WapOpen() for sending WDP UnitData failed") ); 53 hWapSend = 0L; 54 hWapSendMsgEvent= NULL; 55 return false; 56 } 57 58 hResult = WapOpen( wlLayer, dwRecvPort, &hWapRecv, &hWapRecvMsgEvent ); 59 if ( FAILED(hResult) || !hWapRecv || !hWapRecvMsgEvent ) 60 { 61 OutputDebugString( _T("WapOpen() for reading WDP UnitData failed") ); 62 hWapRecv= 0L; 63 goto exit_label; 64 } 65 66 if(bUDP) 67 { 68 69 _tcsncpy( waSendRecv.ptsAddress, tszIpLoopbackAddr, MAX_WAP_ADDRESS_LENGTH ); 70 } 71 else 72 { 73 _tcsncpy( waSendRecv.ptsAddress, tszTelNum, MAX_WAP_ADDRESS_LENGTH); 74 } 75 76 waSendRecv.watAddressType = bUDP ? WAP_ADDRESS_TYPE_UDP : WAP_ADDRESS_TYPE_GSM_SMS; 77 78 // Initialize the WDP UnitData 79 memset( &Wdpunitdata, 0, sizeof(Wdpunitdata) ); 80 Wdpunitdata.wpiPrimitiveID = WAP_PRIMITIVE_ID_T_DUNITDATA; 81 Wdpunitdata.wptPrimitiveType = WAP_PRIMITIVE_TYPE_REQUEST; 82 Wdpunitdata.dwValidFields = (WDP_FIELD_SOURCEADDRESS | 83 WDP_FIELD_SOURCEPORT | 84 WDP_FIELD_DESTINATIONADDRESS | 85 WDP_FIELD_DESTINATIONPORT | 86 WDP_FIELD_USERDATA ); 87 Wdpunitdata.waSourceAddress = waSendRecv; 88 Wdpunitdata.dwSourcePort = dwSendPort; 89 Wdpunitdata.waDestinationAddress = Wdpunitdata.waSourceAddress; 90 Wdpunitdata.dwDestinationPort = dwRecvPort; 91 // Set Random user data with size = 0x100 92 Wdpunitdata.dwUserDataSize = 0x100; 93 94 if(pbBuffer = new BYTE[Wdpunitdata.dwUserDataSize]) 95 { 96 // Fill the user data section with random data 97 for ( DWORD dw = 0; dw < Wdpunitdata.dwUserDataSize; dw++ ) 98 { 99 pbBuffer[ dw ] = rand() % 0xFF; 100 } 101 } 102 else 103 { 104 goto exit_label; 105 } 106 Wdpunitdata.pbUserData = pbBuffer; 107 108 // Send WDP Data 109 hResult = WapSend( hWapSend, (WAP_PRIMITIVE_BASE *)&Wdpunitdata ); 110 if ( FAILED(hResult) ) 111 { 112 OutputDebugString( _T("WapSend() with WDP Unitdata failed") ); 113 goto exit_label; 114 } 115 116 // Wait for WAP message received Event 117 if ( WAIT_OBJECT_0 != WaitForSingleObject( hWapRecvMsgEvent , 10000L )) 118 { 119 OutputDebugString( _T("Failed to wait for or timed out waiting for expected WDP Data") ); 120 goto exit_label; 121 } 122 123 hResult = WapGetNextPrimitiveSize( hWapRecv, &dwWdpRecvDataSize); 124 if ( FAILED(hResult) || (0 == dwWdpRecvDataSize) ) 125 { 126 OutputDebugString( _T("WapGetNextPrimitiveSize() failed") ); 127 goto exit_label; 128 } 129 130 // Allocate memory for storing received WDP Data 131 pcbWdpRecvData =( WDP_UNITDATA* ) new BYTE[ dwWdpRecvDataSize ]; 132 if ( !pcbWdpRecvData) 133 { 134 OutputDebugString( _T("Failed to allocate memory for storing WDP Unit Data") ); 135 goto exit_label; 136 } 137 138 // Read WDP Data 139 hResult = WapRead( hWapRecv, (WAP_PRIMITIVE_BASE *)pcbWdpRecvData, dwWdpRecvDataSize ); 140 if ( FAILED(hResult) ) 141 { 142 OutputDebugString( _T("WapRead() failed") ); 143 goto exit_label; 144 } 145 146 // Validate the received primitive and 147 // Compare the received user data to the sent user data 148 if (!pcbWdpRecvData) 149 goto exit_label; 150 151 bSendReadOK = true; 152 bSendReadOK &= (WAP_PRIMITIVE_ID_T_DUNITDATA == pcbWdpRecvData->wpiPrimitiveID); 153 bSendReadOK &= (WAP_PRIMITIVE_TYPE_INDICATION == pcbWdpRecvData->wptPrimitiveType); 154 bSendReadOK &= (pcbWdpRecvData->dwValidFields & WDP_FIELD_SOURCEADDRESS ) && ( pcbWdpRecvData->dwValidFields & WDP_FIELD_SOURCEPORT ); 155 bSendReadOK &= (pcbWdpRecvData->dwUserDataSize == Wdpunitdata.dwUserDataSize); 156 if(bSendReadOK && pcbWdpRecvData->pbUserData) 157 { 158 bSendReadOK &= !memcmp(pcbWdpRecvData->pbUserData, Wdpunitdata.pbUserData, pcbWdpRecvData->dwUserDataSize); 159 } 160 161 exit_label: 162 163 // Close WAP for Sending WDP UnitData 164 if ( hWapSend) 165 { 166 hResult = WapClose( hWapSend ); 167 if ( FAILED(hResult) ) 168 { 169 OutputDebugString( _T("WapClose() for sending WDP UnitData failed") ); 170 bSendReadOK = false; 171 } 172 else 173 { 174 hWapSend = 0L; 175 } 176 } 177 178 // Close WAP for Receiving WDP UnitData 179 if ( hWapRecv ) 180 { 181 hResult = WapClose( hWapRecv ); 182 if ( FAILED(hResult) ) 183 { 184 OutputDebugString( _T("WapClose() for reading WDP UnitData failed") ); 185 bSendReadOK = false; 186 } 187 else 188 { 189 hWapRecv = 0L; 190 } 191 } 192 // Clear the buffers 193 if (pbBuffer) 194 { 195 delete[] (BYTE*) pbBuffer; 196 pbBuffer = NULL; 197 } 198 199 if ( pcbWdpRecvData ) 200 { 201 delete [] (BYTE*) pcbWdpRecvData; 202 pcbWdpRecvData = NULL; 203 } 204 205 return bSendReadOK; 206 } 207 // CWapTestDlg 对话框 208 209 CWapTestDlg::CWapTestDlg(CWnd* pParent /*=NULL*/) 210 : CDialog(CWapTestDlg::IDD, pParent) 211 { 212 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 213 } 214 215 void CWapTestDlg::DoDataExchange(CDataExchange* pDX) 216 { 217 CDialog::DoDataExchange(pDX); 218 } 219 220 BEGIN_MESSAGE_MAP(CWapTestDlg, CDialog) 221 #if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP) 222 ON_WM_SIZE() 223 #endif 224 //}}AFX_MSG_MAP 225 END_MESSAGE_MAP() 226 227 // CWapTestDlg 消息处理程序 228 229 BOOL CWapTestDlg::OnInitDialog() 230 { 231 CDialog::OnInitDialog(); 232 233 // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动 234 // 执行此操作 235 SetIcon(m_hIcon, TRUE); // 设置大图标 236 SetIcon(m_hIcon, FALSE); // 设置小图标 237 238 // TODO: 在此添加额外的初始化代码 239 WdpSendAndReadEcho(true); 240 241 return TRUE; // 除非将焦点设置到控件,否则返回 TRUE 242 }