Click here to Skip to main content
15,912,507 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRe: CreateCompatibleDC(NULL) == CreateCompatibleDC(hdc) for some hdc? Pin
Randor 14-Jun-09 5:44
professional Randor 14-Jun-09 5:44 
AnswerRe: CreateCompatibleDC(NULL) == CreateCompatibleDC(hdc) for some hdc? Pin
japcrword14-Jun-09 6:24
japcrword14-Jun-09 6:24 
GeneralRe: CreateCompatibleDC(NULL) == CreateCompatibleDC(hdc) for some hdc? Pin
Randor 14-Jun-09 6:39
professional Randor 14-Jun-09 6:39 
GeneralRe: CreateCompatibleDC(NULL) == CreateCompatibleDC(hdc) for some hdc? [modified] Pin
japcrword14-Jun-09 7:12
japcrword14-Jun-09 7:12 
GeneralRe: CreateCompatibleDC(NULL) == CreateCompatibleDC(hdc) for some hdc? Pin
Randor 14-Jun-09 8:03
professional Randor 14-Jun-09 8:03 
GeneralRe: CreateCompatibleDC(NULL) == CreateCompatibleDC(hdc) for some hdc? Pin
japcrword14-Jun-09 8:55
japcrword14-Jun-09 8:55 
GeneralRe: CreateCompatibleDC(NULL) == CreateCompatibleDC(hdc) for some hdc? Pin
Randor 14-Jun-09 9:34
professional Randor 14-Jun-09 9:34 
QuestionPlease solve my problem its very urgent Pin
santhosh-padamatinti14-Jun-09 1:23
santhosh-padamatinti14-Jun-09 1:23 
Hi,
I am trying to display the attachments for message which is inside Outlook. There i am retriving the mails from inbox also attachments are displaying, but when I am trying to save in the hard disc it is not working. When I am tried to debug the solution i am getting error at Istream interface please give me solution. I am sending attach.cpp code, it is just win32 console application


/////////////////////////////////////////////////////////////////////////////////////
////////////////////file name:getattach.cpp//////////////////////////////////////
#include
#include
#include <mapix.h>
#include
#include <mapiutil.h>
#define _tcscpy strcpy
#define _tcscmp strcmp

STDMETHODIMP OpenDefaultMessageStore(LPMAPISESSION lpMAPISession,LPMDB * lpMDB);
STDMETHODIMP OpenInbox(LPMDB lpMDB,LPMAPIFOLDER *lpInboxFolder);
STDMETHODIMP ListMessages(LPMDB lpMDB,LPMAPIFOLDER lpInboxFolder);

int main()
{

HRESULT hRes;
LPMAPIFOLDER lpInboxFolder = NULL;
LPMAPISESSION lpMAPISession = NULL;
LPMDB lpMDB = NULL;
LPSPropValue tmp = NULL;
LPMESSAGE lpMessage = NULL;

IMAPISession *pIMapi = NULL;

if(MAPIInitialize(NULL) != S_OK)
return FALSE;

hRes = MAPILogonEx(0,NULL,NULL,MAPI_LOGON_UI, &lpMAPISession);
if (FAILED(hRes))
return FALSE;

hRes = OpenDefaultMessageStore(lpMAPISession,&lpMDB);
if (FAILED(hRes))
return FALSE;

hRes = OpenInbox(lpMDB,&lpInboxFolder);
if (FAILED(hRes))
return FALSE;

hRes = HrGetOneProp(lpInboxFolder,PR_DISPLAY_NAME,&tmp);
if (FAILED(hRes))
return FALSE;
printf("I managed to open the folder '%s'\n",tmp->Value.lpszA);


hRes = ListMessages(lpMDB,lpInboxFolder);
if (FAILED(hRes))
return FALSE;

if(!lpMessage)
return FALSE;

IMAPITable *pAttachTable = NULL;
hRes = lpMessage->GetAttachmentTable(0, &pAttachTable);

}

/////////////////////////////////////////////////////////////////////////////////////////
/* open message store table, This step we are opening the
default message store table.*////////////



