|
这个是短信接收的例子:
- #include "StdAfx.h"
- #include
- #pragma comment (lib, "sms.lib")
- int WINAPI WinMain (HINSTANCE hInstance,
- HINSTANCE hPreviousInstance,
- LPWSTR pszCommandLine,
- int nCommandShow)
- {
- SMS_HANDLE smshHandle = 0;
- SMS_MESSAGE_ID smsmidMessageID = 0;
- HRESULT hRet = 0;
- SMS_HANDLE smshRecieveHandle;
- DWORD dwReturn = 0;
- HANDLE hSmsEvent;
- dwReturn = SmsOpen(SMS_MSGTYPE_TEXT, SMS_MODE_RECEIVE, &smshRecieveHandle, &hSmsEvent);
-
- // 程序会在此等待模块来短信,直到超时或者有短信来到
- dwReturn = WaitForSingleObject(hSmsEvent, INFINITE);
- // 判断是否成功接收到短信
- if(dwReturn == WAIT_ABANDONED || dwReturn == WAIT_TIMEOUT)
- {
- return FALSE;
- }
- // 成功接收到短信,先设置好短信大小
- DWORD dwMessageSize = 1024;
- // 初始化短信结构
- SMS_ADDRESS smscAddress;
- SMS_ADDRESS inAddress;
- SYSTEMTIME rcvTime;
- TEXT_PROVIDER_SPECIFIC_DATA txtProviderData;
- DWORD dwProviderLength = 0;
- dwProviderLength = sizeof(TEXT_PROVIDER_SPECIFIC_DATA);
- memset(&txtProviderData, 0, dwProviderLength);
- memset(&smscAddress, 0, sizeof(SMS_ADDRESS));
- memset(&inAddress, 0, sizeof(SMS_ADDRESS));
- memset(&rcvTime, 0, sizeof(SYSTEMTIME));
- // 创建一个buffer来存放短信
- TCHAR *tchMsgBuffer = NULL;
- tchMsgBuffer = (TCHAR *)LocalAlloc(LPTR,
- dwMessageSize*sizeof(TCHAR));
- if(!tchMsgBuffer)
- {
- SmsClose(smshHandle);
- SmsClose(smshRecieveHandle);
- return FALSE;
- }
- // 把短信从模块里读出来放到buffer里,执行完以后,tchMsgBuffer里存放的就是接收到短信的UNICODE编码
- DWORD dwBytesRead = 0;
- hRet = SmsReadMessage(
- smshRecieveHandle,
- &smscAddress,
- &inAddress,
- &rcvTime,
- (LPBYTE)tchMsgBuffer,
- dwMessageSize,
- (LPBYTE)&txtProviderData,
- dwProviderLength,
- &dwBytesRead
- );
- if(FAILED(hRet))
- {
- LocalFree(tchMsgBuffer);
- tchMsgBuffer = NULL;
- SmsClose(smshHandle);
- SmsClose(smshRecieveHandle);
- return FALSE;
- }
- if (smshHandle != NULL)
- {
- SmsClose(smshHandle);
- SmsClose(smshRecieveHandle);
- smshHandle = NULL;
- }
- return TRUE;
- }
复制代码 |
|