如何通过 WinCE 访问 WAP 数据

WAP 的访问需要通过 Wireless Datagram Protocol (WDP) 协议来实现。前提是 WinCE OS 在定制时需要选择 WAP 相应的组件,否则在使用相应的 SDK 编译时,对应的 WAP 功能函数无法编译通过。

因前公司项目需要,写了如下的代码。但未经过测试(项目所需功能确认无法通过 WAP 实现),也未提供此部分代码给公司,所以在网上放出来供大家参考。


以下是在 WinCE6.0 下,使用 VS2008 开发环境的示例代码:

// WapTestDlg.cpp : 实现文件
// 

#include "stdafx.h"
#include "WapTest.h"
#include "WapTestDlg.h"

#include "wap.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// http://technet.microsoft.com/zh-cn/windows/ee497490(en-us).aspx
// http://msdn.microsoft.com/zh-cn/library/ee497490(v=WinEmbedded.60)
//  Use UDP bearer if bUDP = true, use SMS bearer if bUDP = false;
bool WdpSendAndReadEcho( bool bUDP ) 
{

	bool bSendReadOK = false;

	HRESULT hResult;

	//  Open WAP for Sending WDP UnitData
	const WAP_LAYER wlLayer = WAP_LAYER_WDP;
	const DWORD dwSendPort = 3000L;
	WAP_HANDLE hWapSend = 0L;
	HANDLE hWapSendMsgEvent= NULL;

	//  Open WAP for reading WDP UnitData
	const DWORD dwRecvPort = 3001L;
	WAP_HANDLE hWapRecv= 0L;
	HANDLE hWapRecvMsgEvent = NULL;

	//  Create WDP UnitData structure with user data
	WAP_ADDRESS waSendRecv;
	const TCHAR *const tszIpLoopbackAddr = _T("127.0.0.1");// For UDP bearer
	const TCHAR *const tszTelNum = _T("8009352663");   // Sample Phone number, used for SMS bearer

	WDP_UNITDATA Wdpunitdata;

	DWORD dwWdpRecvDataSize = 0L;

	WDP_UNITDATA *pcbWdpRecvData = NULL;

	// Use temporary buffer because pbUserData is const BYTE * 
	BYTE *pbBuffer = NULL;

	hResult = WapOpen( wlLayer, dwSendPort, &hWapSend, &hWapSendMsgEvent );
	if ( FAILED(hResult) || !hWapSend || !hWapSendMsgEvent  ) 
	{
		OutputDebugString( _T("WapOpen() for sending WDP UnitData failed") );
		hWapSend = 0L;
		hWapSendMsgEvent= NULL;
		return false;
	}

	hResult = WapOpen( wlLayer, dwRecvPort, &hWapRecv, &hWapRecvMsgEvent );
	if ( FAILED(hResult) || !hWapRecv || !hWapRecvMsgEvent ) 
	{
		OutputDebugString( _T("WapOpen() for reading WDP UnitData failed") );
		hWapRecv= 0L;
		goto exit_label;
	}

	if(bUDP)
	{

		_tcsncpy( waSendRecv.ptsAddress, tszIpLoopbackAddr, MAX_WAP_ADDRESS_LENGTH );
	}
	else
	{
		_tcsncpy( waSendRecv.ptsAddress, tszTelNum, MAX_WAP_ADDRESS_LENGTH);
	}

	waSendRecv.watAddressType = bUDP ? WAP_ADDRESS_TYPE_UDP : WAP_ADDRESS_TYPE_GSM_SMS;

	// Initialize the WDP UnitData
	memset( &Wdpunitdata, 0, sizeof(Wdpunitdata) );
	Wdpunitdata.wpiPrimitiveID = WAP_PRIMITIVE_ID_T_DUNITDATA;
	Wdpunitdata.wptPrimitiveType = WAP_PRIMITIVE_TYPE_REQUEST;
	Wdpunitdata.dwValidFields =  (WDP_FIELD_SOURCEADDRESS      |
		WDP_FIELD_SOURCEPORT         |
		WDP_FIELD_DESTINATIONADDRESS |
		WDP_FIELD_DESTINATIONPORT    |
		WDP_FIELD_USERDATA );
	Wdpunitdata.waSourceAddress =  waSendRecv;
	Wdpunitdata.dwSourcePort = dwSendPort;
	Wdpunitdata.waDestinationAddress = Wdpunitdata.waSourceAddress;
	Wdpunitdata.dwDestinationPort = dwRecvPort;
	// Set Random user data with size = 0x100
	Wdpunitdata.dwUserDataSize = 0x100;

	if(pbBuffer = new BYTE[Wdpunitdata.dwUserDataSize])
	{
		// Fill the user data section with random data
		for ( DWORD dw = 0; dw < Wdpunitdata.dwUserDataSize; dw++ ) 
		{
			pbBuffer[ dw ] = rand() % 0xFF;
		}
	}    
	else 
	{
		goto exit_label;
	}
	Wdpunitdata.pbUserData = pbBuffer;

	//  Send WDP Data
	hResult = WapSend( hWapSend, (WAP_PRIMITIVE_BASE *)&Wdpunitdata );
	if ( FAILED(hResult) ) 
	{
		OutputDebugString( _T("WapSend() with WDP Unitdata failed") );
		goto exit_label;
	}

	//  Wait for WAP message received Event
	if ( WAIT_OBJECT_0 != WaitForSingleObject( hWapRecvMsgEvent , 10000L ))
	{
		OutputDebugString( _T("Failed to wait for or timed out waiting for expected WDP Data") );
		goto exit_label;
	}

	hResult = WapGetNextPrimitiveSize( hWapRecv, &dwWdpRecvDataSize);
	if ( FAILED(hResult) || (0 == dwWdpRecvDataSize) ) 
	{
		OutputDebugString( _T("WapGetNextPrimitiveSize() failed") );
		goto exit_label;
	}

	//  Allocate memory for storing received WDP Data
	pcbWdpRecvData =( WDP_UNITDATA* ) new BYTE[ dwWdpRecvDataSize ];
	if ( !pcbWdpRecvData) 
	{
		OutputDebugString( _T("Failed to allocate memory for storing WDP Unit Data") );
		goto exit_label;
	}

	//  Read WDP Data
	hResult = WapRead( hWapRecv, (WAP_PRIMITIVE_BASE *)pcbWdpRecvData, dwWdpRecvDataSize );
	if ( FAILED(hResult) ) 
	{
		OutputDebugString( _T("WapRead() failed") );
		goto exit_label;
	}

	// Validate the received primitive and
	// Compare the received user data to the sent user data
	if (!pcbWdpRecvData)
		goto exit_label;

	bSendReadOK = true;
	bSendReadOK &= (WAP_PRIMITIVE_ID_T_DUNITDATA == pcbWdpRecvData->wpiPrimitiveID);
	bSendReadOK &= (WAP_PRIMITIVE_TYPE_INDICATION == pcbWdpRecvData->wptPrimitiveType);
	bSendReadOK &= (pcbWdpRecvData->dwValidFields & WDP_FIELD_SOURCEADDRESS ) && ( pcbWdpRecvData->dwValidFields & WDP_FIELD_SOURCEPORT );
	bSendReadOK &= (pcbWdpRecvData->dwUserDataSize == Wdpunitdata.dwUserDataSize);
	if(bSendReadOK && pcbWdpRecvData->pbUserData)
	{
		bSendReadOK &= !memcmp(pcbWdpRecvData->pbUserData, Wdpunitdata.pbUserData, pcbWdpRecvData->dwUserDataSize);
	}

exit_label:

	//  Close WAP for Sending WDP UnitData
	if ( hWapSend) 
	{
		hResult = WapClose( hWapSend );
		if ( FAILED(hResult) ) 
		{
			OutputDebugString( _T("WapClose() for sending WDP UnitData failed") );
			bSendReadOK = false;
		} 
		else 
		{
			hWapSend = 0L;
		}
	}

	//  Close WAP for Receiving WDP UnitData
	if ( hWapRecv ) 
	{
		hResult = WapClose( hWapRecv );
		if ( FAILED(hResult) ) 
		{
			OutputDebugString( _T("WapClose() for reading WDP UnitData failed") );
			bSendReadOK = false;
		} 
		else 
		{
			hWapRecv = 0L;
		}
	}
	// Clear the buffers
	if (pbBuffer) 
	{
		delete[] (BYTE*) pbBuffer;
		pbBuffer = NULL;
	}

	if ( pcbWdpRecvData ) 
	{
		delete [] (BYTE*) pcbWdpRecvData;
		pcbWdpRecvData = NULL;
	}

	return bSendReadOK;
}
// CWapTestDlg 对话框

CWapTestDlg::CWapTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CWapTestDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CWapTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CWapTestDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
	ON_WM_SIZE()
#endif
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// CWapTestDlg 消息处理程序

BOOL CWapTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	WdpSendAndReadEcho(true);
	
	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}



你可能感兴趣的:(如何通过 WinCE 访问 WAP 数据)