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
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
36 #include "wine/debug.h"
37 #include "wine/unicode.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(mapi
);
45 static const BYTE digitsToHex
[] = {
46 0,1,2,3,4,5,6,7,8,9,0xff,0xff,0xff,0xff,0xff,0xff,0xff,10,11,12,13,14,15,
47 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
48 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,10,11,12,13,
51 MAPI_FUNCTIONS mapiFunctions
;
53 /**************************************************************************
54 * ScInitMapiUtil (MAPI32.33)
56 * Initialise Mapi utility functions.
59 * ulReserved [I] Reserved, pass 0.
62 * Success: S_OK. Mapi utility functions may be called.
63 * Failure: MAPI_E_INVALID_PARAMETER, if ulReserved is not 0.
66 * Your application does not need to call this function unless it does not
67 * call MAPIInitialize()/MAPIUninitialize().
69 SCODE WINAPI
ScInitMapiUtil(ULONG ulReserved
)
71 if (mapiFunctions
.ScInitMapiUtil
)
72 return mapiFunctions
.ScInitMapiUtil(ulReserved
);
74 FIXME("(0x%08x)stub!\n", ulReserved
);
76 return MAPI_E_INVALID_PARAMETER
;
80 /**************************************************************************
81 * DeinitMapiUtil (MAPI32.34)
83 * Uninitialise Mapi utility functions.
92 * Your application does not need to call this function unless it does not
93 * call MAPIInitialize()/MAPIUninitialize().
95 VOID WINAPI
DeinitMapiUtil(void)
97 if (mapiFunctions
.DeinitMapiUtil
)
98 mapiFunctions
.DeinitMapiUtil();
103 typedef LPVOID
*LPMAPIALLOCBUFFER
;
105 /**************************************************************************
106 * MAPIAllocateBuffer (MAPI32.12)
107 * MAPIAllocateBuffer@8 (MAPI32.13)
109 * Allocate a block of memory.
112 * cbSize [I] Size of the block to allocate in bytes
113 * lppBuffer [O] Destination for pointer to allocated memory
116 * Success: S_OK. *lppBuffer is filled with a pointer to a memory block of
117 * length cbSize bytes.
118 * Failure: MAPI_E_INVALID_PARAMETER, if lppBuffer is NULL.
119 * MAPI_E_NOT_ENOUGH_MEMORY, if the memory allocation fails.
122 * Memory allocated with this function should be freed with MAPIFreeBuffer().
123 * Further allocations of memory may be linked to the pointer returned using
124 * MAPIAllocateMore(). Linked allocations are freed when the initial pointer
127 SCODE WINAPI
MAPIAllocateBuffer(ULONG cbSize
, LPVOID
*lppBuffer
)
129 LPMAPIALLOCBUFFER lpBuff
;
131 TRACE("(%d,%p)\n", cbSize
, lppBuffer
);
133 if (mapiFunctions
.MAPIAllocateBuffer
)
134 return mapiFunctions
.MAPIAllocateBuffer(cbSize
, lppBuffer
);
139 lpBuff
= HeapAlloc(GetProcessHeap(), 0, cbSize
+ sizeof(*lpBuff
));
141 return MAPI_E_NOT_ENOUGH_MEMORY
;
143 TRACE("initial allocation:%p, returning %p\n", lpBuff
, lpBuff
+ 1);
149 /**************************************************************************
150 * MAPIAllocateMore (MAPI32.14)
151 * MAPIAllocateMore@12 (MAPI32.15)
153 * Allocate a block of memory linked to a previous allocation.
156 * cbSize [I] Size of the block to allocate in bytes
157 * lpOrig [I] Initial allocation to link to, from MAPIAllocateBuffer()
158 * lppBuffer [O] Destination for pointer to allocated memory
161 * Success: S_OK. *lppBuffer is filled with a pointer to a memory block of
162 * length cbSize bytes.
163 * Failure: MAPI_E_INVALID_PARAMETER, if lpOrig or lppBuffer is invalid.
164 * MAPI_E_NOT_ENOUGH_MEMORY, if memory allocation fails.
167 * Memory allocated with this function and stored in *lppBuffer is freed
168 * when lpOrig is passed to MAPIFreeBuffer(). It should not be freed independently.
170 SCODE WINAPI
MAPIAllocateMore(ULONG cbSize
, LPVOID lpOrig
, LPVOID
*lppBuffer
)
172 LPMAPIALLOCBUFFER lpBuff
= lpOrig
;
174 TRACE("(%d,%p,%p)\n", cbSize
, lpOrig
, lppBuffer
);
176 if (mapiFunctions
.MAPIAllocateMore
)
177 return mapiFunctions
.MAPIAllocateMore(cbSize
, lpOrig
, lppBuffer
);
179 if (!lppBuffer
|| !lpBuff
|| !--lpBuff
)
182 /* Find the last allocation in the chain */
185 TRACE("linked:%p->%p\n", lpBuff
, *lpBuff
);
189 if (SUCCEEDED(MAPIAllocateBuffer(cbSize
, lppBuffer
)))
191 *lpBuff
= ((LPMAPIALLOCBUFFER
)*lppBuffer
) - 1;
192 TRACE("linking %p->%p\n", lpBuff
, *lpBuff
);
194 return *lppBuffer
? S_OK
: MAPI_E_NOT_ENOUGH_MEMORY
;
197 /**************************************************************************
198 * MAPIFreeBuffer (MAPI32.16)
199 * MAPIFreeBuffer@4 (MAPI32.17)
201 * Free a block of memory and any linked allocations associated with it.
204 * lpBuffer [I] Memory to free, returned from MAPIAllocateBuffer()
209 ULONG WINAPI
MAPIFreeBuffer(LPVOID lpBuffer
)
211 LPMAPIALLOCBUFFER lpBuff
= lpBuffer
;
213 TRACE("(%p)\n", lpBuffer
);
215 if (mapiFunctions
.MAPIFreeBuffer
)
216 return mapiFunctions
.MAPIFreeBuffer(lpBuffer
);
218 if (lpBuff
&& --lpBuff
)
222 LPVOID lpFree
= lpBuff
;
226 TRACE("linked:%p->%p, freeing %p\n", lpFree
, lpBuff
, lpFree
);
227 HeapFree(GetProcessHeap(), 0, lpFree
);
233 /**************************************************************************
234 * WrapProgress@20 (MAPI32.41)
236 HRESULT WINAPI
WrapProgress(PVOID unk1
, PVOID unk2
, PVOID unk3
, PVOID unk4
, PVOID unk5
)
238 /* Native does not implement this function */
239 return MAPI_E_NO_SUPPORT
;
242 /*************************************************************************
243 * HrThisThreadAdviseSink@8 (MAPI32.42)
245 * Ensure that an advise sink is only notified in its originating thread.
248 * lpSink [I] IMAPIAdviseSink interface to be protected
249 * lppNewSink [I] Destination for wrapper IMAPIAdviseSink interface
252 * Success: S_OK. *lppNewSink contains a new sink to use in place of lpSink.
253 * Failure: E_INVALIDARG, if any parameter is invalid.
255 HRESULT WINAPI
HrThisThreadAdviseSink(LPMAPIADVISESINK lpSink
, LPMAPIADVISESINK
* lppNewSink
)
257 if (mapiFunctions
.HrThisThreadAdviseSink
)
258 return mapiFunctions
.HrThisThreadAdviseSink(lpSink
, lppNewSink
);
260 FIXME("(%p,%p)semi-stub\n", lpSink
, lppNewSink
);
262 if (!lpSink
|| !lppNewSink
)
265 /* Don't wrap the sink for now, just copy it */
266 *lppNewSink
= lpSink
;
267 IMAPIAdviseSink_AddRef(lpSink
);
271 /*************************************************************************
272 * FBinFromHex (MAPI32.44)
274 * Create an array of binary data from a string.
277 * lpszHex [I] String to convert to binary data
278 * lpOut [O] Destination for resulting binary data
281 * Success: TRUE. lpOut contains the decoded binary data.
282 * Failure: FALSE, if lpszHex does not represent a binary string.
285 * - lpOut must be at least half the length of lpszHex in bytes.
286 * - Although the Mapi headers prototype this function as both
287 * Ascii and Unicode, there is only one (Ascii) implementation. This
288 * means that lpszHex is treated as an Ascii string (i.e. a single NUL
289 * character in the byte stream terminates the string).
291 BOOL WINAPI
FBinFromHex(LPWSTR lpszHex
, LPBYTE lpOut
)
293 LPSTR lpStr
= (LPSTR
)lpszHex
;
295 TRACE("(%p,%p)\n", lpszHex
, lpOut
);
299 if (lpStr
[0] < '0' || lpStr
[0] > 'f' || digitsToHex
[lpStr
[0] - '0'] == 0xff ||
300 lpStr
[1] < '0' || lpStr
[1] > 'f' || digitsToHex
[lpStr
[1] - '0'] == 0xff)
303 *lpOut
++ = (digitsToHex
[lpStr
[0] - '0'] << 4) | digitsToHex
[lpStr
[1] - '0'];
309 /*************************************************************************
310 * HexFromBin (MAPI32.45)
312 * Create a string from an array of binary data.
315 * lpHex [I] Binary data to convert to string
316 * iCount [I] Length of lpHex in bytes
317 * lpszOut [O] Destination for resulting hex string
323 * - lpszOut must be at least 2 * iCount + 1 bytes characters long.
324 * - Although the Mapi headers prototype this function as both
325 * Ascii and Unicode, there is only one (Ascii) implementation. This
326 * means that the resulting string is not properly NUL terminated
327 * if the caller expects it to be a Unicode string.
329 void WINAPI
HexFromBin(LPBYTE lpHex
, int iCount
, LPWSTR lpszOut
)
331 static const char hexDigits
[] = { "0123456789ABCDEF" };
332 LPSTR lpStr
= (LPSTR
)lpszOut
;
334 TRACE("(%p,%d,%p)\n", lpHex
, iCount
, lpszOut
);
338 *lpStr
++ = hexDigits
[*lpHex
>> 4];
339 *lpStr
++ = hexDigits
[*lpHex
& 0xf];
345 /*************************************************************************
346 * SwapPlong@8 (MAPI32.47)
348 * Swap the bytes in a ULONG array.
351 * lpData [O] Array to swap bytes in
352 * ulLen [I] Number of ULONG element to swap the bytes of
357 VOID WINAPI
SwapPlong(PULONG lpData
, ULONG ulLen
)
361 for (i
= 0; i
< ulLen
; i
++)
362 lpData
[i
] = RtlUlongByteSwap(lpData
[i
]);
365 /*************************************************************************
366 * SwapPword@8 (MAPI32.48)
368 * Swap the bytes in a USHORT array.
371 * lpData [O] Array to swap bytes in
372 * ulLen [I] Number of USHORT element to swap the bytes of
377 VOID WINAPI
SwapPword(PUSHORT lpData
, ULONG ulLen
)
381 for (i
= 0; i
< ulLen
; i
++)
382 lpData
[i
] = RtlUshortByteSwap(lpData
[i
]);
385 /**************************************************************************
386 * MNLS_lstrlenW@4 (MAPI32.62)
388 * Calculate the length of a Unicode string.
391 * lpszStr [I] String to calculate the length of
394 * The length of lpszStr in Unicode characters.
396 ULONG WINAPI
MNLS_lstrlenW(LPCWSTR lpszStr
)
398 TRACE("(%s)\n", debugstr_w(lpszStr
));
399 return strlenW(lpszStr
);
402 /*************************************************************************
403 * MNLS_lstrcmpW@8 (MAPI32.63)
405 * Compare two Unicode strings.
408 * lpszLeft [I] First string to compare
409 * lpszRight [I] Second string to compare
412 * An integer less than, equal to or greater than 0, indicating that
413 * lpszLeft is less than, the same, or greater than lpszRight.
415 INT WINAPI
MNLS_lstrcmpW(LPCWSTR lpszLeft
, LPCWSTR lpszRight
)
417 TRACE("(%s,%s)\n", debugstr_w(lpszLeft
), debugstr_w(lpszRight
));
418 return strcmpW(lpszLeft
, lpszRight
);
421 /*************************************************************************
422 * MNLS_lstrcpyW@8 (MAPI32.64)
424 * Copy a Unicode string to another string.
427 * lpszDest [O] Destination string
428 * lpszSrc [I] Source string
431 * The length lpszDest in Unicode characters.
433 ULONG WINAPI
MNLS_lstrcpyW(LPWSTR lpszDest
, LPCWSTR lpszSrc
)
437 TRACE("(%p,%s)\n", lpszDest
, debugstr_w(lpszSrc
));
438 len
= (strlenW(lpszSrc
) + 1) * sizeof(WCHAR
);
439 memcpy(lpszDest
, lpszSrc
, len
);
443 /*************************************************************************
444 * MNLS_CompareStringW@12 (MAPI32.65)
446 * Compare two Unicode strings.
449 * dwCp [I] Code page for the comparison
450 * lpszLeft [I] First string to compare
451 * lpszRight [I] Second string to compare
454 * CSTR_LESS_THAN, CSTR_EQUAL or CSTR_GREATER_THAN, indicating that
455 * lpszLeft is less than, the same, or greater than lpszRight.
457 INT WINAPI
MNLS_CompareStringW(DWORD dwCp
, LPCWSTR lpszLeft
, LPCWSTR lpszRight
)
461 TRACE("0x%08x,%s,%s\n", dwCp
, debugstr_w(lpszLeft
), debugstr_w(lpszRight
));
462 ret
= MNLS_lstrcmpW(lpszLeft
, lpszRight
);
463 return ret
< 0 ? CSTR_LESS_THAN
: ret
? CSTR_GREATER_THAN
: CSTR_EQUAL
;
466 /**************************************************************************
467 * FEqualNames@8 (MAPI32.72)
469 * Compare two Mapi names.
472 * lpName1 [I] First name to compare to lpName2
473 * lpName2 [I] Second name to compare to lpName1
476 * TRUE, if the names are the same,
479 BOOL WINAPI
FEqualNames(LPMAPINAMEID lpName1
, LPMAPINAMEID lpName2
)
481 TRACE("(%p,%p)\n", lpName1
, lpName2
);
483 if (!lpName1
|| !lpName2
||
484 !IsEqualGUID(lpName1
->lpguid
, lpName2
->lpguid
) ||
485 lpName1
->ulKind
!= lpName2
->ulKind
)
488 if (lpName1
->ulKind
== MNID_STRING
)
489 return !strcmpW(lpName1
->Kind
.lpwstrName
, lpName2
->Kind
.lpwstrName
);
491 return lpName1
->Kind
.lID
== lpName2
->Kind
.lID
? TRUE
: FALSE
;
494 /**************************************************************************
495 * IsBadBoundedStringPtr@8 (MAPI32.71)
497 * Determine if a string pointer is valid.
500 * lpszStr [I] String to check
501 * ulLen [I] Maximum length of lpszStr
504 * TRUE, if lpszStr is invalid or longer than ulLen,
507 BOOL WINAPI
IsBadBoundedStringPtr(LPCSTR lpszStr
, ULONG ulLen
)
509 if (!lpszStr
|| IsBadStringPtrA(lpszStr
, -1) || strlen(lpszStr
) >= ulLen
)
514 /**************************************************************************
515 * FtAddFt@16 (MAPI32.121)
517 * Add two FILETIME's together.
520 * ftLeft [I] FILETIME to add to ftRight
521 * ftRight [I] FILETIME to add to ftLeft
524 * The sum of ftLeft and ftRight
526 LONGLONG WINAPI
MAPI32_FtAddFt(FILETIME ftLeft
, FILETIME ftRight
)
528 LONGLONG
*pl
= (LONGLONG
*)&ftLeft
, *pr
= (LONGLONG
*)&ftRight
;
533 /**************************************************************************
534 * FtSubFt@16 (MAPI32.123)
536 * Subtract two FILETIME's together.
539 * ftLeft [I] Initial FILETIME
540 * ftRight [I] FILETIME to subtract from ftLeft
543 * The remainder after ftRight is subtracted from ftLeft.
545 LONGLONG WINAPI
MAPI32_FtSubFt(FILETIME ftLeft
, FILETIME ftRight
)
547 LONGLONG
*pl
= (LONGLONG
*)&ftLeft
, *pr
= (LONGLONG
*)&ftRight
;
552 /**************************************************************************
553 * FtMulDw@12 (MAPI32.124)
555 * Multiply a FILETIME by a DWORD.
558 * dwLeft [I] DWORD to multiply with ftRight
559 * ftRight [I] FILETIME to multiply with dwLeft
562 * The product of dwLeft and ftRight
564 LONGLONG WINAPI
MAPI32_FtMulDw(DWORD dwLeft
, FILETIME ftRight
)
566 LONGLONG
*pr
= (LONGLONG
*)&ftRight
;
568 return (LONGLONG
)dwLeft
* (*pr
);
571 /**************************************************************************
572 * FtMulDwDw@8 (MAPI32.125)
574 * Multiply two DWORD, giving the result as a FILETIME.
577 * dwLeft [I] DWORD to multiply with dwRight
578 * dwRight [I] DWORD to multiply with dwLeft
581 * The product of ftMultiplier and ftMultiplicand as a FILETIME.
583 LONGLONG WINAPI
MAPI32_FtMulDwDw(DWORD dwLeft
, DWORD dwRight
)
585 return (LONGLONG
)dwLeft
* (LONGLONG
)dwRight
;
588 /**************************************************************************
589 * FtNegFt@8 (MAPI32.126)
594 * ft [I] FILETIME to negate
597 * The negation of ft.
599 LONGLONG WINAPI
MAPI32_FtNegFt(FILETIME ft
)
601 LONGLONG
*p
= (LONGLONG
*)&ft
;
606 /**************************************************************************
607 * UlAddRef@4 (MAPI32.128)
609 * Add a reference to an object.
612 * lpUnk [I] Object to add a reference to.
615 * The new reference count of the object, or 0 if lpUnk is NULL.
618 * See IUnknown_AddRef.
620 ULONG WINAPI
UlAddRef(void *lpUnk
)
622 TRACE("(%p)\n", lpUnk
);
626 return IUnknown_AddRef((LPUNKNOWN
)lpUnk
);
629 /**************************************************************************
630 * UlRelease@4 (MAPI32.129)
632 * Remove a reference from an object.
635 * lpUnk [I] Object to remove reference from.
638 * The new reference count of the object, or 0 if lpUnk is NULL. If lpUnk is
639 * non-NULL and this function returns 0, the object pointed to by lpUnk has
643 * See IUnknown_Release.
645 ULONG WINAPI
UlRelease(void *lpUnk
)
647 TRACE("(%p)\n", lpUnk
);
651 return IUnknown_Release((LPUNKNOWN
)lpUnk
);
654 /**************************************************************************
655 * UFromSz@4 (MAPI32.133)
657 * Read an integer from a string
660 * lpszStr [I] String to read the integer from.
663 * Success: The integer read from lpszStr.
664 * Failure: 0, if the first character in lpszStr is not 0-9.
667 * This function does not accept whitespace and stops at the first non-digit
670 UINT WINAPI
UFromSz(LPCSTR lpszStr
)
674 TRACE("(%s)\n", debugstr_a(lpszStr
));
678 while (*lpszStr
>= '0' && *lpszStr
<= '9')
680 ulRet
= ulRet
* 10 + (*lpszStr
- '0');
687 /*************************************************************************
688 * OpenStreamOnFile@24 (MAPI32.147)
690 * Create a stream on a file.
693 * lpAlloc [I] Memory allocation function
694 * lpFree [I] Memory free function
695 * ulFlags [I] Flags controlling the opening process
696 * lpszPath [I] Path of file to create stream on
697 * lpszPrefix [I] Prefix of the temporary file name (if ulFlags includes SOF_UNIQUEFILENAME)
698 * lppStream [O] Destination for created stream
701 * Success: S_OK. lppStream contains the new stream object
702 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
703 * describing the error.
705 HRESULT WINAPI
OpenStreamOnFile(LPALLOCATEBUFFER lpAlloc
, LPFREEBUFFER lpFree
,
706 ULONG ulFlags
, LPWSTR lpszPath
, LPWSTR lpszPrefix
,
709 WCHAR szBuff
[MAX_PATH
];
710 DWORD dwMode
= STGM_READWRITE
, dwAttributes
= 0;
713 TRACE("(%p,%p,0x%08x,%s,%s,%p)\n", lpAlloc
, lpFree
, ulFlags
,
714 debugstr_a((LPSTR
)lpszPath
), debugstr_a((LPSTR
)lpszPrefix
), lppStream
);
716 if (mapiFunctions
.OpenStreamOnFile
)
717 return mapiFunctions
.OpenStreamOnFile(lpAlloc
, lpFree
, ulFlags
, lpszPath
, lpszPrefix
, lppStream
);
722 if (ulFlags
& SOF_UNIQUEFILENAME
)
724 FIXME("Should generate a temporary name\n");
728 if (!lpszPath
|| !lppStream
)
731 /* FIXME: Should probably munge mode and attributes, and should handle
732 * Unicode arguments (I assume MAPI_UNICODE is set in ulFlags if
733 * we are being passed Unicode strings; MSDN doesn't say).
734 * This implementation is just enough for Outlook97 to start.
736 MultiByteToWideChar(CP_ACP
, 0, (LPSTR
)lpszPath
, -1, szBuff
, MAX_PATH
);
737 hRet
= SHCreateStreamOnFileEx(szBuff
, dwMode
, dwAttributes
, TRUE
,
742 /*************************************************************************
743 * UlFromSzHex@4 (MAPI32.155)
745 * Read an integer from a hexadecimal string.
748 * lpSzHex [I] String containing the hexadecimal number to read
751 * Success: The number represented by lpszHex.
752 * Failure: 0, if lpszHex does not contain a hex string.
755 * This function does not accept whitespace and stops at the first non-hex
758 ULONG WINAPI
UlFromSzHex(LPCWSTR lpszHex
)
760 LPCSTR lpStr
= (LPCSTR
)lpszHex
;
763 TRACE("(%s)\n", debugstr_a(lpStr
));
767 if (lpStr
[0] < '0' || lpStr
[0] > 'f' || digitsToHex
[lpStr
[0] - '0'] == 0xff ||
768 lpStr
[1] < '0' || lpStr
[1] > 'f' || digitsToHex
[lpStr
[1] - '0'] == 0xff)
771 ulRet
= ulRet
* 16 + ((digitsToHex
[lpStr
[0] - '0'] << 4) | digitsToHex
[lpStr
[1] - '0']);
777 /************************************************************************
778 * FBadEntryList@4 (MAPI32.190)
780 * Determine is an entry list is invalid.
783 * lpEntryList [I] List to check
786 * TRUE, if lpEntryList is invalid,
789 BOOL WINAPI
FBadEntryList(LPENTRYLIST lpEntryList
)
793 if (IsBadReadPtr(lpEntryList
, sizeof(*lpEntryList
)) ||
794 IsBadReadPtr(lpEntryList
->lpbin
,
795 lpEntryList
->cValues
* sizeof(*lpEntryList
->lpbin
)))
798 for (i
= 0; i
< lpEntryList
->cValues
; i
++)
799 if(IsBadReadPtr(lpEntryList
->lpbin
[i
].lpb
, lpEntryList
->lpbin
[i
].cb
))
805 /*************************************************************************
806 * CbOfEncoded@4 (MAPI32.207)
808 * Return the length of an encoded string.
811 * lpSzEnc [I] Encoded string to get the length of.
814 * The length of the encoded string in bytes.
816 ULONG WINAPI
CbOfEncoded(LPCSTR lpszEnc
)
820 TRACE("(%s)\n", debugstr_a(lpszEnc
));
823 ulRet
= (((strlen(lpszEnc
) | 3) >> 2) + 1) * 3;
827 /*************************************************************************
828 * cmc_query_configuration (MAPI32.235)
830 * Retrieves the configuration information for the installed CMC
833 * session [I] MAPI session handle
834 * item [I] Enumerated variable that identifies which
835 * configuration information is being requested
836 * reference [O] Buffer where configuration information is written
837 * config_extensions[I/O] Path of file to create stream on
842 CMC_return_code WINAPI
cmc_query_configuration(
843 CMC_session_id session
,
845 CMC_buffer reference
,
846 CMC_extension
*config_extensions
)
849 return CMC_E_NOT_SUPPORTED
;
852 /**************************************************************************
853 * FGetComponentPath (MAPI32.254)
854 * FGetComponentPath@20 (MAPI32.255)
856 * Return the installed component path, usually to the private mapi32.dll.
859 * component [I] Component ID
860 * qualifier [I] Application LCID
861 * dll_path [O] returned component path
862 * dll_path_length [I] component path length
863 * install [I] install mode
870 * Previously documented in Q229700 "How to locate the correct path
871 * to the Mapisvc.inf file in Microsoft Outlook".
873 BOOL WINAPI
FGetComponentPath(LPCSTR component
, LPCSTR qualifier
, LPSTR dll_path
,
874 DWORD dll_path_length
, BOOL install
)
879 TRACE("%s %s %p %u %d\n", component
, qualifier
, dll_path
, dll_path_length
, install
);
881 if (mapiFunctions
.FGetComponentPath
)
882 return mapiFunctions
.FGetComponentPath(component
, qualifier
, dll_path
, dll_path_length
, install
);
886 hmsi
= LoadLibraryA("msi.dll");
889 UINT (WINAPI
*pMsiProvideQualifiedComponentA
)(LPCSTR
, LPCSTR
, DWORD
, LPSTR
, LPDWORD
);
891 pMsiProvideQualifiedComponentA
= (void *)GetProcAddress(hmsi
, "MsiProvideQualifiedComponentA");
892 if (pMsiProvideQualifiedComponentA
)
894 static const char * const fmt
[] = { "%d\\NT", "%d\\95", "%d" };
898 for (i
= 0; i
< sizeof(fmt
)/sizeof(fmt
[0]); i
++)
900 /* FIXME: what's the correct behaviour here? */
901 if (!qualifier
|| qualifier
== lcid_ver
)
903 sprintf(lcid_ver
, fmt
[i
], GetUserDefaultUILanguage());
904 qualifier
= lcid_ver
;
907 if (pMsiProvideQualifiedComponentA(component
, qualifier
,
908 install
? INSTALLMODE_DEFAULT
: INSTALLMODE_EXISTING
,
909 dll_path
, &dll_path_length
) == ERROR_SUCCESS
)
915 if (qualifier
!= lcid_ver
) break;
923 /**************************************************************************
924 * HrQueryAllRows (MAPI32.75)
926 HRESULT WINAPI
HrQueryAllRows(LPMAPITABLE lpTable
, LPSPropTagArray lpPropTags
,
927 LPSRestriction lpRestriction
, LPSSortOrderSet lpSortOrderSet
,
928 LONG crowsMax
, LPSRowSet
*lppRows
)
930 if (mapiFunctions
.HrQueryAllRows
)
931 return mapiFunctions
.HrQueryAllRows(lpTable
, lpPropTags
, lpRestriction
, lpSortOrderSet
, crowsMax
, lppRows
);
933 FIXME("(%p, %p, %p, %p, %d, %p): stub\n", lpTable
, lpPropTags
, lpRestriction
, lpSortOrderSet
, crowsMax
, lppRows
);
935 return MAPI_E_CALL_FAILED
;
938 static HMODULE mapi_provider
;
939 static HMODULE mapi_ex_provider
;
941 /**************************************************************************
944 * Attempts to load a MAPI provider from the specified registry key.
946 * Returns a handle to the loaded module in `mapi_provider' if successful.
948 static void load_mapi_provider(HKEY hkeyMail
, LPCWSTR valueName
, HMODULE
*mapi_provider
)
950 static const WCHAR mapi32_dll
[] = {'m','a','p','i','3','2','.','d','l','l',0 };
952 DWORD dwType
, dwLen
= 0;
955 /* Check if we have a value set for DLLPath */
956 if ((RegQueryValueExW(hkeyMail
, valueName
, NULL
, &dwType
, NULL
, &dwLen
) == ERROR_SUCCESS
) &&
957 ((dwType
== REG_SZ
) || (dwType
== REG_EXPAND_SZ
)) && (dwLen
> 0))
959 dllPath
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
963 RegQueryValueExW(hkeyMail
, valueName
, NULL
, NULL
, (LPBYTE
)dllPath
, &dwLen
);
965 /* Check that this value doesn't refer to mapi32.dll (eg, as Outlook does) */
966 if (lstrcmpiW(dllPath
, mapi32_dll
) != 0)
968 if (dwType
== REG_EXPAND_SZ
)
971 LPWSTR dllPathExpanded
;
973 /* Expand the path if necessary */
974 dwExpandLen
= ExpandEnvironmentStringsW(dllPath
, NULL
, 0);
975 dllPathExpanded
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
) * dwExpandLen
+ 1);
979 ExpandEnvironmentStringsW(dllPath
, dllPathExpanded
, dwExpandLen
+ 1);
981 HeapFree(GetProcessHeap(), 0, dllPath
);
982 dllPath
= dllPathExpanded
;
987 TRACE("loading %s\n", debugstr_w(dllPath
));
988 *mapi_provider
= LoadLibraryW(dllPath
);
991 HeapFree(GetProcessHeap(), 0, dllPath
);
996 /**************************************************************************
997 * load_mapi_providers
999 * Scans the registry for MAPI providers and attempts to load a Simple and
1000 * Extended MAPI library.
1002 * Returns TRUE if at least one library loaded, FALSE otherwise.
1004 void load_mapi_providers(void)
1006 static const WCHAR regkey_mail
[] = {
1007 'S','o','f','t','w','a','r','e','\\','C','l','i','e','n','t','s','\\',
1008 'M','a','i','l',0 };
1010 static const WCHAR regkey_dllpath
[] = {'D','L','L','P','a','t','h',0 };
1011 static const WCHAR regkey_dllpath_ex
[] = {'D','L','L','P','a','t','h','E','x',0 };
1012 static const WCHAR regkey_backslash
[] = { '\\', 0 };
1015 DWORD dwType
, dwLen
= 0;
1016 LPWSTR appName
= NULL
, appKey
= NULL
;
1020 /* Open the Mail key */
1021 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, regkey_mail
, 0, KEY_READ
, &hkeyMail
) != ERROR_SUCCESS
)
1024 /* Check if we have a default value set, and the length of it */
1025 if ((RegQueryValueExW(hkeyMail
, NULL
, NULL
, &dwType
, NULL
, &dwLen
) != ERROR_SUCCESS
) ||
1026 !((dwType
== REG_SZ
) || (dwType
== REG_EXPAND_SZ
)) || (dwLen
== 0))
1029 appName
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
1034 /* Get the value, and get the path to the app key */
1035 RegQueryValueExW(hkeyMail
, NULL
, NULL
, NULL
, (LPBYTE
)appName
, &dwLen
);
1037 TRACE("appName: %s\n", debugstr_w(appName
));
1039 appKey
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
) * (lstrlenW(regkey_mail
) +
1040 lstrlenW(regkey_backslash
) + lstrlenW(appName
) + 1));
1045 lstrcpyW(appKey
, regkey_mail
);
1046 lstrcatW(appKey
, regkey_backslash
);
1047 lstrcatW(appKey
, appName
);
1049 RegCloseKey(hkeyMail
);
1051 TRACE("appKey: %s\n", debugstr_w(appKey
));
1053 /* Open the app's key */
1054 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, appKey
, 0, KEY_READ
, &hkeyMail
) != ERROR_SUCCESS
)
1057 /* Try to load the providers */
1058 load_mapi_provider(hkeyMail
, regkey_dllpath
, &mapi_provider
);
1059 load_mapi_provider(hkeyMail
, regkey_dllpath_ex
, &mapi_ex_provider
);
1061 /* Now try to load our function pointers */
1062 ZeroMemory(&mapiFunctions
, sizeof(mapiFunctions
));
1064 /* Simple MAPI functions */
1067 mapiFunctions
.MAPIAddress
= (void*) GetProcAddress(mapi_provider
, "MAPIAddress");
1068 mapiFunctions
.MAPIDeleteMail
= (void*) GetProcAddress(mapi_provider
, "MAPIDeleteMail");
1069 mapiFunctions
.MAPIDetails
= (void*) GetProcAddress(mapi_provider
, "MAPIDetails");
1070 mapiFunctions
.MAPIFindNext
= (void*) GetProcAddress(mapi_provider
, "MAPIFindNext");
1071 mapiFunctions
.MAPILogoff
= (void*) GetProcAddress(mapi_provider
, "MAPILogoff");
1072 mapiFunctions
.MAPILogon
= (void*) GetProcAddress(mapi_provider
, "MAPILogon");
1073 mapiFunctions
.MAPIReadMail
= (void*) GetProcAddress(mapi_provider
, "MAPIReadMail");
1074 mapiFunctions
.MAPIResolveName
= (void*) GetProcAddress(mapi_provider
, "MAPIResolveName");
1075 mapiFunctions
.MAPISaveMail
= (void*) GetProcAddress(mapi_provider
, "MAPISaveMail");
1076 mapiFunctions
.MAPISendDocuments
= (void*) GetProcAddress(mapi_provider
, "MAPISendDocuments");
1077 mapiFunctions
.MAPISendMail
= (void*) GetProcAddress(mapi_provider
, "MAPISendMail");
1080 /* Extended MAPI functions */
1081 if (mapi_ex_provider
)
1083 mapiFunctions
.MAPIInitialize
= (void*) GetProcAddress(mapi_ex_provider
, "MAPIInitialize");
1084 mapiFunctions
.MAPILogonEx
= (void*) GetProcAddress(mapi_ex_provider
, "MAPILogonEx");
1085 mapiFunctions
.MAPIUninitialize
= (void*) GetProcAddress(mapi_ex_provider
, "MAPIUninitialize");
1087 mapiFunctions
.DeinitMapiUtil
= (void*) GetProcAddress(mapi_ex_provider
, "DeinitMapiUtil@0");
1088 mapiFunctions
.DllCanUnloadNow
= (void*) GetProcAddress(mapi_ex_provider
, "DllCanUnloadNow");
1089 mapiFunctions
.DllGetClassObject
= (void*) GetProcAddress(mapi_ex_provider
, "DllGetClassObject");
1090 mapiFunctions
.FGetComponentPath
= (void*) GetProcAddress(mapi_ex_provider
, "FGetComponentPath");
1091 mapiFunctions
.HrThisThreadAdviseSink
= (void*) GetProcAddress(mapi_ex_provider
, "HrThisThreadAdviseSink@8");
1092 mapiFunctions
.HrQueryAllRows
= (void*) GetProcAddress(mapi_ex_provider
, "HrQueryAllRows@24");
1093 mapiFunctions
.MAPIAdminProfiles
= (void*) GetProcAddress(mapi_ex_provider
, "MAPIAdminProfiles");
1094 mapiFunctions
.MAPIAllocateBuffer
= (void*) GetProcAddress(mapi_ex_provider
, "MAPIAllocateBuffer");
1095 mapiFunctions
.MAPIAllocateMore
= (void*) GetProcAddress(mapi_ex_provider
, "MAPIAllocateMore");
1096 mapiFunctions
.MAPIFreeBuffer
= (void*) GetProcAddress(mapi_ex_provider
, "MAPIFreeBuffer");
1097 mapiFunctions
.MAPIGetDefaultMalloc
= (void*) GetProcAddress(mapi_ex_provider
, "MAPIGetDefaultMalloc@0");
1098 mapiFunctions
.MAPIOpenLocalFormContainer
= (void *) GetProcAddress(mapi_ex_provider
, "MAPIOpenLocalFormContainer");
1099 mapiFunctions
.OpenStreamOnFile
= (void*) GetProcAddress(mapi_ex_provider
, "OpenStreamOnFile@24");
1100 mapiFunctions
.ScInitMapiUtil
= (void*) GetProcAddress(mapi_ex_provider
, "ScInitMapiUtil@4");
1104 RegCloseKey(hkeyMail
);
1105 HeapFree(GetProcessHeap(), 0, appKey
);
1106 HeapFree(GetProcessHeap(), 0, appName
);
1109 /**************************************************************************
1110 * unload_mapi_providers
1112 * Unloads any loaded MAPI libraries.
1114 void unload_mapi_providers(void)
1118 FreeLibrary(mapi_provider
);
1119 FreeLibrary(mapi_ex_provider
);