none
Who can show me the snippet to Open Store via C++ MAPI RRS feed

  • Question

  • I want to OpenStore my Outlook via MAPI C++

    // MAPISample.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <string>
    #include <stdio.h>
    #include <iostream>
    #include <Windows.h>	//Must use
    #include "mapi.h"		//Must use
    #include "mapix.h"		//Must use
    
    using namespace std;
    
    void ListProfile(HMODULE & hMod,LPMAPILOGONEX &pfnMAPILogonEx,LPMAPISESSION pfnSession);
    bool LogOn(HMODULE & hMod,LPMAPILOGONEX &pfnMAPILogonEx,LPMAPISESSION &pfnSession);
    void ListStores(HMODULE & hMod,LPMAPISESSION & pfnSession);
    bool OpenStore(HMODULE & hMod,LPMAPISESSION & pfnSession);
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	LPMAPIINITIALIZE pfnMAPIInitialize = NULL;
    	LPMAPIUNINITIALIZE pfnMAPIUninitialize = NULL;
    	HMODULE hMod = NULL;
    	hMod = LoadLibrary(L"MAPI32.dll");
    	pfnMAPIInitialize = (LPMAPIINITIALIZE)GetProcAddress(hMod, "MAPIInitialize");
        pfnMAPIUninitialize = (LPMAPIUNINITIALIZE)GetProcAddress(hMod, "MAPIUninitialize");
    	HRESULT hr = NULL;
    	hr = (*pfnMAPIInitialize)(NULL);
    	if(SUCCEEDED(hr))
    	{
    		cout<<"Success Initialize"<<endl;
    		LPMAPILOGONEX pfnMAPILogonEx = NULL;
    		LPMAPISESSION pfnSession = NULL;
    		pfnMAPILogonEx = (LPMAPILOGONEX)GetProcAddress(hMod,"MAPILogonEx");
    		bool flag = true;
    		while(flag)
    		{
    			//system("cls"); 
    			cout<<"1 : List Profile"<<endl;
    			cout<<"2 : Log In by Profile"<<endl;
    			cout<<"3 : List Stores"<<endl;
    			cout<<"4 : Open Stores"<<endl;
    			cout<<"0 : Exit"<<endl;
    			cout<<"Select :";
    			int s = 0;
    			cin>>s;
    			string StoreName = "";
    			switch(s)
    			{
    			case 0:
    				flag = false;
    				break;
    			case 1:
    				ListProfile(hMod,pfnMAPILogonEx,pfnSession);
    				break;
    			case 2:
    				if(LogOn(hMod,pfnMAPILogonEx,pfnSession))
    				{
    					cout<<"--------------------------"<<endl;
    					cout<<"opne session successfully!"<<endl;
    					cout<<"--------------------------"<<endl;
    				}
    				else
    				{
    					cout<<"---------------------"<<endl;
    					cout<<"Can not open session!"<<endl;
    					cout<<"---------------------"<<endl;
    				}
    				break;
    			case 3:
    				ListStores(hMod,pfnSession);
    				break;
    			case 4:
    				cout<<"Store Name : ";
    				cin>>StoreName;
    				cout<<StoreName<<endl;
    				break;
    			default:
    				flag = false;
    				break;
    			}
    		}
    	}
    	(*pfnMAPIUninitialize)();
    	FreeLibrary(hMod);
    	system("Pause");
    	return 0;
    }
    
    void ListProfile(HMODULE & hMod,LPMAPILOGONEX &pfnMAPILogonEx,LPMAPISESSION pfnSession)
    {
    	HRESULT hr = NULL;
    	hr = (*pfnMAPILogonEx)(0,NULL,NULL,MAPI_EXPLICIT_PROFILE,&pfnSession);
    	if(SUCCEEDED(hr))
    	{
    		LPMAPIADMINPROFILES pfnMAPIAdminProfiles = NULL;
    		pfnMAPIAdminProfiles = (LPMAPIADMINPROFILES)GetProcAddress(hMod,"MAPIAdminProfiles");
    		LPPROFADMIN pfnProfAdmin = NULL;
    		hr=(*pfnMAPIAdminProfiles)(NULL,&pfnProfAdmin);
    		if(SUCCEEDED(hr))
    		{
    			LPMAPITABLE pfnProfileTable = NULL;
    			pfnProfAdmin->GetProfileTable(NULL,&pfnProfileTable);
    			ULONG max;
    			hr = pfnProfileTable->GetRowCount(NULL,&max);
    			if(SUCCEEDED(hr))
    			{
    				LPSRowSet rows = NULL;
    				pfnProfileTable->QueryRows((LONG)max,NULL,&rows);
    				cout<<"---------------------------"<<endl;
    				cout<<"Profile Name\tIs Default"<<endl;
    				cout<<"---------------------------"<<endl;
    				for(int i = 0;i < (int)rows->cRows;i++)
    				{
    					cout<<rows->aRow[i].lpProps[0].Value.lpszA<<"\t\t";
    					if(rows->aRow[i].lpProps[1].Value.b)
    						cout<<"True"<<endl;
    					else
    						cout<<"False"<<endl;
    				}
    				cout<<"---------------------------"<<endl;
    			}
    			pfnSession->Release();
    			pfnSession = NULL;
    		}
    	}
    }
    
    bool LogOn(HMODULE & hMod,LPMAPILOGONEX &pfnMAPILogonEx,LPMAPISESSION &pfnSession)
    {
    	bool result = false;
    	LPTSTR profile = NULL;
    	HRESULT hr = NULL;
    	hr = (*pfnMAPILogonEx)(NULL,NULL,NULL,MAPI_EXTENDED|MAPI_LOGON_UI|MAPI_NEW_SESSION|MAPI_EXPLICIT_PROFILE,&pfnSession);
    	if(SUCCEEDED(hr))
    		result = true;
    	return result;
    }
    
    void ListStores(HMODULE & hMod,LPMAPISESSION & pfnSession)
    {
    	HRESULT hr = NULL;
    	LPMAPITABLE msgTable = NULL;
    	hr = pfnSession->GetMsgStoresTable(NULL,&msgTable);
    	if(SUCCEEDED(hr))
    	{
    		ULONG count;
    		hr = msgTable->GetRowCount(0,&count);
    		LPSRowSet rows = NULL;
    		hr = msgTable->QueryRows((LONG)count,NULL,&rows);
    		cout<<"-------------------------------------------"<<endl;
    		cout<<"Stores Display Name"<<endl;
    		cout<<"-------------------------------------------"<<endl;
    		for(int i = 0;i < (int)count;i++)
    		{
    			cout<<rows->aRow[i].lpProps[3].Value.lpszA<<"";
    			if(rows->aRow[i].lpProps[7].Value.b)
    			{
    				cout<<"(Default)"<<endl;
    			}
    			else
    			{
    				cout<<endl;
    			}
    		}
    		cout<<"-------------------------------------------"<<endl;
    	}
    	else
    	{
    		cout<<"Can not get Stores"<<endl;
    	}
    }
    
    bool OpenStore(HMODULE & hMod,LPMAPISESSION & pfnSession)
    {
    	bool result = false;
    	HRESULT hr = NULL;
    	
    	return result;
    }

    However, I failed at OpenStore method. Who can tell me how to do it?

    Have a good day,

    Ice

    Thursday, March 1, 2012 9:19 AM

