To implement an offline state add-in, you must implement connection, initialization, and other setup functions. In this topic, these connection, initialization, and setup functions are demonstrated by using code examples from the Sample Offline State Add-in. The Sample Offline State Add-in is a COM add-in that adds an Offline State menu to Outlook and uses the Offline State API. Through the Offline State menu, you can enable or disable state monitoring, check the current state, and change the current state. For more information about downloading and installing the Sample Offline State Add-in, see Installing the Sample Offline State Add-in. For more information about the Offline State API, see About the Offline State API.
The IDTExtensibility2.OnConnection Method is called every time an add-in is loaded. It is the entry point for the add-in, so the code you put in the OnConnection function will be called when the add-in starts. In the following example, the OnConnection function calls the HrInitAddin function.
// Get the system directory path
// (mapistub.dll and mapi32.dll reside here)
uiRet = GetSystemDirectoryA(szSystemDir, MAX_PATH);
if (uiRet > 0)
{
CHAR szDLLPath[MAX_PATH+1] = {0};
hRes = StringCchPrintfA(szDLLPath, MAX_PATH+1, "%s\\%s",
szSystemDir, "mapistub.dll");
if (SUCCEEDED(hRes))
{
LPFGETCOMPONENTPATH pfnFGetComponentPath = NULL;
// Load mapistub.dll
hModMAPIStub = LoadLibraryA(szDLLPath);
if (hModMAPIStub)
{
// Get the address of FGetComponentPath from the mapistub
pfnFGetComponentPath = (LPFGETCOMPONENTPATH)GetProcAddress(
hModMAPIStub, "FGetComponentPath");
}
// If there is no address for FGetComponentPath yet
// try getting it from mapi32.dll
if (!pfnFGetComponentPath)
{
hRes = StringCchPrintfA(szDLLPath, MAX_PATH+1, "%s\\%s",
szSystemDir, "mapi32.dll");
if (SUCCEEDED(hRes))
{
// Load mapi32.dll
hModMAPI = LoadLibraryA(szDLLPath);
if (hModMAPI)
{
// Get the address of FGetComponentPath from mapi32
pfnFGetComponentPath = (LPFGETCOMPONENTPATH)GetProcAddress(
hModMAPI, "FGetComponentPath");
}
}
}
if (pfnFGetComponentPath)
{
LPSTR szAppLCID = NULL;
LPSTR szOfficeLCID = NULL;
HKEY hMicrosoftOutlook = NULL;
lRet = RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
_T("Software\\Clients\\Mail\\Microsoft Outlook"),
NULL,
KEY_READ,
&hMicrosoftOutlook);
if (ERROR_SUCCESS == lRet && hMicrosoftOutlook)
{
HrGetRegMultiSZValueA(hMicrosoftOutlook, "MSIApplicationLCID", (LPVOID*) &szAppLCID);
HrGetRegMultiSZValueA(hMicrosoftOutlook, "MSIOfficeLCID", (LPVOID*) &szOfficeLCID);
}
if (szAppLCID)
{
bRet = pfnFGetComponentPath(
"{FF1D0740-D227-11D1-A4B0-006008AF820E}", szAppLCID, szDLLPath, MAX_PATH, true);
}
if ((!bRet || szDLLPath[0] == _T('\0')) && szOfficeLCID)
{
bRet = pfnFGetComponentPath(
"{FF1D0740-D227-11D1-A4B0-006008AF820E}", szOfficeLCID, szDLLPath, MAX_PATH, true);
}
if (!bRet || szDLLPath[0] == _T('\0'))
{
bRet = pfnFGetComponentPath(
"{FF1D0740-D227-11D1-A4B0-006008AF820E}", NULL, szDLLPath, MAX_PATH, true);
}
delete[] szOfficeLCID;
delete[] szAppLCID;
if (hMicrosoftOutlook) RegCloseKey(hMicrosoftOutlook);
}
hModMSMAPI = LoadLibrary(szDLLPath);
if (hModMSMAPI)
{
pfnHrOpenOfflineObj = (HROPENOFFLINEOBJ*) GetProcAddress(
hModMSMAPI,
"HrOpenOfflineObj@20");
pfnMAPIFreeBuffer = (LPMAPIFREEBUFFER) GetProcAddress(
hModMSMAPI,
"MAPIFreeBuffer");
}
}
}
}
Cache Profile Name Routine
The HrCacheProfileName function calls the IMAPISupport::OpenProfileSection function to open a profile section for the current session, and then sets the profile for the button handlers.
if (SUCCEEDED(hRes) && lpPSGlobal)
{
LPSPropValue lpProfileName = NULL;
// Asking for PR_PROFILE_NAME_W here - this might not work with earlier versions of Outlook
SPropTagArray staga = {1,PR_PROFILE_NAME_W};
ULONG cVal = 0;
hRes = lpPSGlobal->GetProps(&staga,0,&cVal,&lpProfileName);
if (SUCCEEDED(hRes) && 1 == cVal && lpProfileName && PR_PROFILE_NAME_W == lpProfileName->ulPropTag)
{
m_InitButtonHandler.SetProfile(lpProfileName->Value.lpszW);
m_GetStateButtonHandler.SetProfile(lpProfileName->Value.lpszW);
m_SetStateButtonHandler.SetProfile(lpProfileName->Value.lpszW);
}
if (pfnMAPIFreeBuffer) pfnMAPIFreeBuffer(lpProfileName);
}
if (lpPSGlobal) lpPSGlobal->Release();
}
if (lpMAPISession) lpMAPISession->Release();
return hRes;
}
Add Menu Items Routine
The HrAddMenuItems function defines the menu options that appear under the Offline State menu that is created when the add-in is loaded in Outlook, and then calls DispEventAdvise for each menu item.