STDMETHODIMP OpenDefaultMessageStore(LPMAPISESSION lpMAPISession,LPMDB * lpMDB)
{
LPMAPITABLE pStoresTbl = NULL;
LPSRowSet pRow = NULL;
static SRestriction sres;
SPropValue spv;
HRESULT hRes;
LPMDB lpTempMDB = NULL;

enum {EID, NAME, NUM_COLS};
static SizedSPropTagArray(NUM_COLS,sptCols) = {NUM_COLS, PR_ENTRYID, PR_DISPLAY_NAME};

*lpMDB = NULL;


hRes = lpMAPISession -> GetMsgStoresTable(0, &pStoresTbl);
if (FAILED(hRes))
return FALSE;


sres.rt = RES_PROPERTY;
sres.res.resProperty.relop = RELOP_EQ;
sres.res.resProperty.ulPropTag = PR_DEFAULT_STORE;
sres.res.resProperty.lpProp = &spv;

spv.ulPropTag = PR_DEFAULT_STORE;
spv.Value.b = TRUE;


hRes = HrQueryAllRows(pStoresTbl,(LPSPropTagArray) &sptCols,&sres,NULL,0,&pRow);
if (FAILED(hRes))
goto quit;


hRes = lpMAPISession->OpenMsgStore(NULL,pRow->aRow[0].lpProps[EID].Value.bin.cb,
(LPENTRYID)pRow->aRow[0].lpProps[EID].Value.bin.lpb,NULL,MAPI_BEST_ACCESS,
&lpTempMDB);
if (FAILED(hRes))
goto quit;

*lpMDB = lpTempMDB;

quit: //quit label

FreeProws(pRow);
UlRelease(pStoresTbl);
if (FAILED(hRes))
{
HRESULT hr;
LPMAPIERROR lpError;
hr = lpMAPISession->GetLastError(hRes,0,&lpError);
if (!hr)
{
printf("%s\n%s\n",lpError->lpszError,lpError->lpszComponent);
MAPIFreeBuffer(lpError);
}
}
return hRes;

}


//////* CLOSE open message store table, Now we are opened default message store*////////////

/////////////////////////////////////////////////////////////////////////////////////////
/////////////////* Now in this we have to open inbox*///////////////////////////////////



STDMETHODIMP OpenInbox(LPMDB lpMDB, LPMAPIFOLDER *lpInboxFolder)
{
ULONG cbInbox;
LPENTRYID lpbInbox;
ULONG ulObjType;
HRESULT hRes = S_OK;
LPMAPIFOLDER lpTempFolder = NULL;

*lpInboxFolder = NULL;
hRes = lpMDB->GetReceiveFolder(NULL,NULL,&cbInbox,&lpbInbox,NULL);
if (FAILED(hRes))
goto quit;
hRes = lpMDB->OpenEntry(cbInbox,lpbInbox,NULL,MAPI_BEST_ACCESS,
&ulObjType,(LPUNKNOWN *) &lpTempFolder);
if (FAILED(hRes))
goto quit;

*lpInboxFolder = lpTempFolder;

quit:
MAPIFreeBuffer(lpbInbox);
return hRes;
}



//////////////* Now sucessfully opened the INBOX *//////////////////
///////////////////////////////////////////////////////////////////
///////////* List the messages which is inside INBOX*/////////////


