c++

The day I failed at googling

I've accidentally created one small piece of WTF! code yesterday, while hacking some c++ / windows mobile mail checker utility.

I wanted to synchronize mailbox in pocket outlook, but for some unknown reason I missed one simple and elegant solution...

Just compare:
tmail.exe -service "netvor.sk" -sync

with my original CEMAPI solution...

HRESULT hr;
HINSTANCE hMailTrns;
ICEMAPISession *pSession = NULL;
IMAPITable * pTable = NULL;
IMAPITable * pMsgStoresTable = NULL;
IMsgStore *pStore = NULL;
SRowSet *pSRowSet = NULL;
SPropValue *pPropValue = NULL;
ULONG lCount = -1;
SizedSPropTagArray(2, PropTagArray) = { 2, PR_DISPLAY_NAME, PR_EMAIL_ADDRESS };
IMailSyncHandler * pIMailSyncHandler = NULL;
CMailSyncCallBack MailSyncCallBack;
ONESTOPFACTORYFUNC pOneStopFactoryFunc = NULL;
SYNCCREDENTIALS sc;
MAILSYNCREQUEST msr;

hMailTrns = LoadLibrary(_T("MailTrns.dll"));
if (hMailTrns == NULL)
  return false;
}

pOneStopFactoryFunc = (ONESTOPFACTORYFUNC)GetProcAddress(hMailTrns, _T("OneStopFactory"));
if (pOneStopFactoryFunc == NULL)
  return false;
}
if (FAILED((*pOneStopFactoryFunc)(L"IMAP4", &pIMailSyncHandler))) {
  return false;
}

MAPIInitialize(NULL);
MAPILogonEx(0, NULL, NULL, 0, (LPMAPISESSION*)&pSession);
hr = pSession->GetMsgStoresTable(0, &pTable);

while (!FAILED(hr)) {
  if (FAILED(hr = pTable->QueryRows(1, 0, &pSRowSet)))
    break;
  if (pSRowSet->cRows != 1)
    break;

  pSession->OpenMsgStore(NULL,
                         pSRowSet->aRow[0].lpProps[0].Value.bin.cb,
                         (ENTRYID*)pSRowSet->aRow[0].lpProps[0].Value.bin.lpb,
                         NULL, 0, &pStore);
  if (SUCCEEDED(pStore->GetProps((SPropTagArray*)&PropTagArray, 0, &lCount, &pPropValue))) {
    if (wcscmp(sName,pPropValue[0].Value.lpszW) == 0) {
      // match
      if (FAILED(pIMailSyncHandler->Initialize(&MailSyncCallBack,
                          pPropValue[0].Value.lpszW,pStore))) {
        return false;
      }

      ZeroMemory(&sc, sizeof(SYNCCREDENTIALS));
      sc.cbSize = sizeof(SYNCCREDENTIALS);
      sc.pszUsername = _T("username");
      sc.pszPassword = _T("password");
      sc.pszDomain = _T("domain");

      if (FAILED(pIMailSyncHandler->Connect(0, &sc))) {
        return false;
      }

      ZeroMemory(&msr, sizeof(MAILSYNCREQUEST));
      msr.cbSize = sizeof(MAILSYNCREQUEST);
      msr.ffFlags = SYNC_NORMAL;
     
      if (FAILED(pIMailSyncHandler->Synchronize(&msr))) {
        return false;
      }

      pIMailSyncHandler->Disconnect(0);
      pIMailSyncHandler->ShutDown(0);
    }
    // cleanup
    MAPIFreeBuffer(pPropValue);
  }
  //cleanup
  FreeProws(pSRowSet);
}
 johnny's blog   Add new comment    
Tags:  c++   hacks   PDA   pissed   WM6   wtf

Syndicate content
Π