예제 #1
0
HRESULT GetShortcutFolder(LPMAPISESSION lpSession, LPTSTR lpszFolderName, LPTSTR lpszFolderComment, ULONG ulFlags, LPMAPIFOLDER* lppShortcutFolder)
{
	HRESULT hr = hrSuccess;
	LPSPropValue lpPropValue = NULL;
	IMsgStore *lpMsgStore = NULL;
	IMAPIFolder *lpFolder = NULL;
	ULONG ulObjType = 0;

	hr = HrOpenDefaultStore(lpSession, &lpMsgStore);
	if(hr != hrSuccess)
		goto exit;

	// Get shortcut entryid
	hr = HrGetOneProp(lpMsgStore, PR_IPM_FAVORITES_ENTRYID, &lpPropValue);
	if(hr != hrSuccess) {

		if(hr == MAPI_E_NOT_FOUND && (ulFlags&MAPI_CREATE)) {
			// Propety not found, re-create the shortcut folder
			hr = CreateShortcutFolder(lpMsgStore, lpszFolderName, lpszFolderComment, ulFlags & MAPI_UNICODE, lppShortcutFolder);
		}
		goto exit;
	}

	// Open Shortcut folder
	hr = lpMsgStore->OpenEntry(lpPropValue->Value.bin.cb, (LPENTRYID)lpPropValue->Value.bin.lpb, &IID_IMAPIFolder, MAPI_BEST_ACCESS, &ulObjType, (LPUNKNOWN *)&lpFolder);
	if (hr != hrSuccess) {
		if(hr == MAPI_E_NOT_FOUND && (ulFlags&MAPI_CREATE)) {
			// Folder not found, re-create the shortcut folder
			hr = CreateShortcutFolder(lpMsgStore, lpszFolderName, lpszFolderComment, ulFlags & MAPI_UNICODE, lppShortcutFolder);
		}

		goto exit;
	}

	hr = lpFolder->QueryInterface(IID_IMAPIFolder, (void**)lppShortcutFolder);
	if (hr != hrSuccess)
		goto exit;

exit:
	if (lpPropValue)
		MAPIFreeBuffer(lpPropValue);

	if(lpFolder)
		lpFolder->Release();

	if(lpMsgStore)
		lpMsgStore->Release();

	return hr;
}
예제 #2
0
/**
 * Helper function to create a new shorcut folder and update the property 
 * PR_IPM_FAVORITES_ENTRYID on the store object.
 * If the folder already exist, it will used and update only the property
 *
 * @param lpMsgStore Pointer to the private store
 * @param lpszFolderName Pointer to a string containing the name for the shorcut folder. If NULL is passed, the folder has the default name 'Shortcut'.
 * @param lpszFolderComment Pointer to a string containing a comment associated with the shorcut folder. If NULL is passed, the folder has the default comment 'Shortcut folder'.
 * @param ulFlags MAPI_UNICODE if the folder strings are in unicode format, otherwise 0.
 * @param lppShortcutFolder Pointer to a pointer to the opened shortcut folder
 */
HRESULT CreateShortcutFolder(IMsgStore *lpMsgStore, LPTSTR lpszFolderName, LPTSTR lpszFolderComment, ULONG ulFlags, LPMAPIFOLDER* lppShortcutFolder)
{
	HRESULT hr = hrSuccess;
	IMAPIFolder *lpFolder = NULL;
	IMAPIFolder *lpNewFolder = NULL;
	ULONG ulObjType = 0;
	LPSPropValue lpProp = NULL;

	if (lpMsgStore == NULL || lppShortcutFolder == NULL) {
		hr = MAPI_E_INVALID_PARAMETER;
		goto exit;
	}

	if (lpszFolderName == NULL) {
		if (ulFlags & MAPI_UNICODE)
			lpszFolderName = (LPTSTR)L"Shortcut";
		else
			lpszFolderName = (LPTSTR)"Shortcut";
	}

	if (lpszFolderComment == NULL) {
		if (ulFlags & MAPI_UNICODE)
			lpszFolderComment = (LPTSTR)L"Shortcut folder";
		else
			lpszFolderComment = (LPTSTR)"Shortcut folder";
	}

	// Open root folder
	hr = lpMsgStore->OpenEntry(0, NULL, &IID_IMAPIFolder, MAPI_BEST_ACCESS, &ulObjType, (LPUNKNOWN *)&lpFolder);
	if (hr != hrSuccess)
		goto exit;

	hr = lpFolder->CreateFolder(FOLDER_GENERIC, lpszFolderName, lpszFolderComment, &IID_IMAPIFolder, ulFlags | OPEN_IF_EXISTS, &lpNewFolder);
	if (hr != hrSuccess)
		goto exit;

	hr = HrGetOneProp(lpNewFolder, PR_ENTRYID, &lpProp);
	if (hr != hrSuccess)
		goto exit;

	lpProp->ulPropTag = PR_IPM_FAVORITES_ENTRYID;

	hr = HrSetOneProp(lpMsgStore, lpProp);
	if (hr != hrSuccess)
		goto exit;

	hr = lpNewFolder->QueryInterface(IID_IMAPIFolder, (void**)lppShortcutFolder);
	if (hr != hrSuccess)
		goto exit;

exit:
	if (lpProp)
		MAPIFreeBuffer(lpProp);

	if (lpFolder)
		lpFolder->Release();

	if (lpNewFolder)
		lpNewFolder->Release();

	return hr;
}