STDMETHODIMP ListMessages(LPMDB lpMDB,LPMAPIFOLDER lpInboxFolder)
{
HRESULT hRes = S_OK;
LPMAPITABLE lpContentsTable = NULL;
LPSRowSet pRows = NULL;
LPSTREAM lpStream = NULL;
ULONG i;
enum
{
ePR_SENT_REPRESENTING_NAME,
ePR_SUBJECT,
ePR_BODY,
ePR_PRIORITY,
ePR_ENTRYID,
NUM_COLS
};
static SizedSPropTagArray(NUM_COLS,sptCols) =
{ NUM_COLS, PR_SENT_REPRESENTING_NAME, PR_SUBJECT,
PR_BODY, PR_PRIORITY, PR_ENTRYID };

hRes = lpInboxFolder->GetContentsTable(0,&lpContentsTable);
if (FAILED(hRes))
goto quit;

hRes = HrQueryAllRows(lpContentsTable,(LPSPropTagArray) &sptCols,NULL,NULL,0,&pRows);
if (FAILED(hRes))
goto quit;

for (i = 0; i < pRows -> cRows; i++)
{
LPMESSAGE lpMessage = NULL;
ULONG ulObjType = NULL;
LPSPropValue lpProp = NULL;

printf("Message %d:\n",i);
if (PR_SENT_REPRESENTING_NAME == pRows -> aRow[i].lpProps[ePR_SENT_REPRESENTING_NAME].ulPropTag)
{
printf("From: %s\n",pRows->aRow[i].lpProps[ePR_SENT_REPRESENTING_NAME].Value.lpszA);
}
if (PR_SUBJECT == pRows -> aRow[i].lpProps[ePR_SUBJECT].ulPropTag)
{
printf("Subject: %s\n",pRows->aRow[i].lpProps[ePR_SUBJECT].Value.lpszA);
}
if (PR_PRIORITY == pRows -> aRow[i].lpProps[ePR_PRIORITY].ulPropTag)
{
printf("Priority: %d\n",pRows->aRow[i].lpProps[ePR_PRIORITY].Value.l);
}
/////////////////////body///////////////////////////////////////////////////////

if (MAPI_E_NOT_FOUND != pRows -> aRow[i].lpProps[ePR_BODY].Value.l)
{
hRes = lpMDB->OpenEntry(pRows->aRow[i].lpProps[ePR_ENTRYID].Value.bin.cb,
(LPENTRYID) pRows->aRow[i].lpProps[ePR_ENTRYID].Value.bin.lpb,NULL,
MAPI_BEST_ACCESS,&ulObjType,(LPUNKNOWN *) &lpMessage);
if (!FAILED(hRes))
{
hRes = HrGetOneProp(lpMessage,PR_BODY,&lpProp);
if (hRes == MAPI_E_NOT_ENOUGH_MEMORY)
{
char szBuf[255];
ULONG ulNumChars;
hRes = lpMessage->OpenProperty(PR_BODY,&IID_IStream,STGM_READ,NULL,
(LPUNKNOWN *) &lpStream);

do
{
lpStream->Read(szBuf,255,&ulNumChars);
if (ulNumChars >0) printf("%.*s",ulNumChars,szBuf);
}
while (ulNumChars >= 255);

printf("\n");

hRes = S_OK;
}
else if (hRes == MAPI_E_NOT_FOUND)
{
printf("Message has no body!\n");
hRes = S_OK;
}
else
{
printf("Body: %s\n",lpProp->Value.lpszA);
}

}



//////////////////////////////////////////////////////////////////////////////
///////start for displaying a attachment/////////////////////////////////////



if(!lpMessage)
goto a;

IMAPITable *pAttachTable = NULL;
hRes = lpMessage->GetAttachmentTable(0, &pAttachTable);

SRowSet *pAttachRowSet = NULL;
SizedSPropTagArray(3, tblAttachColumns)={3,{PR_ATTACH_NUM,PR_ATTACH_SIZE,PR_ATTACH_FILENAME}};

pAttachTable->SetColumns((LPSPropTagArray)&tblAttachColumns, 0);
hRes = pAttachTable->QueryRows(1, 0, &pAttachRowSet);
if(pAttachRowSet->cRows != 1)
goto a;

long lAttachNum = 0;
DWORD dwAttachSize = 0;
TCHAR tchAttachName[MAX_PATH] = TEXT("\0");

lAttachNum = pAttachRowSet->aRow[0].lpProps[0].Value.l;
dwAttachSize = pAttachRowSet->aRow[0].lpProps[1].Value.ul;

char *ptr=tchAttachName;
const char *ptr1=(char*)pAttachRowSet->aRow[0].lpProps[2].Value.lpszW;
_tcscpy(ptr,ptr1);

printf("attachment name is:",ptr1,"\n");
IAttach *pAttach = NULL;

hRes = lpMessage->OpenAttach(lAttachNum, 0, 0, &pAttach);
if(FAILED(hRes))
goto a;

IStream *pAttachStream = NULL;
HANDLE hAttachFile = NULL;
TCHAR tchFilePath[MAX_PATH] = TEXT("\0");

wsprintf(tchFilePath, TEXT("\\%s"), tchAttachName);
hAttachFile = CreateFile(tchFilePath, GENERIC_WRITE, 0, NULL,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
hRes=pAttach->OpenProperty(PR_ATTACH_DATA_BIN, NULL, 0, 0,(IUnknown **)&pAttachStream);

if(hAttachFile == INVALID_HANDLE_VALUE || FAILED(hRes))
goto a;


LPVOID lpBuffer = NULL;
DWORD dwRead = 0, dwWritten = 0;
lpBuffer = (LPVOID)LocalAlloc(LPTR, 4096);

do
{
memset(lpBuffer, 0, 4096);
pAttachStream->Read(lpBuffer, 4096, &dwRead);
// actual problem here, Because pattachstream is storing null, giving a debug error.

if(dwRead > 0)
WriteFile(hAttachFile, lpBuffer, dwRead, &dwWritten,NULL);
} while(dwRead > 0);

CloseHandle(hAttachFile);
LocalFree(lpBuffer);
FreeProws(pAttachRowSet);

}

a:
MAPIFreeBuffer(lpProp);
UlRelease(lpMessage);
hRes = S_OK;

}
quit:
FreeProws(pRows);
UlRelease(lpContentsTable);
return hRes;
}

sampath-padamatinti

AnswerThe best way to make urgent question answered Pin
CPallini14-Jun-09 21:54
mveCPallini14-Jun-09 21:54 
QuestionRe: Please solve my problem its very urgent Pin
David Crow15-Jun-09 3:02
David Crow15-Jun-09 3:02 
AnswerRe: Please solve my problem its very urgent Pin
Randor 15-Jun-09 6:33
professional Randor 15-Jun-09 6:33 
Questionchar* as return type in dll Pin
_808613-Jun-09 23:05
_808613-Jun-09 23:05 
AnswerRe: char* as return type in dll Pin
Stuart Dootson14-Jun-09 2:26
professionalStuart Dootson14-Jun-09 2:26 
GeneralRe: char* as return type in dll Pin
_808614-Jun-09 4:13
_808614-Jun-09 4:13 
GeneralRe: char* as return type in dll Pin
_808614-Jun-09 4:21
_808614-Jun-09 4:21 
GeneralRe: char* as return type in dll Pin
0x3c014-Jun-09 6:44
0x3c014-Jun-09 6:44 
QuestionWhere does VC9 store its run time type info? Pin
followait13-Jun-09 16:55
followait13-Jun-09 16:55 
AnswerRe: Where does VC9 store its run time type info? Pin
harold aptroot13-Jun-09 22:44
harold aptroot13-Jun-09 22:44 
AnswerRe: Where does VC9 store its run time type info? Pin
Stuart Dootson14-Jun-09 2:26
professionalStuart Dootson14-Jun-09 2:26 
QuestionPassing CDialog results to CFormView ?? Pin
Vaclav_13-Jun-09 15:45
Vaclav_13-Jun-09 15:45 
AnswerRe: Passing CDialog results to CFormView ?? Pin
Stuart Dootson14-Jun-09 2:21
professionalStuart Dootson14-Jun-09 2:21 
AnswerRe: Passing CDialog results to CFormView ?? Pin
krmed14-Jun-09 4:23
krmed14-Jun-09 4:23 
GeneralRe: Passing CDialog results to CFormView SOLVED Pin
Vaclav_14-Jun-09 14:10
Vaclav_14-Jun-09 14:10 
QuestionEncryption / Decryption Pin
queries36513-Jun-09 10:01
queries36513-Jun-09 10:01 
QuestionDoubt - C [modified] Pin
musiclover9113-Jun-09 8:10
musiclover9113-Jun-09 8:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.