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    
Tags:  c++   hacks   PDA   pissed   WM6   wtf

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can use BBCode tags in the text.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Π