Answers

  • Hi Ice,

    Thanks for posting in the MSDN Forum.

    I think this snippet will help you.

    bool OpenStore(HMODULE & hMod,LPMAPISESSION & pfnSession,int Index)
    {
    	bool result = false;
    	HRESULT hr = NULL;
    	LPMAPITABLE msgTable = NULL;
    	hr = pfnSession->GetMsgStoresTable(NULL,&msgTable);
    	LPMDB lpMDB;
    	if(SUCCEEDED(hr))
    	{
    		ULONG count;
    		hr = msgTable->GetRowCount(0,&count);
    		LPSRowSet rows = NULL;
    		hr = msgTable->QueryRows((LONG)count,NULL,&rows);
    		if(SUCCEEDED(hr))
    		{
    			hr = pfnSession->OpenMsgStore(NULL,
    				rows->aRow[Index].lpProps[0].Value.bin.cb,
    				(LPENTRYID)rows->aRow[Index].lpProps[0].Value.bin.lpb,
    				NULL,MAPI_BEST_ACCESS,&lpMDB);
    			if(SUCCEEDED(hr))
    			{
    				cout<<"OK"<<endl;
    			}
    		}
    	}
    	else
    	{
    		cout<<"Can not get Stores"<<endl;
    	}
    	return result;
    }

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, March 6, 2012 5:06 AM
    Moderator