2 * MAPI Utility functions
4 * Copyright 2004 Jon Griffiths
5 * Copyright 2009 Owen Rudge for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
35 #include "wine/debug.h"
36 #include "wine/unicode.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(mapi
);
44 static const BYTE digitsToHex
[] = {
45 0,1,2,3,4,5,6,7,8,9,0xff,0xff,0xff,0xff,0xff,0xff,0xff,10,11,12,13,14,15,
46 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
47 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,10,11,12,13,
50 MAPI_FUNCTIONS mapiFunctions
;
52 /**************************************************************************
53 * ScInitMapiUtil (MAPI32.33)
55 * Initialise Mapi utility functions.
58 * ulReserved [I] Reserved, pass 0.
61 * Success: S_OK. Mapi utility functions may be called.
62 * Failure: MAPI_E_INVALID_PARAMETER, if ulReserved is not 0.
65 * Your application does not need to call this function unless it does not
66 * call MAPIInitialize()/MAPIUninitialize().
68 SCODE WINAPI
ScInitMapiUtil(ULONG ulReserved
)
70 if (mapiFunctions
.ScInitMapiUtil
)
71 return mapiFunctions
.ScInitMapiUtil(ulReserved
);
73 FIXME("(0x%08x)stub!\n", ulReserved
);
75 return MAPI_E_INVALID_PARAMETER
;
79 /**************************************************************************
80 * DeinitMapiUtil (MAPI32.34)
82 * Uninitialise Mapi utility functions.
91 * Your application does not need to call this function unless it does not
92 * call MAPIInitialize()/MAPIUninitialize().
94 VOID WINAPI
DeinitMapiUtil(void)
96 if (mapiFunctions
.DeinitMapiUtil
)
97 mapiFunctions
.DeinitMapiUtil();
102 typedef LPVOID
*LPMAPIALLOCBUFFER
;
104 /**************************************************************************
105 * MAPIAllocateBuffer (MAPI32.12)
106 * MAPIAllocateBuffer@8 (MAPI32.13)
108 * Allocate a block of memory.
111 * cbSize [I] Size of the block to allocate in bytes
112 * lppBuffer [O] Destination for pointer to allocated memory
115 * Success: S_OK. *lppBuffer is filled with a pointer to a memory block of
116 * length cbSize bytes.
117 * Failure: MAPI_E_INVALID_PARAMETER, if lppBuffer is NULL.
118 * MAPI_E_NOT_ENOUGH_MEMORY, if the memory allocation fails.
121 * Memory allocated with this function should be freed with MAPIFreeBuffer().
122 * Further allocations of memory may be linked to the pointer returned using
123 * MAPIAllocateMore(). Linked allocations are freed when the initial pointer
126 SCODE WINAPI
MAPIAllocateBuffer(ULONG cbSize
, LPVOID
*lppBuffer
)
128 LPMAPIALLOCBUFFER lpBuff
;
130 TRACE("(%d,%p)\n", cbSize
, lppBuffer
);
132 if (mapiFunctions
.MAPIAllocateBuffer
)
133 return mapiFunctions
.MAPIAllocateBuffer(cbSize
, lppBuffer
);
138 lpBuff
= HeapAlloc(GetProcessHeap(), 0, cbSize
+ sizeof(*lpBuff
));
140 return MAPI_E_NOT_ENOUGH_MEMORY
;
142 TRACE("initial allocation:%p, returning %p\n", lpBuff
, lpBuff
+ 1);
148 /**************************************************************************
149 * MAPIAllocateMore (MAPI32.14)
150 * MAPIAllocateMore@12 (MAPI32.15)
152 * Allocate a block of memory linked to a previous allocation.
155 * cbSize [I] Size of the block to allocate in bytes
156 * lpOrig [I] Initial allocation to link to, from MAPIAllocateBuffer()
157 * lppBuffer [O] Destination for pointer to allocated memory
160 * Success: S_OK. *lppBuffer is filled with a pointer to a memory block of
161 * length cbSize bytes.
162 * Failure: MAPI_E_INVALID_PARAMETER, if lpOrig or lppBuffer is invalid.
163 * MAPI_E_NOT_ENOUGH_MEMORY, if memory allocation fails.
166 * Memory allocated with this function and stored in *lppBuffer is freed
167 * when lpOrig is passed to MAPIFreeBuffer(). It should not be freed independently.
169 SCODE WINAPI
MAPIAllocateMore(ULONG cbSize
, LPVOID lpOrig
, LPVOID
*lppBuffer
)
171 LPMAPIALLOCBUFFER lpBuff
= lpOrig
;
173 TRACE("(%d,%p,%p)\n", cbSize
, lpOrig
, lppBuffer
);
175 if (mapiFunctions
.MAPIAllocateMore
)
176 return mapiFunctions
.MAPIAllocateMore(cbSize
, lpOrig
, lppBuffer
);
178 if (!lppBuffer
|| !lpBuff
|| !--lpBuff
)
181 /* Find the last allocation in the chain */
184 TRACE("linked:%p->%p\n", lpBuff
, *lpBuff
);
188 if (SUCCEEDED(MAPIAllocateBuffer(cbSize
, lppBuffer
)))
190 *lpBuff
= ((LPMAPIALLOCBUFFER
)*lppBuffer
) - 1;
191 TRACE("linking %p->%p\n", lpBuff
, *lpBuff
);
193 return *lppBuffer
? S_OK
: MAPI_E_NOT_ENOUGH_MEMORY
;
196 /**************************************************************************
197 * MAPIFreeBuffer (MAPI32.16)
198 * MAPIFreeBuffer@4 (MAPI32.17)
200 * Free a block of memory and any linked allocations associated with it.
203 * lpBuffer [I] Memory to free, returned from MAPIAllocateBuffer()
208 ULONG WINAPI
MAPIFreeBuffer(LPVOID lpBuffer
)
210 LPMAPIALLOCBUFFER lpBuff
= lpBuffer
;
212 TRACE("(%p)\n", lpBuffer
);
214 if (mapiFunctions
.MAPIFreeBuffer
)
215 return mapiFunctions
.MAPIFreeBuffer(lpBuffer
);
217 if (lpBuff
&& --lpBuff
)
221 LPVOID lpFree
= lpBuff
;
225 TRACE("linked:%p->%p, freeing %p\n", lpFree
, lpBuff
, lpFree
);
226 HeapFree(GetProcessHeap(), 0, lpFree
);
232 /**************************************************************************
233 * WrapProgress@20 (MAPI32.41)
235 HRESULT WINAPI
WrapProgress(PVOID unk1
, PVOID unk2
, PVOID unk3
, PVOID unk4
, PVOID unk5
)
237 /* Native does not implement this function */
238 return MAPI_E_NO_SUPPORT
;
241 /*************************************************************************
242 * HrDispatchNotifications@4 (MAPI32.239)
244 HRESULT WINAPI
HrDispatchNotifications(ULONG flags
)
246 FIXME("(%08x)\n", flags
);
250 /*************************************************************************
251 * HrThisThreadAdviseSink@8 (MAPI32.42)
253 * Ensure that an advise sink is only notified in its originating thread.
256 * lpSink [I] IMAPIAdviseSink interface to be protected
257 * lppNewSink [I] Destination for wrapper IMAPIAdviseSink interface
260 * Success: S_OK. *lppNewSink contains a new sink to use in place of lpSink.
261 * Failure: E_INVALIDARG, if any parameter is invalid.
263 HRESULT WINAPI
HrThisThreadAdviseSink(LPMAPIADVISESINK lpSink
, LPMAPIADVISESINK
* lppNewSink
)
265 if (mapiFunctions
.HrThisThreadAdviseSink
)
266 return mapiFunctions
.HrThisThreadAdviseSink(lpSink
, lppNewSink
);
268 FIXME("(%p,%p)semi-stub\n", lpSink
, lppNewSink
);
270 if (!lpSink
|| !lppNewSink
)
273 /* Don't wrap the sink for now, just copy it */
274 *lppNewSink
= lpSink
;
275 IMAPIAdviseSink_AddRef(lpSink
);
279 /*************************************************************************
280 * FBinFromHex (MAPI32.44)
282 * Create an array of binary data from a string.
285 * lpszHex [I] String to convert to binary data
286 * lpOut [O] Destination for resulting binary data
289 * Success: TRUE. lpOut contains the decoded binary data.
290 * Failure: FALSE, if lpszHex does not represent a binary string.
293 * - lpOut must be at least half the length of lpszHex in bytes.
294 * - Although the Mapi headers prototype this function as both
295 * Ascii and Unicode, there is only one (Ascii) implementation. This
296 * means that lpszHex is treated as an Ascii string (i.e. a single NUL
297 * character in the byte stream terminates the string).
299 BOOL WINAPI
FBinFromHex(LPWSTR lpszHex
, LPBYTE lpOut
)
301 LPSTR lpStr
= (LPSTR
)lpszHex
;
303 TRACE("(%p,%p)\n", lpszHex
, lpOut
);
307 if (lpStr
[0] < '0' || lpStr
[0] > 'f' || digitsToHex
[lpStr
[0] - '0'] == 0xff ||
308 lpStr
[1] < '0' || lpStr
[1] > 'f' || digitsToHex
[lpStr
[1] - '0'] == 0xff)
311 *lpOut
++ = (digitsToHex
[lpStr
[0] - '0'] << 4) | digitsToHex
[lpStr
[1] - '0'];
317 /*************************************************************************
318 * HexFromBin (MAPI32.45)
320 * Create a string from an array of binary data.
323 * lpHex [I] Binary data to convert to string
324 * iCount [I] Length of lpHex in bytes
325 * lpszOut [O] Destination for resulting hex string
331 * - lpszOut must be at least 2 * iCount + 1 bytes characters long.
332 * - Although the Mapi headers prototype this function as both
333 * Ascii and Unicode, there is only one (Ascii) implementation. This
334 * means that the resulting string is not properly NUL terminated
335 * if the caller expects it to be a Unicode string.
337 void WINAPI
HexFromBin(LPBYTE lpHex
, int iCount
, LPWSTR lpszOut
)
339 static const char hexDigits
[] = { "0123456789ABCDEF" };
340 LPSTR lpStr
= (LPSTR
)lpszOut
;
342 TRACE("(%p,%d,%p)\n", lpHex
, iCount
, lpszOut
);
346 *lpStr
++ = hexDigits
[*lpHex
>> 4];
347 *lpStr
++ = hexDigits
[*lpHex
& 0xf];
353 /*************************************************************************
354 * SwapPlong@8 (MAPI32.47)
356 * Swap the bytes in a ULONG array.
359 * lpData [O] Array to swap bytes in
360 * ulLen [I] Number of ULONG element to swap the bytes of
365 VOID WINAPI
SwapPlong(PULONG lpData
, ULONG ulLen
)
369 for (i
= 0; i
< ulLen
; i
++)
370 lpData
[i
] = RtlUlongByteSwap(lpData
[i
]);
373 /*************************************************************************
374 * SwapPword@8 (MAPI32.48)
376 * Swap the bytes in a USHORT array.
379 * lpData [O] Array to swap bytes in
380 * ulLen [I] Number of USHORT element to swap the bytes of
385 VOID WINAPI
SwapPword(PUSHORT lpData
, ULONG ulLen
)
389 for (i
= 0; i
< ulLen
; i
++)
390 lpData
[i
] = RtlUshortByteSwap(lpData
[i
]);
393 /**************************************************************************
394 * MNLS_lstrlenW@4 (MAPI32.62)
396 * Calculate the length of a Unicode string.
399 * lpszStr [I] String to calculate the length of
402 * The length of lpszStr in Unicode characters.
404 ULONG WINAPI
MNLS_lstrlenW(LPCWSTR lpszStr
)
406 TRACE("(%s)\n", debugstr_w(lpszStr
));
407 return strlenW(lpszStr
);
410 /*************************************************************************
411 * MNLS_lstrcmpW@8 (MAPI32.63)
413 * Compare two Unicode strings.
416 * lpszLeft [I] First string to compare
417 * lpszRight [I] Second string to compare
420 * An integer less than, equal to or greater than 0, indicating that
421 * lpszLeft is less than, the same, or greater than lpszRight.
423 INT WINAPI
MNLS_lstrcmpW(LPCWSTR lpszLeft
, LPCWSTR lpszRight
)
425 TRACE("(%s,%s)\n", debugstr_w(lpszLeft
), debugstr_w(lpszRight
));
426 return strcmpW(lpszLeft
, lpszRight
);
429 /*************************************************************************
430 * MNLS_lstrcpyW@8 (MAPI32.64)
432 * Copy a Unicode string to another string.
435 * lpszDest [O] Destination string
436 * lpszSrc [I] Source string
439 * The length lpszDest in Unicode characters.
441 ULONG WINAPI
MNLS_lstrcpyW(LPWSTR lpszDest
, LPCWSTR lpszSrc
)
445 TRACE("(%p,%s)\n", lpszDest
, debugstr_w(lpszSrc
));
446 len
= (strlenW(lpszSrc
) + 1) * sizeof(WCHAR
);
447 memcpy(lpszDest
, lpszSrc
, len
);
451 /*************************************************************************
452 * MNLS_CompareStringW@12 (MAPI32.65)
454 * Compare two Unicode strings.
457 * dwCp [I] Code page for the comparison
458 * lpszLeft [I] First string to compare
459 * lpszRight [I] Second string to compare
462 * CSTR_LESS_THAN, CSTR_EQUAL or CSTR_GREATER_THAN, indicating that
463 * lpszLeft is less than, the same, or greater than lpszRight.
465 INT WINAPI
MNLS_CompareStringW(DWORD dwCp
, LPCWSTR lpszLeft
, LPCWSTR lpszRight
)
469 TRACE("0x%08x,%s,%s\n", dwCp
, debugstr_w(lpszLeft
), debugstr_w(lpszRight
));
470 ret
= MNLS_lstrcmpW(lpszLeft
, lpszRight
);
471 return ret
< 0 ? CSTR_LESS_THAN
: ret
? CSTR_GREATER_THAN
: CSTR_EQUAL
;
474 /**************************************************************************
475 * FEqualNames@8 (MAPI32.72)
477 * Compare two Mapi names.
480 * lpName1 [I] First name to compare to lpName2
481 * lpName2 [I] Second name to compare to lpName1
484 * TRUE, if the names are the same,
487 BOOL WINAPI
FEqualNames(LPMAPINAMEID lpName1
, LPMAPINAMEID lpName2
)
489 TRACE("(%p,%p)\n", lpName1
, lpName2
);
491 if (!lpName1
|| !lpName2
||
492 !IsEqualGUID(lpName1
->lpguid
, lpName2
->lpguid
) ||
493 lpName1
->ulKind
!= lpName2
->ulKind
)
496 if (lpName1
->ulKind
== MNID_STRING
)
497 return !strcmpW(lpName1
->Kind
.lpwstrName
, lpName2
->Kind
.lpwstrName
);
499 return lpName1
->Kind
.lID
== lpName2
->Kind
.lID
;
502 /**************************************************************************
503 * IsBadBoundedStringPtr@8 (MAPI32.71)
505 * Determine if a string pointer is valid.
508 * lpszStr [I] String to check
509 * ulLen [I] Maximum length of lpszStr
512 * TRUE, if lpszStr is invalid or longer than ulLen,
515 BOOL WINAPI
IsBadBoundedStringPtr(LPCSTR lpszStr
, ULONG ulLen
)
517 if (!lpszStr
|| IsBadStringPtrA(lpszStr
, -1) || strlen(lpszStr
) >= ulLen
)
522 /**************************************************************************
523 * FtAddFt@16 (MAPI32.121)
525 * Add two FILETIME's together.
528 * ftLeft [I] FILETIME to add to ftRight
529 * ftRight [I] FILETIME to add to ftLeft
532 * The sum of ftLeft and ftRight
534 LONGLONG WINAPI
MAPI32_FtAddFt(FILETIME ftLeft
, FILETIME ftRight
)
536 LONGLONG
*pl
= (LONGLONG
*)&ftLeft
, *pr
= (LONGLONG
*)&ftRight
;
541 /**************************************************************************
542 * FtSubFt@16 (MAPI32.123)
544 * Subtract two FILETIME's together.
547 * ftLeft [I] Initial FILETIME
548 * ftRight [I] FILETIME to subtract from ftLeft
551 * The remainder after ftRight is subtracted from ftLeft.
553 LONGLONG WINAPI
MAPI32_FtSubFt(FILETIME ftLeft
, FILETIME ftRight
)
555 LONGLONG
*pl
= (LONGLONG
*)&ftLeft
, *pr
= (LONGLONG
*)&ftRight
;
560 /**************************************************************************
561 * FtMulDw@12 (MAPI32.124)
563 * Multiply a FILETIME by a DWORD.
566 * dwLeft [I] DWORD to multiply with ftRight
567 * ftRight [I] FILETIME to multiply with dwLeft
570 * The product of dwLeft and ftRight
572 LONGLONG WINAPI
MAPI32_FtMulDw(DWORD dwLeft
, FILETIME ftRight
)
574 LONGLONG
*pr
= (LONGLONG
*)&ftRight
;
576 return (LONGLONG
)dwLeft
* (*pr
);
579 /**************************************************************************
580 * FtMulDwDw@8 (MAPI32.125)
582 * Multiply two DWORD, giving the result as a FILETIME.
585 * dwLeft [I] DWORD to multiply with dwRight
586 * dwRight [I] DWORD to multiply with dwLeft
589 * The product of ftMultiplier and ftMultiplicand as a FILETIME.
591 LONGLONG WINAPI
MAPI32_FtMulDwDw(DWORD dwLeft
, DWORD dwRight
)
593 return (LONGLONG
)dwLeft
* (LONGLONG
)dwRight
;
596 /**************************************************************************
597 * FtNegFt@8 (MAPI32.126)
602 * ft [I] FILETIME to negate
605 * The negation of ft.
607 LONGLONG WINAPI
MAPI32_FtNegFt(FILETIME ft
)
609 LONGLONG
*p
= (LONGLONG
*)&ft
;
614 /**************************************************************************
615 * UlAddRef@4 (MAPI32.128)
617 * Add a reference to an object.
620 * lpUnk [I] Object to add a reference to.
623 * The new reference count of the object, or 0 if lpUnk is NULL.
626 * See IUnknown_AddRef.
628 ULONG WINAPI
UlAddRef(void *lpUnk
)
630 TRACE("(%p)\n", lpUnk
);
634 return IUnknown_AddRef((LPUNKNOWN
)lpUnk
);
637 /**************************************************************************
638 * UlRelease@4 (MAPI32.129)
640 * Remove a reference from an object.
643 * lpUnk [I] Object to remove reference from.
646 * The new reference count of the object, or 0 if lpUnk is NULL. If lpUnk is
647 * non-NULL and this function returns 0, the object pointed to by lpUnk has
651 * See IUnknown_Release.
653 ULONG WINAPI
UlRelease(void *lpUnk
)
655 TRACE("(%p)\n", lpUnk
);
659 return IUnknown_Release((LPUNKNOWN
)lpUnk
);
662 /**************************************************************************
663 * UFromSz@4 (MAPI32.133)
665 * Read an integer from a string
668 * lpszStr [I] String to read the integer from.
671 * Success: The integer read from lpszStr.
672 * Failure: 0, if the first character in lpszStr is not 0-9.
675 * This function does not accept whitespace and stops at the first non-digit
678 UINT WINAPI
UFromSz(LPCSTR lpszStr
)
682 TRACE("(%s)\n", debugstr_a(lpszStr
));
686 while (*lpszStr
>= '0' && *lpszStr
<= '9')
688 ulRet
= ulRet
* 10 + (*lpszStr
- '0');
695 /*************************************************************************
696 * OpenStreamOnFile@24 (MAPI32.147)
698 * Create a stream on a file.
701 * lpAlloc [I] Memory allocation function
702 * lpFree [I] Memory free function
703 * ulFlags [I] Flags controlling the opening process
704 * lpszPath [I] Path of file to create stream on
705 * lpszPrefix [I] Prefix of the temporary file name (if ulFlags includes SOF_UNIQUEFILENAME)
706 * lppStream [O] Destination for created stream
709 * Success: S_OK. lppStream contains the new stream object
710 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
711 * describing the error.
713 HRESULT WINAPI
OpenStreamOnFile(LPALLOCATEBUFFER lpAlloc
, LPFREEBUFFER lpFree
,
714 ULONG ulFlags
, LPWSTR lpszPath
, LPWSTR lpszPrefix
,
717 WCHAR szBuff
[MAX_PATH
];
718 DWORD dwMode
= STGM_READWRITE
, dwAttributes
= 0;
721 TRACE("(%p,%p,0x%08x,%s,%s,%p)\n", lpAlloc
, lpFree
, ulFlags
,
722 debugstr_a((LPSTR
)lpszPath
), debugstr_a((LPSTR
)lpszPrefix
), lppStream
);
724 if (mapiFunctions
.OpenStreamOnFile
)
725 return mapiFunctions
.OpenStreamOnFile(lpAlloc
, lpFree
, ulFlags
, lpszPath
, lpszPrefix
, lppStream
);
730 if (ulFlags
& SOF_UNIQUEFILENAME
)
732 FIXME("Should generate a temporary name\n");
736 if (!lpszPath
|| !lppStream
)
739 /* FIXME: Should probably munge mode and attributes, and should handle
740 * Unicode arguments (I assume MAPI_UNICODE is set in ulFlags if
741 * we are being passed Unicode strings; MSDN doesn't say).
742 * This implementation is just enough for Outlook97 to start.
744 MultiByteToWideChar(CP_ACP
, 0, (LPSTR
)lpszPath
, -1, szBuff
, MAX_PATH
);
745 hRet
= SHCreateStreamOnFileEx(szBuff
, dwMode
, dwAttributes
, TRUE
,
750 /*************************************************************************
751 * UlFromSzHex@4 (MAPI32.155)
753 * Read an integer from a hexadecimal string.
756 * lpSzHex [I] String containing the hexadecimal number to read
759 * Success: The number represented by lpszHex.
760 * Failure: 0, if lpszHex does not contain a hex string.
763 * This function does not accept whitespace and stops at the first non-hex
766 ULONG WINAPI
UlFromSzHex(LPCWSTR lpszHex
)
768 LPCSTR lpStr
= (LPCSTR
)lpszHex
;
771 TRACE("(%s)\n", debugstr_a(lpStr
));
775 if (lpStr
[0] < '0' || lpStr
[0] > 'f' || digitsToHex
[lpStr
[0] - '0'] == 0xff ||
776 lpStr
[1] < '0' || lpStr
[1] > 'f' || digitsToHex
[lpStr
[1] - '0'] == 0xff)
779 ulRet
= ulRet
* 16 + ((digitsToHex
[lpStr
[0] - '0'] << 4) | digitsToHex
[lpStr
[1] - '0']);
785 /************************************************************************
786 * FBadEntryList@4 (MAPI32.190)
788 * Determine is an entry list is invalid.
791 * lpEntryList [I] List to check
794 * TRUE, if lpEntryList is invalid,
797 BOOL WINAPI
FBadEntryList(LPENTRYLIST lpEntryList
)
801 if (IsBadReadPtr(lpEntryList
, sizeof(*lpEntryList
)) ||
802 IsBadReadPtr(lpEntryList
->lpbin
,
803 lpEntryList
->cValues
* sizeof(*lpEntryList
->lpbin
)))
806 for (i
= 0; i
< lpEntryList
->cValues
; i
++)
807 if(IsBadReadPtr(lpEntryList
->lpbin
[i
].lpb
, lpEntryList
->lpbin
[i
].cb
))
813 /*************************************************************************
814 * CbOfEncoded@4 (MAPI32.207)
816 * Return the length of an encoded string.
819 * lpSzEnc [I] Encoded string to get the length of.
822 * The length of the encoded string in bytes.
824 ULONG WINAPI
CbOfEncoded(LPCSTR lpszEnc
)
828 TRACE("(%s)\n", debugstr_a(lpszEnc
));
831 ulRet
= (((strlen(lpszEnc
) | 3) >> 2) + 1) * 3;
835 /*************************************************************************
836 * cmc_query_configuration (MAPI32.235)
838 * Retrieves the configuration information for the installed CMC
841 * session [I] MAPI session handle
842 * item [I] Enumerated variable that identifies which
843 * configuration information is being requested
844 * reference [O] Buffer where configuration information is written
845 * config_extensions[I/O] Path of file to create stream on
850 CMC_return_code WINAPI
cmc_query_configuration(
851 CMC_session_id session
,
853 CMC_buffer reference
,
854 CMC_extension
*config_extensions
)
857 return CMC_E_NOT_SUPPORTED
;
860 /**************************************************************************
861 * FGetComponentPath (MAPI32.254)
862 * FGetComponentPath@20 (MAPI32.255)
864 * Return the installed component path, usually to the private mapi32.dll.
867 * component [I] Component ID
868 * qualifier [I] Application LCID
869 * dll_path [O] returned component path
870 * dll_path_length [I] component path length
871 * install [I] install mode
878 * Previously documented in Q229700 "How to locate the correct path
879 * to the Mapisvc.inf file in Microsoft Outlook".
881 BOOL WINAPI
FGetComponentPath(LPCSTR component
, LPCSTR qualifier
, LPSTR dll_path
,
882 DWORD dll_path_length
, BOOL install
)
887 TRACE("%s %s %p %u %d\n", component
, qualifier
, dll_path
, dll_path_length
, install
);
889 if (mapiFunctions
.FGetComponentPath
)
890 return mapiFunctions
.FGetComponentPath(component
, qualifier
, dll_path
, dll_path_length
, install
);
894 hmsi
= LoadLibraryA("msi.dll");
897 UINT (WINAPI
*pMsiProvideQualifiedComponentA
)(LPCSTR
, LPCSTR
, DWORD
, LPSTR
, LPDWORD
);
899 pMsiProvideQualifiedComponentA
= (void *)GetProcAddress(hmsi
, "MsiProvideQualifiedComponentA");
900 if (pMsiProvideQualifiedComponentA
)
902 static const char * const fmt
[] = { "%d\\NT", "%d\\95", "%d" };
906 for (i
= 0; i
< ARRAY_SIZE(fmt
); i
++)
908 /* FIXME: what's the correct behaviour here? */
909 if (!qualifier
|| qualifier
== lcid_ver
)
911 sprintf(lcid_ver
, fmt
[i
], GetUserDefaultUILanguage());
912 qualifier
= lcid_ver
;
915 if (pMsiProvideQualifiedComponentA(component
, qualifier
,
916 install
? INSTALLMODE_DEFAULT
: INSTALLMODE_EXISTING
,
917 dll_path
, &dll_path_length
) == ERROR_SUCCESS
)
923 if (qualifier
!= lcid_ver
) break;
931 /**************************************************************************
932 * HrQueryAllRows (MAPI32.75)
934 HRESULT WINAPI
HrQueryAllRows(LPMAPITABLE lpTable
, LPSPropTagArray lpPropTags
,
935 LPSRestriction lpRestriction
, LPSSortOrderSet lpSortOrderSet
,
936 LONG crowsMax
, LPSRowSet
*lppRows
)
938 if (mapiFunctions
.HrQueryAllRows
)
939 return mapiFunctions
.HrQueryAllRows(lpTable
, lpPropTags
, lpRestriction
, lpSortOrderSet
, crowsMax
, lppRows
);
941 FIXME("(%p, %p, %p, %p, %d, %p): stub\n", lpTable
, lpPropTags
, lpRestriction
, lpSortOrderSet
, crowsMax
, lppRows
);
943 return MAPI_E_CALL_FAILED
;
946 /**************************************************************************
947 * WrapCompressedRTFStream (MAPI32.186)
949 HRESULT WINAPI
WrapCompressedRTFStream(LPSTREAM compressed
, ULONG flags
, LPSTREAM
*uncompressed
)
951 if (mapiFunctions
.WrapCompressedRTFStream
)
952 return mapiFunctions
.WrapCompressedRTFStream(compressed
, flags
, uncompressed
);
954 FIXME("(%p, 0x%08x, %p): stub\n", compressed
, flags
, uncompressed
);
955 return MAPI_E_NO_SUPPORT
;
958 static HMODULE mapi_provider
;
959 static HMODULE mapi_ex_provider
;
961 /**************************************************************************
964 * Attempts to load a MAPI provider from the specified registry key.
966 * Returns a handle to the loaded module in `mapi_provider' if successful.
968 static void load_mapi_provider(HKEY hkeyMail
, LPCWSTR valueName
, HMODULE
*mapi_provider
)
970 static const WCHAR mapi32_dll
[] = {'m','a','p','i','3','2','.','d','l','l',0 };
972 DWORD dwType
, dwLen
= 0;
975 /* Check if we have a value set for DLLPath */
976 if ((RegQueryValueExW(hkeyMail
, valueName
, NULL
, &dwType
, NULL
, &dwLen
) == ERROR_SUCCESS
) &&
977 ((dwType
== REG_SZ
) || (dwType
== REG_EXPAND_SZ
)) && (dwLen
> 0))
979 dllPath
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
983 RegQueryValueExW(hkeyMail
, valueName
, NULL
, NULL
, (LPBYTE
)dllPath
, &dwLen
);
985 /* Check that this value doesn't refer to mapi32.dll (eg, as Outlook does) */
986 if (lstrcmpiW(dllPath
, mapi32_dll
) != 0)
988 if (dwType
== REG_EXPAND_SZ
)
991 LPWSTR dllPathExpanded
;
993 /* Expand the path if necessary */
994 dwExpandLen
= ExpandEnvironmentStringsW(dllPath
, NULL
, 0);
995 dllPathExpanded
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
) * dwExpandLen
+ 1);
999 ExpandEnvironmentStringsW(dllPath
, dllPathExpanded
, dwExpandLen
+ 1);
1001 HeapFree(GetProcessHeap(), 0, dllPath
);
1002 dllPath
= dllPathExpanded
;
1007 TRACE("loading %s\n", debugstr_w(dllPath
));
1008 *mapi_provider
= LoadLibraryW(dllPath
);
1011 HeapFree(GetProcessHeap(), 0, dllPath
);
1016 /**************************************************************************
1017 * load_mapi_providers
1019 * Scans the registry for MAPI providers and attempts to load a Simple and
1020 * Extended MAPI library.
1022 * Returns TRUE if at least one library loaded, FALSE otherwise.
1024 void load_mapi_providers(void)
1026 static const WCHAR regkey_mail
[] = {
1027 'S','o','f','t','w','a','r','e','\\','C','l','i','e','n','t','s','\\',
1028 'M','a','i','l',0 };
1030 static const WCHAR regkey_dllpath
[] = {'D','L','L','P','a','t','h',0 };
1031 static const WCHAR regkey_dllpath_ex
[] = {'D','L','L','P','a','t','h','E','x',0 };
1032 static const WCHAR regkey_backslash
[] = { '\\', 0 };
1035 DWORD dwType
, dwLen
= 0;
1036 LPWSTR appName
= NULL
, appKey
= NULL
;
1040 /* Open the Mail key */
1041 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, regkey_mail
, 0, KEY_READ
, &hkeyMail
) != ERROR_SUCCESS
)
1044 /* Check if we have a default value set, and the length of it */
1045 if ((RegQueryValueExW(hkeyMail
, NULL
, NULL
, &dwType
, NULL
, &dwLen
) != ERROR_SUCCESS
) ||
1046 !((dwType
== REG_SZ
) || (dwType
== REG_EXPAND_SZ
)) || (dwLen
== 0))
1049 appName
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
1054 /* Get the value, and get the path to the app key */
1055 RegQueryValueExW(hkeyMail
, NULL
, NULL
, NULL
, (LPBYTE
)appName
, &dwLen
);
1057 TRACE("appName: %s\n", debugstr_w(appName
));
1059 appKey
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
) * (lstrlenW(regkey_mail
) +
1060 lstrlenW(regkey_backslash
) + lstrlenW(appName
) + 1));
1065 lstrcpyW(appKey
, regkey_mail
);
1066 lstrcatW(appKey
, regkey_backslash
);
1067 lstrcatW(appKey
, appName
);
1069 RegCloseKey(hkeyMail
);
1071 TRACE("appKey: %s\n", debugstr_w(appKey
));
1073 /* Open the app's key */
1074 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, appKey
, 0, KEY_READ
, &hkeyMail
) != ERROR_SUCCESS
)
1077 /* Try to load the providers */
1078 load_mapi_provider(hkeyMail
, regkey_dllpath
, &mapi_provider
);
1079 load_mapi_provider(hkeyMail
, regkey_dllpath_ex
, &mapi_ex_provider
);
1081 /* Now try to load our function pointers */
1082 ZeroMemory(&mapiFunctions
, sizeof(mapiFunctions
));
1084 /* Simple MAPI functions */
1087 mapiFunctions
.MAPIAddress
= (void*) GetProcAddress(mapi_provider
, "MAPIAddress");
1088 mapiFunctions
.MAPIDeleteMail
= (void*) GetProcAddress(mapi_provider
, "MAPIDeleteMail");
1089 mapiFunctions
.MAPIDetails
= (void*) GetProcAddress(mapi_provider
, "MAPIDetails");
1090 mapiFunctions
.MAPIFindNext
= (void*) GetProcAddress(mapi_provider
, "MAPIFindNext");
1091 mapiFunctions
.MAPILogoff
= (void*) GetProcAddress(mapi_provider
, "MAPILogoff");
1092 mapiFunctions
.MAPILogon
= (void*) GetProcAddress(mapi_provider
, "MAPILogon");
1093 mapiFunctions
.MAPIReadMail
= (void*) GetProcAddress(mapi_provider
, "MAPIReadMail");
1094 mapiFunctions
.MAPIResolveName
= (void*) GetProcAddress(mapi_provider
, "MAPIResolveName");
1095 mapiFunctions
.MAPISaveMail
= (void*) GetProcAddress(mapi_provider
, "MAPISaveMail");
1096 mapiFunctions
.MAPISendDocuments
= (void*) GetProcAddress(mapi_provider
, "MAPISendDocuments");
1097 mapiFunctions
.MAPISendMail
= (void*) GetProcAddress(mapi_provider
, "MAPISendMail");
1098 mapiFunctions
.MAPISendMailW
= (void*) GetProcAddress(mapi_provider
, "MAPISendMailW");
1101 /* Extended MAPI functions */
1102 if (mapi_ex_provider
)
1104 mapiFunctions
.MAPIInitialize
= (void*) GetProcAddress(mapi_ex_provider
, "MAPIInitialize");
1105 mapiFunctions
.MAPILogonEx
= (void*) GetProcAddress(mapi_ex_provider
, "MAPILogonEx");
1106 mapiFunctions
.MAPIUninitialize
= (void*) GetProcAddress(mapi_ex_provider
, "MAPIUninitialize");
1108 mapiFunctions
.DeinitMapiUtil
= (void*) GetProcAddress(mapi_ex_provider
, "DeinitMapiUtil@0");
1109 mapiFunctions
.DllCanUnloadNow
= (void*) GetProcAddress(mapi_ex_provider
, "DllCanUnloadNow");
1110 mapiFunctions
.DllGetClassObject
= (void*) GetProcAddress(mapi_ex_provider
, "DllGetClassObject");
1111 mapiFunctions
.FGetComponentPath
= (void*) GetProcAddress(mapi_ex_provider
, "FGetComponentPath");
1112 mapiFunctions
.HrThisThreadAdviseSink
= (void*) GetProcAddress(mapi_ex_provider
, "HrThisThreadAdviseSink@8");
1113 mapiFunctions
.HrQueryAllRows
= (void*) GetProcAddress(mapi_ex_provider
, "HrQueryAllRows@24");
1114 mapiFunctions
.MAPIAdminProfiles
= (void*) GetProcAddress(mapi_ex_provider
, "MAPIAdminProfiles");
1115 mapiFunctions
.MAPIAllocateBuffer
= (void*) GetProcAddress(mapi_ex_provider
, "MAPIAllocateBuffer");
1116 mapiFunctions
.MAPIAllocateMore
= (void*) GetProcAddress(mapi_ex_provider
, "MAPIAllocateMore");
1117 mapiFunctions
.MAPIFreeBuffer
= (void*) GetProcAddress(mapi_ex_provider
, "MAPIFreeBuffer");
1118 mapiFunctions
.MAPIGetDefaultMalloc
= (void*) GetProcAddress(mapi_ex_provider
, "MAPIGetDefaultMalloc@0");
1119 mapiFunctions
.MAPIOpenLocalFormContainer
= (void *) GetProcAddress(mapi_ex_provider
, "MAPIOpenLocalFormContainer");
1120 mapiFunctions
.OpenStreamOnFile
= (void*) GetProcAddress(mapi_ex_provider
, "OpenStreamOnFile@24");
1121 mapiFunctions
.ScInitMapiUtil
= (void*) GetProcAddress(mapi_ex_provider
, "ScInitMapiUtil@4");
1122 mapiFunctions
.WrapCompressedRTFStream
= (void*) GetProcAddress(mapi_ex_provider
, "WrapCompressedRTFStream@12");
1126 RegCloseKey(hkeyMail
);
1127 HeapFree(GetProcessHeap(), 0, appKey
);
1128 HeapFree(GetProcessHeap(), 0, appName
);
1131 /**************************************************************************
1132 * unload_mapi_providers
1134 * Unloads any loaded MAPI libraries.
1136 void unload_mapi_providers(void)
1140 FreeLibrary(mapi_provider
);
1141 FreeLibrary(mapi_ex_provider
);