From 8437c5cc0636009e0e88e3f18e4e2492abcad7e3 Mon Sep 17 00:00:00 2001 From: Vincent Lucas Date: Sat, 2 Mar 2013 20:30:39 +0000 Subject: Applies patch proposed by Ingo Bauersachs for jmsoutlookaddrbook.dkk to lock the MAPI session, correct mixing of char/wchar_t and set to NULL deleted pointers. Removes names (surname, firstname, company name, etc.) from the contact call items. The dll will be generated in the following commit. --- src/native/addrbook/msoutlook/MAPINotification.cxx | 32 +++- src/native/addrbook/msoutlook/MAPISession.cxx | 21 +++ src/native/addrbook/msoutlook/MAPISession.h | 5 + .../addrbook/msoutlook/MsOutlookAddrBook.sln | 20 +++ .../addrbook/msoutlook/MsOutlookAddrBook.vcxproj | 105 ++++++++++++ src/native/addrbook/msoutlook/MsOutlookDll.cxx | 28 +++ src/native/addrbook/msoutlook/MsOutlookDll.h | 26 +++ src/native/addrbook/msoutlook/MsOutlookMAPI.h | 6 +- .../msoutlook/MsOutlookMAPIHResultException.cxx | 26 +-- .../msoutlook/MsOutlookMAPIHResultException.h | 2 +- ...ook_msoutlook_MsOutlookAddrBookContactQuery.cxx | 73 +++++--- ...tlook_MsOutlookAddrBookContactSourceService.cxx | 189 ++++++++++----------- 12 files changed, 384 insertions(+), 149 deletions(-) create mode 100644 src/native/addrbook/msoutlook/MsOutlookAddrBook.sln create mode 100644 src/native/addrbook/msoutlook/MsOutlookAddrBook.vcxproj create mode 100644 src/native/addrbook/msoutlook/MsOutlookDll.cxx create mode 100644 src/native/addrbook/msoutlook/MsOutlookDll.h (limited to 'src/native/addrbook/msoutlook') diff --git a/src/native/addrbook/msoutlook/MAPINotification.cxx b/src/native/addrbook/msoutlook/MAPINotification.cxx index eed80e0..78c039f 100644 --- a/src/native/addrbook/msoutlook/MAPINotification.cxx +++ b/src/native/addrbook/msoutlook/MAPINotification.cxx @@ -169,7 +169,7 @@ STDAPICALLTYPE MAPINotification_onNotify { if(lpvContext != NULL) { - char entryIdStr[lpNotifications[i].info.obj.cbEntryID * 2 + 1]; + LPSTR entryIdStr = (LPSTR)::malloc(lpNotifications[i].info.obj.cbEntryID * 2 + 1); HexFromBin( (LPBYTE) lpNotifications[i].info.obj.lpEntryID, @@ -180,6 +180,9 @@ STDAPICALLTYPE MAPINotification_onNotify { MAPINotification_callDeletedMethod(entryIdStr); } + + ::free(entryIdStr); + entryIdStr = NULL; } } // A contact has been deleted (moved to trash). @@ -187,13 +190,14 @@ STDAPICALLTYPE MAPINotification_onNotify { if(lpvContext != NULL) { - char entryIdStr[lpNotifications[i].info.obj.cbEntryID * 2 + 1]; + LPSTR entryIdStr + = (LPSTR)::malloc(lpNotifications[i].info.obj.cbEntryID * 2 + 1); HexFromBin( (LPBYTE) lpNotifications[i].info.obj.lpEntryID, lpNotifications[i].info.obj.cbEntryID, entryIdStr); - char parentEntryIdStr[ - lpNotifications[i].info.obj.cbParentID * 2 + 1]; + LPSTR parentEntryIdStr + = (LPSTR)::malloc(lpNotifications[i].info.obj.cbParentID * 2 + 1); HexFromBin( (LPBYTE) lpNotifications[i].info.obj.lpParentID, lpNotifications[i].info.obj.cbParentID, @@ -206,8 +210,8 @@ STDAPICALLTYPE MAPINotification_onNotify MAPI_UNICODE, &wasteBasketNbValues, &wasteBasketProps); - char wasteBasketEntryIdStr[ - wasteBasketProps[0].Value.bin.cb * 2 + 1]; + LPSTR wasteBasketEntryIdStr + = (LPSTR)::malloc(wasteBasketProps[0].Value.bin.cb * 2 + 1); HexFromBin( (LPBYTE) wasteBasketProps[0].Value.bin.lpb, wasteBasketProps[0].Value.bin.cb, @@ -224,6 +228,13 @@ STDAPICALLTYPE MAPINotification_onNotify { MAPINotification_callDeletedMethod(entryIdStr); } + + ::free(entryIdStr); + entryIdStr = NULL; + ::free(parentEntryIdStr); + parentEntryIdStr = NULL; + ::free(wasteBasketEntryIdStr); + wasteBasketEntryIdStr = NULL; } } @@ -493,7 +504,8 @@ void MAPINotification_unregisterNotificationsDelegate(JNIEnv *jniEnv) MAPINotification_unregisterNotifyAllMsgStores(); if(MAPINotification_notificationsDelegateObject != NULL) { - jniEnv->DeleteGlobalRef( MAPINotification_notificationsDelegateObject); + jniEnv->DeleteGlobalRef(MAPINotification_notificationsDelegateObject); + MAPINotification_notificationsDelegateObject = NULL; MAPINotification_notificationsDelegateMethodIdInserted = NULL; MAPINotification_notificationsDelegateMethodIdUpdated = NULL; MAPINotification_notificationsDelegateMethodIdDeleted = NULL; @@ -517,7 +529,9 @@ void MAPINotification_unregisterNotifyAllMsgStores(void) } } free(MAPINotification_msgStoresConnection); + MAPINotification_msgStoresConnection = NULL; } + if(MAPINotification_msgStores != NULL) { for(unsigned int i = 0; i < MAPINotification_nbMsgStores; ++i) @@ -528,12 +542,14 @@ void MAPINotification_unregisterNotifyAllMsgStores(void) } } free(MAPINotification_msgStores); + MAPINotification_msgStores = NULL; } + if(MAPINotification_msgStoresTable != NULL) { MAPINotification_msgStoresTable->Unadvise( MAPINotification_msgStoresTableConnection); MAPINotification_msgStoresTable->Release(); + MAPINotification_msgStoresTable = NULL; } } - diff --git a/src/native/addrbook/msoutlook/MAPISession.cxx b/src/native/addrbook/msoutlook/MAPISession.cxx index 23b46b4..fb672b4 100644 --- a/src/native/addrbook/msoutlook/MAPISession.cxx +++ b/src/native/addrbook/msoutlook/MAPISession.cxx @@ -7,6 +7,7 @@ #include "MAPISession.h" static LPMAPISESSION MAPISession_mapiSession = NULL; +static CRITICAL_SECTION MAPISession_mapiSessionCriticalSection; /** * Returns the current mapi session which have been created using the @@ -31,3 +32,23 @@ void MAPISession_setMapiSession(LPMAPISESSION mapiSession) { MAPISession_mapiSession = mapiSession; } + +void MAPISession_initLock() +{ + InitializeCriticalSection(&MAPISession_mapiSessionCriticalSection); +} + +void MAPISession_lock() +{ + EnterCriticalSection(&MAPISession_mapiSessionCriticalSection); +} + +void MAPISession_unlock() +{ + LeaveCriticalSection(&MAPISession_mapiSessionCriticalSection); +} + +void MAPISession_freeLock() +{ + DeleteCriticalSection(&MAPISession_mapiSessionCriticalSection); +} diff --git a/src/native/addrbook/msoutlook/MAPISession.h b/src/native/addrbook/msoutlook/MAPISession.h index f4676cb..080435b 100644 --- a/src/native/addrbook/msoutlook/MAPISession.h +++ b/src/native/addrbook/msoutlook/MAPISession.h @@ -14,4 +14,9 @@ LPMAPISESSION MAPISession_getMapiSession(void); void MAPISession_setMapiSession(LPMAPISESSION mapiSession); +void MAPISession_initLock(); +void MAPISession_lock(); +void MAPISession_unlock(); +void MAPISession_freeLock(); + #endif /* #ifndef _NET_JAVA_SIP_COMMUNICATOR_PLUGIN_ADDRBOOK_MSOUTLOOK_MAPISESSION_H_ */ diff --git a/src/native/addrbook/msoutlook/MsOutlookAddrBook.sln b/src/native/addrbook/msoutlook/MsOutlookAddrBook.sln new file mode 100644 index 0000000..e9e795e --- /dev/null +++ b/src/native/addrbook/msoutlook/MsOutlookAddrBook.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MsOutlookAddrBook", "MsOutlookAddrBook.vcxproj", "{75EE17D1-65A7-4693-8774-FCBA8AF491A9}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {75EE17D1-65A7-4693-8774-FCBA8AF491A9}.Debug|Win32.ActiveCfg = Debug|Win32 + {75EE17D1-65A7-4693-8774-FCBA8AF491A9}.Debug|Win32.Build.0 = Debug|Win32 + {75EE17D1-65A7-4693-8774-FCBA8AF491A9}.Release|Win32.ActiveCfg = Release|Win32 + {75EE17D1-65A7-4693-8774-FCBA8AF491A9}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/src/native/addrbook/msoutlook/MsOutlookAddrBook.vcxproj b/src/native/addrbook/msoutlook/MsOutlookAddrBook.vcxproj new file mode 100644 index 0000000..73d3d94 --- /dev/null +++ b/src/native/addrbook/msoutlook/MsOutlookAddrBook.vcxproj @@ -0,0 +1,105 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {75EE17D1-65A7-4693-8774-FCBA8AF491A9} + Win32Proj + MsOutlookAddrBook + + + + DynamicLibrary + true + v110 + Unicode + + + DynamicLibrary + false + v110 + true + Unicode + + + + + + + + + + + + + true + $(SolutionDir)../../../../lib/native/windows/ + $(SolutionDir)../../native_obj/ + C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Include;%JDK_HOME%\include;%JDK_HOME%\include\Win32;$(IncludePath) + jmsoutlookaddrbook + + + false + + + + + + Level4 + Disabled + WIN32;_DEBUG;_WINDOWS;_USRDLL;MSOUTLOOKADDRBOOK_EXPORTS;%(PreprocessorDefinitions) + + + Windows + true + mapi32.lib;%(AdditionalDependencies) + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_WINDOWS;_USRDLL;MSOUTLOOKADDRBOOK_EXPORTS;%(PreprocessorDefinitions) + + + Windows + true + true + true + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/native/addrbook/msoutlook/MsOutlookDll.cxx b/src/native/addrbook/msoutlook/MsOutlookDll.cxx new file mode 100644 index 0000000..c3f10a3 --- /dev/null +++ b/src/native/addrbook/msoutlook/MsOutlookDll.cxx @@ -0,0 +1,28 @@ +/* + * Jitsi, the OpenSource Java VoIP and Instant Messaging client. + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ + +#include "MsOutlookDll.h" +#include "MAPISession.h" +#include + +JNIEXPORT BOOL APIENTRY DllMain( + HINSTANCE /*hDLL*/, + DWORD dwReason, + LPVOID /*lpReserved*/) +{ + switch (dwReason) + { + case DLL_PROCESS_ATTACH: + MAPISession_initLock(); + break; + case DLL_PROCESS_DETACH: + MAPISession_freeLock(); + break; + } + + return TRUE; +} diff --git a/src/native/addrbook/msoutlook/MsOutlookDll.h b/src/native/addrbook/msoutlook/MsOutlookDll.h new file mode 100644 index 0000000..97f8bc4 --- /dev/null +++ b/src/native/addrbook/msoutlook/MsOutlookDll.h @@ -0,0 +1,26 @@ +/* + * Jitsi, the OpenSource Java VoIP and Instant Messaging client. + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ + +#ifndef _MSOUTLOOKDLL_H +#define _MSOUTLOOKDLL_H +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +JNIEXPORT BOOL APIENTRY DllMain( + HINSTANCE hDLL, + DWORD dwReason, + LPVOID lpReserved); + +#ifdef __cplusplus +} +#endif + +#endif //_MSOUTLOOKDLL_H \ No newline at end of file diff --git a/src/native/addrbook/msoutlook/MsOutlookMAPI.h b/src/native/addrbook/msoutlook/MsOutlookMAPI.h index 7cf055f..29fc4a2 100644 --- a/src/native/addrbook/msoutlook/MsOutlookMAPI.h +++ b/src/native/addrbook/msoutlook/MsOutlookMAPI.h @@ -28,13 +28,13 @@ #include #include -WINBOOL MsOutlookAddrBook_fBinFromHex(LPTSTR lpsz, LPBYTE lpb); +BOOL MsOutlookAddrBook_fBinFromHex(LPSTR lpsz, LPBYTE lpb); #define FBinFromHex MsOutlookAddrBook_fBinFromHex void MsOutlookAddrBook_freeProws(LPSRowSet lpRows); #define FreeProws MsOutlookAddrBook_freeProws -void MsOutlookAddrBook_hexFromBin(LPBYTE pb, int cb, LPTSTR sz); +void MsOutlookAddrBook_hexFromBin(LPBYTE pb, int cb, LPSTR sz); #define HexFromBin MsOutlookAddrBook_hexFromBin void MsOutlookAddrBook_hrAllocAdviseSink @@ -56,7 +56,7 @@ ULONG MsOutlookAddrBook_mapiFreeBuffer(LPVOID buffer); HRESULT MsOutlookAddrBook_mapiLogonEx (ULONG_PTR uiParam, - LPSTR profileName, LPSTR password, + LPTSTR profileName, LPTSTR password, FLAGS flags, LPMAPISESSION FAR *mapiSession); #define MAPILogonEx MsOutlookAddrBook_mapiLogonEx diff --git a/src/native/addrbook/msoutlook/MsOutlookMAPIHResultException.cxx b/src/native/addrbook/msoutlook/MsOutlookMAPIHResultException.cxx index 8123b2a..9bb591c 100644 --- a/src/native/addrbook/msoutlook/MsOutlookMAPIHResultException.cxx +++ b/src/native/addrbook/msoutlook/MsOutlookMAPIHResultException.cxx @@ -9,7 +9,7 @@ void MsOutlookMAPIHResultException_throwNew - (JNIEnv *jniEnv, HRESULT hResult, LPCTSTR file, ULONG line) + (JNIEnv *jniEnv, HRESULT hResult, LPCSTR file, ULONG line) { jclass clazz; @@ -18,42 +18,42 @@ MsOutlookMAPIHResultException_throwNew "net/java/sip/communicator/plugin/addrbook/msoutlook/MsOutlookMAPIHResultException"); if (clazz) { - LPCTSTR message; + LPCSTR message; switch (hResult) { case MAPI_E_LOGON_FAILED: - message = _T("MAPI_E_LOGON_FAILED"); + message = "MAPI_E_LOGON_FAILED"; break; case MAPI_E_NO_ACCESS: - message = _T("MAPI_E_NO_ACCESS"); + message = "MAPI_E_NO_ACCESS"; break; case MAPI_E_NO_SUPPORT: - message = _T("MAPI_E_NO_SUPPORT"); + message = "MAPI_E_NO_SUPPORT"; break; case MAPI_E_NOT_ENOUGH_MEMORY: - message = _T("MAPI_E_NOT_ENOUGH_MEMORY"); + message = "MAPI_E_NOT_ENOUGH_MEMORY"; break; case MAPI_E_NOT_FOUND: - message = _T("MAPI_E_NOT_FOUND"); + message = "MAPI_E_NOT_FOUND"; break; case MAPI_E_NOT_INITIALIZED: - message = _T("MAPI_E_NOT_INITIALIZED"); + message = "MAPI_E_NOT_INITIALIZED"; break; case MAPI_E_TIMEOUT: - message = _T("MAPI_E_TIMEOUT"); + message = "MAPI_E_TIMEOUT"; break; case MAPI_E_UNKNOWN_ENTRYID: - message = _T("MAPI_E_UNKNOWN_ENTRYID"); + message = "MAPI_E_UNKNOWN_ENTRYID"; break; case MAPI_E_USER_CANCEL: - message = _T("MAPI_E_USER_CANCEL"); + message = "MAPI_E_USER_CANCEL"; break; case MAPI_W_ERRORS_RETURNED: - message = _T("MAPI_W_ERRORS_RETURNED"); + message = "MAPI_W_ERRORS_RETURNED"; break; case S_OK: - message = _T("S_OK"); + message = "S_OK"; break; default: message = NULL; diff --git a/src/native/addrbook/msoutlook/MsOutlookMAPIHResultException.h b/src/native/addrbook/msoutlook/MsOutlookMAPIHResultException.h index c0f7742..e84a7b1 100644 --- a/src/native/addrbook/msoutlook/MsOutlookMAPIHResultException.h +++ b/src/native/addrbook/msoutlook/MsOutlookMAPIHResultException.h @@ -17,7 +17,7 @@ extern "C" { #endif /* #ifdef __cplusplus */ void MsOutlookMAPIHResultException_throwNew - (JNIEnv *jniEnv, HRESULT hResult, LPCTSTR file, ULONG line); + (JNIEnv *jniEnv, HRESULT hResult, LPCSTR file, ULONG line); #ifdef __cplusplus } diff --git a/src/native/addrbook/msoutlook/net_java_sip_communicator_plugin_addrbook_msoutlook_MsOutlookAddrBookContactQuery.cxx b/src/native/addrbook/msoutlook/net_java_sip_communicator_plugin_addrbook_msoutlook_MsOutlookAddrBookContactQuery.cxx index 48e0394..5eae7ed 100644 --- a/src/native/addrbook/msoutlook/net_java_sip_communicator_plugin_addrbook_msoutlook_MsOutlookAddrBookContactQuery.cxx +++ b/src/native/addrbook/msoutlook/net_java_sip_communicator_plugin_addrbook_msoutlook_MsOutlookAddrBookContactQuery.cxx @@ -87,32 +87,39 @@ Java_net_java_sip_communicator_plugin_addrbook_msoutlook_MsOutlookAddrBookContac { jmethodID callbackMethodID; + MAPISession_lock(); LPMAPISESSION mapiSession = MAPISession_getMapiSession(); + if (!mapiSession) + { + MAPISession_unlock(); + return; + } callbackMethodID = AddrBookContactQuery_getPtrCallbackMethodID(jniEnv, callback); if (!callbackMethodID || jniEnv->ExceptionCheck()) - return; - - if (mapiSession) { - jboolean proceed - = MsOutlookAddrBookContactQuery_foreachContactInMsgStoresTable( - mapiSession, - jniEnv, - query, - callback, callbackMethodID); + MAPISession_unlock(); + return; + } - if (proceed && !(jniEnv->ExceptionCheck())) - { - MsOutlookAddrBookContactQuery_foreachMailUserInAddressBook( - mapiSession, - jniEnv, - query, - callback, callbackMethodID); - } + jboolean proceed + = MsOutlookAddrBookContactQuery_foreachContactInMsgStoresTable( + mapiSession, + jniEnv, + query, + callback, callbackMethodID); + if (proceed && !(jniEnv->ExceptionCheck())) + { + MsOutlookAddrBookContactQuery_foreachMailUserInAddressBook( + mapiSession, + jniEnv, + query, + callback, callbackMethodID); } + + MAPISession_unlock(); } /** @@ -274,7 +281,7 @@ Java_net_java_sip_communicator_plugin_addrbook_msoutlook_MsOutlookAddrBookContac propTag = MsOutlookAddrBookContactQuery_getPropTagFromLid( (LPMAPIPROP) mapiProp, - propId); + (LONG)propId); } *(propTagArray->aulPropTag + i) = propTag; } @@ -410,7 +417,7 @@ Java_net_java_sip_communicator_plugin_addrbook_msoutlook_MsOutlookAddrBookContac case PT_BINARY: { - char entryIdStr[prop->Value.bin.cb * 2 + 1]; + LPSTR entryIdStr = (LPSTR)::malloc(prop->Value.bin.cb * 2 + 1); HexFromBin( prop->Value.bin.lpb, @@ -427,6 +434,9 @@ Java_net_java_sip_communicator_plugin_addrbook_msoutlook_MsOutlookAddrBookContac if (jniEnv->ExceptionCheck()) props = NULL; } + + ::free(entryIdStr); + entryIdStr = NULL; break; } } @@ -484,7 +494,7 @@ Java_net_java_sip_communicator_plugin_addrbook_msoutlook_MsOutlookAddrBookContac const char *nativeValue = jniEnv->GetStringUTFChars(value, NULL); size_t valueLength = strlen(nativeValue); - wchar_t wCharValue[valueLength + 1]; + LPWSTR wCharValue = (LPWSTR)::malloc((valueLength + 1) * sizeof(wchar_t)); if(mbstowcs(wCharValue, nativeValue, valueLength + 1) != valueLength) { @@ -493,6 +503,8 @@ Java_net_java_sip_communicator_plugin_addrbook_msoutlook_MsOutlookAddrBookContac \n\tmbstowcs\n"); fflush(stderr); jniEnv->ReleaseStringUTFChars(value, nativeValue); + ::free(wCharValue); + wCharValue = NULL; return JNI_FALSE; } jniEnv->ReleaseStringUTFChars(value, nativeValue); @@ -594,11 +606,15 @@ Java_net_java_sip_communicator_plugin_addrbook_msoutlook_MsOutlookAddrBookContac if (HR_SUCCEEDED(hResult)) { ((LPMAPIPROP) mapiProp)->Release(); + ::free(wCharValue); + wCharValue = NULL; return JNI_TRUE; } } ((LPMAPIPROP) mapiProp)->Release(); + ::free(wCharValue); + wCharValue = NULL; return JNI_FALSE; } @@ -858,7 +874,7 @@ MsOutlookAddrBookContactQuery_foreachRowInTable if (objType && entryIDBinary.cb && entryIDBinary.lpb) { - LPENTRYID entryID; + LPENTRYID entryID = NULL; if (S_OK == MAPIAllocateBuffer( @@ -1128,9 +1144,9 @@ MsOutlookAddrBookContactQuery_onForeachContactInMsgStoresTableRow &msgStore); if (HR_SUCCEEDED(hResult)) { - LPENTRYID receiveFolderEntryID; - ULONG contactsFolderEntryIDByteCount; - LPENTRYID contactsFolderEntryID; + LPENTRYID receiveFolderEntryID = NULL; + ULONG contactsFolderEntryIDByteCount = 0; + LPENTRYID contactsFolderEntryID = NULL; hResult = msgStore->GetReceiveFolder( @@ -1259,11 +1275,12 @@ LPUNKNOWN MsOutlookAddrBookContactQuery_openEntryId(const char* entryId) &iUnknown); if(hResult == S_OK) { - free(tmpEntryId); + ::free(tmpEntryId); return iUnknown; } } - free(tmpEntryId); + + ::free(tmpEntryId); return NULL; } @@ -1281,7 +1298,7 @@ MsOutlookAddrBookContactQuery_readAttachment hResult = message->OpenAttach(num, NULL, 0, &attach); if (HR_SUCCEEDED(hResult)) { - IStream *stream; + IStream *stream = NULL; if (PT_BOOLEAN == PROP_TYPE(cond)) { @@ -1309,7 +1326,7 @@ MsOutlookAddrBookContactQuery_readAttachment 0, (LPUNKNOWN *) &stream); } - if (HR_SUCCEEDED(hResult)) + if (HR_SUCCEEDED(hResult) && stream) { STATSTG statstg; ULONGLONG length; diff --git a/src/native/addrbook/msoutlook/net_java_sip_communicator_plugin_addrbook_msoutlook_MsOutlookAddrBookContactSourceService.cxx b/src/native/addrbook/msoutlook/net_java_sip_communicator_plugin_addrbook_msoutlook_MsOutlookAddrBookContactSourceService.cxx index 0624357..fa37e95 100644 --- a/src/native/addrbook/msoutlook/net_java_sip_communicator_plugin_addrbook_msoutlook_MsOutlookAddrBookContactSourceService.cxx +++ b/src/native/addrbook/msoutlook/net_java_sip_communicator_plugin_addrbook_msoutlook_MsOutlookAddrBookContactSourceService.cxx @@ -17,9 +17,9 @@ #include #include -typedef BOOL (STDAPICALLTYPE *LPFBINFROMHEX)(LPTSTR, LPBYTE); +typedef BOOL (STDAPICALLTYPE *LPFBINFROMHEX)(LPSTR, LPBYTE); typedef void (STDAPICALLTYPE *LPFREEPROWS)(LPSRowSet); -typedef void (STDAPICALLTYPE *LPHEXFROMBIN)(LPBYTE, int, LPTSTR); +typedef void (STDAPICALLTYPE *LPHEXFROMBIN)(LPBYTE, int, LPSTR); typedef HRESULT (STDAPICALLTYPE *LPHRALLOCADVISESINK)(LPNOTIFCALLBACK, LPVOID, LPMAPIADVISESINK FAR *); typedef HRESULT (STDAPICALLTYPE *LPHRQUERYALLROWS)(LPMAPITABLE, LPSPropTagArray, LPSRestriction, LPSSortOrderSet, LONG, LPSRowSet FAR *); @@ -35,8 +35,7 @@ static LPMAPIINITIALIZE MsOutlookAddrBookContactSourceService_mapiInitialize; static LPMAPILOGONEX MsOutlookAddrBookContactSourceService_mapiLogonEx; static LPMAPIUNINITIALIZE MsOutlookAddrBookContactSourceService_mapiUninitialize; -static CRITICAL_SECTION - MsOutlookAddrBookContactSourceService_mapiSessionCriticalSection; +static HMODULE hMapiLib = NULL; static jboolean MsOutlookAddrBookContactSourceService_isValidDefaultMailClient @@ -44,7 +43,7 @@ MsOutlookAddrBookContactSourceService_isValidDefaultMailClient JNIEXPORT void JNICALL Java_net_java_sip_communicator_plugin_addrbook_msoutlook_MsOutlookAddrBookContactSourceService_MAPIInitialize - (JNIEnv *jniEnv, jclass clazz, jlong version, jlong flags, + (JNIEnv *jniEnv, jclass /*clazz*/, jlong version, jlong flags, jobject notificationsDelegate) { HKEY regKey; @@ -129,7 +128,7 @@ Java_net_java_sip_communicator_plugin_addrbook_msoutlook_MsOutlookAddrBookContac pathValue = installRootKeyName; else { - pathValue = (TCHAR *) malloc(pathValueSize); + pathValue = (LPTSTR)::malloc(pathValueSize); if (!pathValue) continue; } @@ -151,7 +150,7 @@ Java_net_java_sip_communicator_plugin_addrbook_msoutlook_MsOutlookAddrBookContac str = pathValue + (pathValueLength - 1); if (*str) str++; - memcpy(str, "\\Outlook.exe", 12 * sizeof(TCHAR)); + memcpy(str, _T("\\Outlook.exe"), 12 * sizeof(TCHAR)); *(str + 12) = 0; fileAttributes = GetFileAttributes(pathValue); @@ -280,21 +279,52 @@ Java_net_java_sip_communicator_plugin_addrbook_msoutlook_MsOutlookAddrBookContac // If we've determined that we'd like to go on with MAPI, try to load it. if (HR_SUCCEEDED(hResult)) { - HMODULE lib = ::LoadLibrary(_T("mapi32.dll")); + hMapiLib = ::LoadLibrary(_T("mapi32.dll")); hResult = MAPI_E_NO_SUPPORT; - if (lib) + if(hMapiLib) { + // get and check function pointers MsOutlookAddrBookContactSourceService_mapiInitialize - = (LPMAPIINITIALIZE) GetProcAddress(lib, "MAPIInitialize"); + = (LPMAPIINITIALIZE) GetProcAddress(hMapiLib, "MAPIInitialize"); MsOutlookAddrBookContactSourceService_mapiUninitialize - = (LPMAPIUNINITIALIZE) GetProcAddress(lib, "MAPIUninitialize"); + = (LPMAPIUNINITIALIZE) + GetProcAddress(hMapiLib, "MAPIUninitialize"); + MsOutlookAddrBookContactSourceService_mapiAllocateBuffer + = (LPMAPIALLOCATEBUFFER) + GetProcAddress(hMapiLib, "MAPIAllocateBuffer"); + MsOutlookAddrBookContactSourceService_mapiFreeBuffer + = (LPMAPIFREEBUFFER) GetProcAddress(hMapiLib, "MAPIFreeBuffer"); + MsOutlookAddrBookContactSourceService_mapiLogonEx + = (LPMAPILOGONEX) GetProcAddress(hMapiLib, "MAPILogonEx"); + MsOutlookAddrBookContactSourceService_fBinFromHex + = (LPFBINFROMHEX) GetProcAddress(hMapiLib, "FBinFromHex@8"); + MsOutlookAddrBookContactSourceService_freeProws + = (LPFREEPROWS) GetProcAddress(hMapiLib, "FreeProws@4"); + MsOutlookAddrBookContactSourceService_hexFromBin + = (LPHEXFROMBIN) GetProcAddress(hMapiLib, "HexFromBin@12"); + MsOutlookAddrBookContactSourceService_hrAllocAdviseSink + = (LPHRALLOCADVISESINK) + GetProcAddress(hMapiLib, "HrAllocAdviseSink@12"); + MsOutlookAddrBookContactSourceService_hrQueryAllRows + = (LPHRQUERYALLROWS) + GetProcAddress(hMapiLib, "HrQueryAllRows@24"); if (MsOutlookAddrBookContactSourceService_mapiInitialize - && MsOutlookAddrBookContactSourceService_mapiUninitialize) + && MsOutlookAddrBookContactSourceService_mapiUninitialize + && MsOutlookAddrBookContactSourceService_mapiAllocateBuffer + && MsOutlookAddrBookContactSourceService_mapiFreeBuffer + && MsOutlookAddrBookContactSourceService_mapiLogonEx + && MsOutlookAddrBookContactSourceService_fBinFromHex + && MsOutlookAddrBookContactSourceService_freeProws + && MsOutlookAddrBookContactSourceService_hexFromBin + && MsOutlookAddrBookContactSourceService_hrAllocAdviseSink + && MsOutlookAddrBookContactSourceService_hrQueryAllRows) { MAPIINIT_0 mapiInit = { (ULONG) version, (ULONG) flags }; + // Opening MAPI changes the working directory. Make a backup of + // the current directory, login to MAPI and restore it DWORD dwSize = ::GetCurrentDirectory(0, NULL); if (dwSize > 0) { @@ -304,93 +334,53 @@ Java_net_java_sip_communicator_plugin_addrbook_msoutlook_MsOutlookAddrBookContac = ::GetCurrentDirectory(dwSize, lpszWorkingDir); if (dwResult != 0) { + MAPISession_lock(); hResult = MsOutlookAddrBookContactSourceService_mapiInitialize( &mapiInit); ::SetCurrentDirectory(lpszWorkingDir); + + if(HR_SUCCEEDED(hResult) + && MAPISession_getMapiSession() == NULL) + { + LPMAPISESSION mapiSession = NULL; + hResult = MsOutlookAddrBook_mapiLogonEx( + 0, + NULL, NULL, + MAPI_EXTENDED + | MAPI_NO_MAIL + | MAPI_USE_DEFAULT, + &mapiSession); + MAPINotification_registerNotificationsDelegate( + jniEnv, + mapiSession, + notificationsDelegate); + } + MAPISession_unlock(); } else { hResult = HRESULT_FROM_WIN32(::GetLastError()); } + ::free(lpszWorkingDir); } else { hResult = HRESULT_FROM_WIN32(::GetLastError()); } - - if (HR_SUCCEEDED(hResult)) - { - MsOutlookAddrBookContactSourceService_mapiAllocateBuffer - = (LPMAPIALLOCATEBUFFER) - GetProcAddress(lib, "MAPIAllocateBuffer"); - MsOutlookAddrBookContactSourceService_mapiFreeBuffer - = (LPMAPIFREEBUFFER) - GetProcAddress(lib, "MAPIFreeBuffer"); - MsOutlookAddrBookContactSourceService_mapiLogonEx - = (LPMAPILOGONEX) GetProcAddress(lib, "MAPILogonEx"); - - MsOutlookAddrBookContactSourceService_fBinFromHex - = (LPFBINFROMHEX) GetProcAddress(lib, "FBinFromHex@8"); - MsOutlookAddrBookContactSourceService_freeProws - = (LPFREEPROWS) GetProcAddress(lib, "FreeProws@4"); - MsOutlookAddrBookContactSourceService_hexFromBin - = (LPHEXFROMBIN) GetProcAddress(lib, "HexFromBin@12"); - MsOutlookAddrBookContactSourceService_hrAllocAdviseSink - = (LPHRALLOCADVISESINK) - GetProcAddress(lib, "HrAllocAdviseSink@12"); - MsOutlookAddrBookContactSourceService_hrQueryAllRows - = (LPHRQUERYALLROWS) - GetProcAddress(lib, "HrQueryAllRows@24"); - - InitializeCriticalSection( - &MsOutlookAddrBookContactSourceService_mapiSessionCriticalSection); - - if (MsOutlookAddrBookContactSourceService_mapiAllocateBuffer - && MsOutlookAddrBookContactSourceService_mapiFreeBuffer - && MsOutlookAddrBookContactSourceService_mapiLogonEx - - - && MsOutlookAddrBookContactSourceService_fBinFromHex - && MsOutlookAddrBookContactSourceService_freeProws - && MsOutlookAddrBookContactSourceService_hexFromBin - && MsOutlookAddrBookContactSourceService_hrAllocAdviseSink - && - MsOutlookAddrBookContactSourceService_hrQueryAllRows) - { - hResult = S_OK; - } - else - { - MsOutlookAddrBookContactSourceService_mapiUninitialize(); - hResult = MAPI_E_NO_SUPPORT; - } - } } - if (HR_FAILED(hResult)) - FreeLibrary(lib); } } - if(HR_SUCCEEDED(hResult) && MAPISession_getMapiSession() == NULL) - { - LPMAPISESSION mapiSession = NULL; - hResult - = MsOutlookAddrBook_mapiLogonEx( - 0, - NULL, NULL, - MAPI_EXTENDED | MAPI_NO_MAIL | MAPI_USE_DEFAULT, - &mapiSession); - MAPINotification_registerNotificationsDelegate( - jniEnv, - mapiSession, - notificationsDelegate); - } - - /* Report any possible error regardless of where it has come from. */ if (HR_FAILED(hResult)) { + if (hMapiLib) + { + FreeLibrary(hMapiLib); + hMapiLib = NULL; + } + /* Report any possible error regardless of where it has come from. */ MsOutlookMAPIHResultException_throwNew( jniEnv, hResult, @@ -400,28 +390,37 @@ Java_net_java_sip_communicator_plugin_addrbook_msoutlook_MsOutlookAddrBookContac JNIEXPORT void JNICALL Java_net_java_sip_communicator_plugin_addrbook_msoutlook_MsOutlookAddrBookContactSourceService_MAPIUninitialize - (JNIEnv *jniEnv, jclass clazz) + (JNIEnv *jniEnv, jclass /*clazz*/) { - EnterCriticalSection( - &MsOutlookAddrBookContactSourceService_mapiSessionCriticalSection); + MAPISession_lock(); LPMAPISESSION mapiSession = MAPISession_getMapiSession(); if(mapiSession != NULL) { MAPINotification_unregisterNotificationsDelegate(jniEnv); mapiSession->Logoff(0, 0, 0); mapiSession->Release(); - mapiSession = NULL; + MAPISession_setMapiSession(NULL); } - LeaveCriticalSection( - &MsOutlookAddrBookContactSourceService_mapiSessionCriticalSection); MsOutlookAddrBookContactSourceService_mapiUninitialize(); - DeleteCriticalSection( - &MsOutlookAddrBookContactSourceService_mapiSessionCriticalSection); + MsOutlookAddrBookContactSourceService_mapiInitialize = NULL; + MsOutlookAddrBookContactSourceService_mapiUninitialize = NULL; + MsOutlookAddrBookContactSourceService_mapiAllocateBuffer = NULL; + MsOutlookAddrBookContactSourceService_mapiFreeBuffer = NULL; + MsOutlookAddrBookContactSourceService_mapiLogonEx = NULL; + MsOutlookAddrBookContactSourceService_fBinFromHex = NULL; + MsOutlookAddrBookContactSourceService_freeProws = NULL; + MsOutlookAddrBookContactSourceService_hexFromBin = NULL; + MsOutlookAddrBookContactSourceService_hrAllocAdviseSink = NULL; + MsOutlookAddrBookContactSourceService_hrQueryAllRows = NULL; + ::FreeLibrary(hMapiLib); + hMapiLib = NULL; + + MAPISession_unlock(); } -WINBOOL MsOutlookAddrBook_fBinFromHex(LPTSTR lpsz, LPBYTE lpb) +BOOL MsOutlookAddrBook_fBinFromHex(LPSTR lpsz, LPBYTE lpb) { return MsOutlookAddrBookContactSourceService_fBinFromHex(lpsz, lpb); } @@ -431,7 +430,7 @@ void MsOutlookAddrBook_freeProws(LPSRowSet lpRows) MsOutlookAddrBookContactSourceService_freeProws(lpRows); } -void MsOutlookAddrBook_hexFromBin(LPBYTE pb, int cb, LPTSTR sz) +void MsOutlookAddrBook_hexFromBin(LPBYTE pb, int cb, LPSTR sz) { return MsOutlookAddrBookContactSourceService_hexFromBin(pb, cb, sz); } @@ -478,14 +477,13 @@ MsOutlookAddrBook_mapiFreeBuffer(LPVOID buffer) HRESULT MsOutlookAddrBook_mapiLogonEx (ULONG_PTR uiParam, - LPSTR profileName, LPSTR password, + LPTSTR profileName, LPTSTR password, FLAGS flags, LPMAPISESSION FAR *mapiSession) { HRESULT hResult; - EnterCriticalSection( - &MsOutlookAddrBookContactSourceService_mapiSessionCriticalSection); + MAPISession_lock(); LPMAPISESSION currentMapiSession = MAPISession_getMapiSession(); if (currentMapiSession != NULL) hResult = S_OK; @@ -519,15 +517,14 @@ MsOutlookAddrBook_mapiLogonEx { hResult = HRESULT_FROM_WIN32(::GetLastError()); } - - - } + if (HR_SUCCEEDED(hResult)) + { *mapiSession = currentMapiSession; - LeaveCriticalSection( - &MsOutlookAddrBookContactSourceService_mapiSessionCriticalSection); + } + MAPISession_unlock(); return hResult; } -- cgit v1.1