mobile read sms 透過MAPI

转载自【http://sheng-yung.blogspot.com/2008/03/mobile-read-sms-mapi.html】

許多人可能再透過smsopen要讀取sms簡訊時

hr = SmsOpen(SMS_MSGTYPE_TEXT, SMS_MODE_RECEIVE , &hSMS,
&hMsgAvailable);

可能碰過說系統僅能存在一個實體來存取,我想應該是系統內預設的簡訊收件夾
那麼這時候要讀取簡訊就要透過MAPI來連取收件夾 讀取其內容

底下範例參考 microsoft mobile blog
僅提供讀取功能 而無NOTIFICATION

-------------------------------------------------------
設定 連接 cemapi.lib
-------------------------------------------------------
#include
#include
#include
#include

HRESULT SaveMessages(IMsgStore *pStore, LPCTSTR pszFilename); //讀取收件夾
HRESULT SaveSmsMessages(IMAPISession *pSession, LPCTSTR pszFilename); //開啟收件夾實體

HRESULT SaveSmsMessages(IMAPISession *pSession, LPCTSTR pszFilename)
{
static const SizedSPropTagArray (2, spta) = { 2, PR_DISPLAY_NAME, PR_ENTRYID };

HRESULT hr;
SRowSet *prowset = NULL;
CComPtr

ptbl;
CComPtr pStore;

// Get the table of accounts
hr = pSession->GetMsgStoresTable(0, &ptbl);
//CHR(hr);

// set the columns of the table we will query
hr = ptbl->SetColumns((SPropTagArray *) &spta, 0);
//CHR(hr);

while (TRUE)
{
// Free the previous row
FreeProws (prowset);
prowset = NULL;

hr = ptbl->QueryRows (1, 0, &prowset);
if ((hr != S_OK) || (prowset == NULL) || (prowset->cRows == 0))
break;

ASSERT (prowset->aRow[0].cValues == spta.cValues);
SPropValue *pval = prowset->aRow[0].lpProps;

ASSERT (pval[0].ulPropTag == PR_DISPLAY_NAME);
ASSERT (pval[1].ulPropTag == PR_ENTRYID);

if (!_tcscmp(pval[0].Value.lpszW, TEXT("SMS")))
{
// Get the Message Store pointer
hr = pSession->OpenMsgStore(0, pval[1].Value.bin.cb, (LPENTRYID)pval[1].Value.bin.lpb, 0, 0, &pStore);
//CHR(hr);

SaveMessages(pStore, pszFilename);
}
}

Error:
FreeProws (prowset);
return hr;
}

HRESULT SaveMessages(IMsgStore *pStore, LPCTSTR pszFilename)
{
static const SizedSSortOrderSet(1, sortOrderSet) = { 1, 0, 0, { PR_MESSAGE_DELIVERY_TIME, TABLE_SORT_DESCEND } };
static const SizedSPropTagArray (3, spta) = { 3, PR_SENDER_NAME, PR_SUBJECT, PR_MESSAGE_DELIVERY_TIME };
HRESULT hr = S_OK;
LPENTRYID pEntryId = NULL;
ULONG cbEntryId = 0;
CComPtr pFolder;
CComPtr ptbl;
ULONG ulObjType = 0;
SRowSet *prowset = NULL;

// 1 First retrieve the ENTRYID of the Inbox folder of the message store
// Get the inbox folder
hr = pStore->GetReceiveFolder(NULL, MAPI_UNICODE, &cbEntryId, &pEntryId, NULL);
//CHR(hr);

// 2 we have the entryid of the inbox folder, let's get the folder and messages in it
hr = pStore->OpenEntry(cbEntryId, pEntryId, NULL, 0, &ulObjType, (LPUNKNOWN*)&pFolder);
//CHR(hr);
ASSERT(ulObjType == MAPI_FOLDER);

// 3 From the IMAPIFolder pointer, obtain the table to the contents
hr = pFolder->GetContentsTable(0, &ptbl);
//CHR(hr);

// 4 Sort the table that we obtained. This is determined by the sortOrderSet variable
hr = ptbl->SortTable((SSortOrderSet *)&sortOrderSet, 0);
//CHR(hr);

// 5 Set the columns of the table we will query. The columns of each row are determined by spta
hr = ptbl->SetColumns ((SPropTagArray *) &spta, 0);
//CHR(hr);

// now iterate through each message in the table
while (TRUE)
{
// Free the previous row
FreeProws (prowset);
prowset = NULL;

hr = ptbl->QueryRows (1, 0, &prowset);
if ((hr != S_OK) || (prowset == NULL) || (prowset->cRows == 0))
break;

ASSERT (prowset->aRow[0].cValues == spta.cValues);
SPropValue *pval = prowset->aRow[0].lpProps;

// 6 Get the three properties we need: Sender name, Subject, and Delvery time.
ASSERT (pval[0].ulPropTag == PR_SENDER_NAME);
ASSERT (pval[1].ulPropTag == PR_SUBJECT);
ASSERT (pval[2].ulPropTag == PR_MESSAGE_DELIVERY_TIME);

LPCTSTR pszSender = pval[0].Value.lpszW;
LPCTSTR pszSubject = pval[1].Value.lpszW;
SYSTEMTIME st = {0};
FileTimeToSystemTime(&pval[2].Value.ft, &st);

// 7 Pass the parameters to a function to archive (this function is not written)
//hr = AppendToFile(pszFilename, pszSender, pszSubject, st);
AfxMessageBox(pszFilename);
AfxMessageBox(pszSender);
AfxMessageBox(pszSubject);
//CHR(hr);
}

Error:
FreeProws (prowset);
MAPIFreeBuffer(pEntryId);
return hr;
}

//按鈕按下即開始讀取
//事先必須先初始化建立MAPI, 最後並關閉
void CSYDlg::OnBnClickedButton1()
{
HRESULT hr;
ICEMAPISession * pSession = NULL;

hr = MAPIInitialize(NULL);
hr = MAPILogonEx(0, NULL, NULL, 0, (LPMAPISESSION *)&pSession);

SaveSmsMessages(pSession, L"TestFileName");

hr = pSession->Logoff(0, 0, 0);

pSession->Release();
pSession = NULL;
MAPIUninitialize();
}

你可能感兴趣的:(Blog,Microsoft,mobile)