push 6c2bed22d4a356b01aae243fbf554b5dba1af534
[wine/hacks.git] / dlls / mapi32 / util.c
blob4957941f0b00c2cbb7b6e05a49ea5b1678224d18
1 /*
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
22 #include <stdarg.h>
23 #include <stdio.h>
25 #define COBJMACROS
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winreg.h"
31 #include "winuser.h"
32 #include "winerror.h"
33 #include "winternl.h"
34 #include "objbase.h"
35 #include "shlwapi.h"
36 #include "wine/debug.h"
37 #include "wine/unicode.h"
38 #include "mapival.h"
39 #include "xcmc.h"
40 #include "msi.h"
41 #include "util.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,
49 14,15 };
51 MAPI_FUNCTIONS mapiFunctions;
53 /**************************************************************************
54 * ScInitMapiUtil (MAPI32.33)
56 * Initialise Mapi utility functions.
58 * PARAMS
59 * ulReserved [I] Reserved, pass 0.
61 * RETURNS
62 * Success: S_OK. Mapi utility functions may be called.
63 * Failure: MAPI_E_INVALID_PARAMETER, if ulReserved is not 0.
65 * NOTES
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 FIXME("(0x%08x)stub!\n", ulReserved);
72 if (ulReserved)
73 return MAPI_E_INVALID_PARAMETER;
74 return S_OK;
77 /**************************************************************************
78 * DeinitMapiUtil (MAPI32.34)
80 * Uninitialise Mapi utility functions.
82 * PARAMS
83 * None.
85 * RETURNS
86 * Nothing.
88 * NOTES
89 * Your application does not need to call this function unless it does not
90 * call MAPIInitialize()/MAPIUninitialize().
92 VOID WINAPI DeinitMapiUtil(void)
94 FIXME("()stub!\n");
97 typedef LPVOID *LPMAPIALLOCBUFFER;
99 /**************************************************************************
100 * MAPIAllocateBuffer (MAPI32.12)
101 * MAPIAllocateBuffer@8 (MAPI32.13)
103 * Allocate a block of memory.
105 * PARAMS
106 * cbSize [I] Size of the block to allocate in bytes
107 * lppBuffer [O] Destination for pointer to allocated memory
109 * RETURNS
110 * Success: S_OK. *lppBuffer is filled with a pointer to a memory block of
111 * length cbSize bytes.
112 * Failure: MAPI_E_INVALID_PARAMETER, if lppBuffer is NULL.
113 * MAPI_E_NOT_ENOUGH_MEMORY, if the memory allocation fails.
115 * NOTES
116 * Memory allocated with this function should be freed with MAPIFreeBuffer().
117 * Further allocations of memory may be linked to the pointer returned using
118 * MAPIAllocateMore(). Linked allocations are freed when the initial pointer
119 * is feed.
121 SCODE WINAPI MAPIAllocateBuffer(ULONG cbSize, LPVOID *lppBuffer)
123 LPMAPIALLOCBUFFER lpBuff;
125 TRACE("(%d,%p)\n", cbSize, lppBuffer);
127 if (!lppBuffer)
128 return E_INVALIDARG;
130 lpBuff = HeapAlloc(GetProcessHeap(), 0, cbSize + sizeof(*lpBuff));
131 if (!lpBuff)
132 return MAPI_E_NOT_ENOUGH_MEMORY;
134 TRACE("initial allocation:%p, returning %p\n", lpBuff, lpBuff + 1);
135 *lpBuff++ = NULL;
136 *lppBuffer = lpBuff;
137 return S_OK;
140 /**************************************************************************
141 * MAPIAllocateMore (MAPI32.14)
142 * MAPIAllocateMore@12 (MAPI32.15)
144 * Allocate a block of memory linked to a previous allocation.
146 * PARAMS
147 * cbSize [I] Size of the block to allocate in bytes
148 * lpOrig [I] Initial allocation to link to, from MAPIAllocateBuffer()
149 * lppBuffer [O] Destination for pointer to allocated memory
151 * RETURNS
152 * Success: S_OK. *lppBuffer is filled with a pointer to a memory block of
153 * length cbSize bytes.
154 * Failure: MAPI_E_INVALID_PARAMETER, if lpOrig or lppBuffer is invalid.
155 * MAPI_E_NOT_ENOUGH_MEMORY, if memory allocation fails.
157 * NOTES
158 * Memory allocated with this function and stored in *lppBuffer is freed
159 * when lpOrig is passed to MAPIFreeBuffer(). It should not be freed independently.
161 SCODE WINAPI MAPIAllocateMore(ULONG cbSize, LPVOID lpOrig, LPVOID *lppBuffer)
163 LPMAPIALLOCBUFFER lpBuff = lpOrig;
165 TRACE("(%d,%p,%p)\n", cbSize, lpOrig, lppBuffer);
167 if (!lppBuffer || !lpBuff || !--lpBuff)
168 return E_INVALIDARG;
170 /* Find the last allocation in the chain */
171 while (*lpBuff)
173 TRACE("linked:%p->%p\n", lpBuff, *lpBuff);
174 lpBuff = *lpBuff;
177 if (SUCCEEDED(MAPIAllocateBuffer(cbSize, lppBuffer)))
179 *lpBuff = ((LPMAPIALLOCBUFFER)*lppBuffer) - 1;
180 TRACE("linking %p->%p\n", lpBuff, *lpBuff);
182 return *lppBuffer ? S_OK : MAPI_E_NOT_ENOUGH_MEMORY;
185 /**************************************************************************
186 * MAPIFreeBuffer (MAPI32.16)
187 * MAPIFreeBuffer@4 (MAPI32.17)
189 * Free a block of memory and any linked allocations associated with it.
191 * PARAMS
192 * lpBuffer [I] Memory to free, returned from MAPIAllocateBuffer()
194 * RETURNS
195 * S_OK.
197 ULONG WINAPI MAPIFreeBuffer(LPVOID lpBuffer)
199 LPMAPIALLOCBUFFER lpBuff = lpBuffer;
201 TRACE("(%p)\n", lpBuffer);
203 if (lpBuff && --lpBuff)
205 while (lpBuff)
207 LPVOID lpFree = lpBuff;
209 lpBuff = *lpBuff;
211 TRACE("linked:%p->%p, freeing %p\n", lpFree, lpBuff, lpFree);
212 HeapFree(GetProcessHeap(), 0, lpFree);
215 return S_OK;
218 /**************************************************************************
219 * WrapProgress@20 (MAPI32.41)
221 HRESULT WINAPI WrapProgress(PVOID unk1, PVOID unk2, PVOID unk3, PVOID unk4, PVOID unk5)
223 /* Native does not implement this function */
224 return MAPI_E_NO_SUPPORT;
227 /*************************************************************************
228 * HrThisThreadAdviseSink@8 (MAPI32.42)
230 * Ensure that an advise sink is only notified in its originating thread.
232 * PARAMS
233 * lpSink [I] IMAPIAdviseSink interface to be protected
234 * lppNewSink [I] Destination for wrapper IMAPIAdviseSink interface
236 * RETURNS
237 * Success: S_OK. *lppNewSink contains a new sink to use in place of lpSink.
238 * Failure: E_INVALIDARG, if any parameter is invalid.
240 HRESULT WINAPI HrThisThreadAdviseSink(LPMAPIADVISESINK lpSink, LPMAPIADVISESINK* lppNewSink)
242 FIXME("(%p,%p)semi-stub\n", lpSink, lppNewSink);
244 if (!lpSink || !lppNewSink)
245 return E_INVALIDARG;
247 /* Don't wrap the sink for now, just copy it */
248 *lppNewSink = lpSink;
249 IMAPIAdviseSink_AddRef(lpSink);
250 return S_OK;
253 /*************************************************************************
254 * FBinFromHex (MAPI32.44)
256 * Create an array of binary data from a string.
258 * PARAMS
259 * lpszHex [I] String to convert to binary data
260 * lpOut [O] Destination for resulting binary data
262 * RETURNS
263 * Success: TRUE. lpOut contains the decoded binary data.
264 * Failure: FALSE, if lpszHex does not represent a binary string.
266 * NOTES
267 * - lpOut must be at least half the length of lpszHex in bytes.
268 * - Although the Mapi headers prototype this function as both
269 * Ascii and Unicode, there is only one (Ascii) implementation. This
270 * means that lpszHex is treated as an Ascii string (i.e. a single NUL
271 * character in the byte stream terminates the string).
273 BOOL WINAPI FBinFromHex(LPWSTR lpszHex, LPBYTE lpOut)
275 LPSTR lpStr = (LPSTR)lpszHex;
277 TRACE("(%p,%p)\n", lpszHex, lpOut);
279 while (*lpStr)
281 if (lpStr[0] < '0' || lpStr[0] > 'f' || digitsToHex[lpStr[0] - '0'] == 0xff ||
282 lpStr[1] < '0' || lpStr[1] > 'f' || digitsToHex[lpStr[1] - '0'] == 0xff)
283 return FALSE;
285 *lpOut++ = (digitsToHex[lpStr[0] - '0'] << 4) | digitsToHex[lpStr[1] - '0'];
286 lpStr += 2;
288 return TRUE;
291 /*************************************************************************
292 * HexFromBin (MAPI32.45)
294 * Create a string from an array of binary data.
296 * PARAMS
297 * lpHex [I] Binary data to convert to string
298 * iCount [I] Length of lpHex in bytes
299 * lpszOut [O] Destination for resulting hex string
301 * RETURNS
302 * Nothing.
304 * NOTES
305 * - lpszOut must be at least 2 * iCount + 1 bytes characters long.
306 * - Although the Mapi headers prototype this function as both
307 * Ascii and Unicode, there is only one (Ascii) implementation. This
308 * means that the resulting string is not properly NUL terminated
309 * if the caller expects it to be a Unicode string.
311 void WINAPI HexFromBin(LPBYTE lpHex, int iCount, LPWSTR lpszOut)
313 static const char hexDigits[] = { "0123456789ABCDEF" };
314 LPSTR lpStr = (LPSTR)lpszOut;
316 TRACE("(%p,%d,%p)\n", lpHex, iCount, lpszOut);
318 while (iCount-- > 0)
320 *lpStr++ = hexDigits[*lpHex >> 4];
321 *lpStr++ = hexDigits[*lpHex & 0xf];
322 lpHex++;
324 *lpStr = '\0';
327 /*************************************************************************
328 * SwapPlong@8 (MAPI32.47)
330 * Swap the bytes in a ULONG array.
332 * PARAMS
333 * lpData [O] Array to swap bytes in
334 * ulLen [I] Number of ULONG element to swap the bytes of
336 * RETURNS
337 * Nothing.
339 VOID WINAPI SwapPlong(PULONG lpData, ULONG ulLen)
341 ULONG i;
343 for (i = 0; i < ulLen; i++)
344 lpData[i] = RtlUlongByteSwap(lpData[i]);
347 /*************************************************************************
348 * SwapPword@8 (MAPI32.48)
350 * Swap the bytes in a USHORT array.
352 * PARAMS
353 * lpData [O] Array to swap bytes in
354 * ulLen [I] Number of USHORT element to swap the bytes of
356 * RETURNS
357 * Nothing.
359 VOID WINAPI SwapPword(PUSHORT lpData, ULONG ulLen)
361 ULONG i;
363 for (i = 0; i < ulLen; i++)
364 lpData[i] = RtlUshortByteSwap(lpData[i]);
367 /**************************************************************************
368 * MNLS_lstrlenW@4 (MAPI32.62)
370 * Calculate the length of a Unicode string.
372 * PARAMS
373 * lpszStr [I] String to calculate the length of
375 * RETURNS
376 * The length of lpszStr in Unicode characters.
378 ULONG WINAPI MNLS_lstrlenW(LPCWSTR lpszStr)
380 TRACE("(%s)\n", debugstr_w(lpszStr));
381 return strlenW(lpszStr);
384 /*************************************************************************
385 * MNLS_lstrcmpW@8 (MAPI32.63)
387 * Compare two Unicode strings.
389 * PARAMS
390 * lpszLeft [I] First string to compare
391 * lpszRight [I] Second string to compare
393 * RETURNS
394 * An integer less than, equal to or greater than 0, indicating that
395 * lpszLeft is less than, the same, or greater than lpszRight.
397 INT WINAPI MNLS_lstrcmpW(LPCWSTR lpszLeft, LPCWSTR lpszRight)
399 TRACE("(%s,%s)\n", debugstr_w(lpszLeft), debugstr_w(lpszRight));
400 return strcmpW(lpszLeft, lpszRight);
403 /*************************************************************************
404 * MNLS_lstrcpyW@8 (MAPI32.64)
406 * Copy a Unicode string to another string.
408 * PARAMS
409 * lpszDest [O] Destination string
410 * lpszSrc [I] Source string
412 * RETURNS
413 * The length lpszDest in Unicode characters.
415 ULONG WINAPI MNLS_lstrcpyW(LPWSTR lpszDest, LPCWSTR lpszSrc)
417 ULONG len;
419 TRACE("(%p,%s)\n", lpszDest, debugstr_w(lpszSrc));
420 len = (strlenW(lpszSrc) + 1) * sizeof(WCHAR);
421 memcpy(lpszDest, lpszSrc, len);
422 return len;
425 /*************************************************************************
426 * MNLS_CompareStringW@12 (MAPI32.65)
428 * Compare two Unicode strings.
430 * PARAMS
431 * dwCp [I] Code page for the comparison
432 * lpszLeft [I] First string to compare
433 * lpszRight [I] Second string to compare
435 * RETURNS
436 * CSTR_LESS_THAN, CSTR_EQUAL or CSTR_GREATER_THAN, indicating that
437 * lpszLeft is less than, the same, or greater than lpszRight.
439 INT WINAPI MNLS_CompareStringW(DWORD dwCp, LPCWSTR lpszLeft, LPCWSTR lpszRight)
441 INT ret;
443 TRACE("0x%08x,%s,%s\n", dwCp, debugstr_w(lpszLeft), debugstr_w(lpszRight));
444 ret = MNLS_lstrcmpW(lpszLeft, lpszRight);
445 return ret < 0 ? CSTR_LESS_THAN : ret ? CSTR_GREATER_THAN : CSTR_EQUAL;
448 /**************************************************************************
449 * FEqualNames@8 (MAPI32.72)
451 * Compare two Mapi names.
453 * PARAMS
454 * lpName1 [I] First name to compare to lpName2
455 * lpName2 [I] Second name to compare to lpName1
457 * RETURNS
458 * TRUE, if the names are the same,
459 * FALSE, Otherwise.
461 BOOL WINAPI FEqualNames(LPMAPINAMEID lpName1, LPMAPINAMEID lpName2)
463 TRACE("(%p,%p)\n", lpName1, lpName2);
465 if (!lpName1 || !lpName2 ||
466 !IsEqualGUID(lpName1->lpguid, lpName2->lpguid) ||
467 lpName1->ulKind != lpName2->ulKind)
468 return FALSE;
470 if (lpName1->ulKind == MNID_STRING)
471 return !strcmpW(lpName1->Kind.lpwstrName, lpName2->Kind.lpwstrName);
473 return lpName1->Kind.lID == lpName2->Kind.lID ? TRUE : FALSE;
476 /**************************************************************************
477 * IsBadBoundedStringPtr@8 (MAPI32.71)
479 * Determine if a string pointer is valid.
481 * PARAMS
482 * lpszStr [I] String to check
483 * ulLen [I] Maximum length of lpszStr
485 * RETURNS
486 * TRUE, if lpszStr is invalid or longer than ulLen,
487 * FALSE, otherwise.
489 BOOL WINAPI IsBadBoundedStringPtr(LPCSTR lpszStr, ULONG ulLen)
491 if (!lpszStr || IsBadStringPtrA(lpszStr, -1) || strlen(lpszStr) >= ulLen)
492 return TRUE;
493 return FALSE;
496 /**************************************************************************
497 * FtAddFt@16 (MAPI32.121)
499 * Add two FILETIME's together.
501 * PARAMS
502 * ftLeft [I] FILETIME to add to ftRight
503 * ftRight [I] FILETIME to add to ftLeft
505 * RETURNS
506 * The sum of ftLeft and ftRight
508 LONGLONG WINAPI MAPI32_FtAddFt(FILETIME ftLeft, FILETIME ftRight)
510 LONGLONG *pl = (LONGLONG*)&ftLeft, *pr = (LONGLONG*)&ftRight;
512 return *pl + *pr;
515 /**************************************************************************
516 * FtSubFt@16 (MAPI32.123)
518 * Subtract two FILETIME's together.
520 * PARAMS
521 * ftLeft [I] Initial FILETIME
522 * ftRight [I] FILETIME to subtract from ftLeft
524 * RETURNS
525 * The remainder after ftRight is subtracted from ftLeft.
527 LONGLONG WINAPI MAPI32_FtSubFt(FILETIME ftLeft, FILETIME ftRight)
529 LONGLONG *pl = (LONGLONG*)&ftLeft, *pr = (LONGLONG*)&ftRight;
531 return *pr - *pl;
534 /**************************************************************************
535 * FtMulDw@12 (MAPI32.124)
537 * Multiply a FILETIME by a DWORD.
539 * PARAMS
540 * dwLeft [I] DWORD to multiply with ftRight
541 * ftRight [I] FILETIME to multiply with dwLeft
543 * RETURNS
544 * The product of dwLeft and ftRight
546 LONGLONG WINAPI MAPI32_FtMulDw(DWORD dwLeft, FILETIME ftRight)
548 LONGLONG *pr = (LONGLONG*)&ftRight;
550 return (LONGLONG)dwLeft * (*pr);
553 /**************************************************************************
554 * FtMulDwDw@8 (MAPI32.125)
556 * Multiply two DWORD, giving the result as a FILETIME.
558 * PARAMS
559 * dwLeft [I] DWORD to multiply with dwRight
560 * dwRight [I] DWORD to multiply with dwLeft
562 * RETURNS
563 * The product of ftMultiplier and ftMultiplicand as a FILETIME.
565 LONGLONG WINAPI MAPI32_FtMulDwDw(DWORD dwLeft, DWORD dwRight)
567 return (LONGLONG)dwLeft * (LONGLONG)dwRight;
570 /**************************************************************************
571 * FtNegFt@8 (MAPI32.126)
573 * Negate a FILETIME.
575 * PARAMS
576 * ft [I] FILETIME to negate
578 * RETURNS
579 * The negation of ft.
581 LONGLONG WINAPI MAPI32_FtNegFt(FILETIME ft)
583 LONGLONG *p = (LONGLONG*)&ft;
585 return - *p;
588 /**************************************************************************
589 * UlAddRef@4 (MAPI32.128)
591 * Add a reference to an object.
593 * PARAMS
594 * lpUnk [I] Object to add a reference to.
596 * RETURNS
597 * The new reference count of the object, or 0 if lpUnk is NULL.
599 * NOTES
600 * See IUnknown_AddRef.
602 ULONG WINAPI UlAddRef(void *lpUnk)
604 TRACE("(%p)\n", lpUnk);
606 if (!lpUnk)
607 return 0UL;
608 return IUnknown_AddRef((LPUNKNOWN)lpUnk);
611 /**************************************************************************
612 * UlRelease@4 (MAPI32.129)
614 * Remove a reference from an object.
616 * PARAMS
617 * lpUnk [I] Object to remove reference from.
619 * RETURNS
620 * The new reference count of the object, or 0 if lpUnk is NULL. If lpUnk is
621 * non-NULL and this function returns 0, the object pointed to by lpUnk has
622 * been released.
624 * NOTES
625 * See IUnknown_Release.
627 ULONG WINAPI UlRelease(void *lpUnk)
629 TRACE("(%p)\n", lpUnk);
631 if (!lpUnk)
632 return 0UL;
633 return IUnknown_Release((LPUNKNOWN)lpUnk);
636 /**************************************************************************
637 * UFromSz@4 (MAPI32.133)
639 * Read an integer from a string
641 * PARAMS
642 * lpszStr [I] String to read the integer from.
644 * RETURNS
645 * Success: The integer read from lpszStr.
646 * Failure: 0, if the first character in lpszStr is not 0-9.
648 * NOTES
649 * This function does not accept whitespace and stops at the first non-digit
650 * character.
652 UINT WINAPI UFromSz(LPCSTR lpszStr)
654 ULONG ulRet = 0;
656 TRACE("(%s)\n", debugstr_a(lpszStr));
658 if (lpszStr)
660 while (*lpszStr >= '0' && *lpszStr <= '9')
662 ulRet = ulRet * 10 + (*lpszStr - '0');
663 lpszStr++;
666 return ulRet;
669 /*************************************************************************
670 * OpenStreamOnFile@24 (MAPI32.147)
672 * Create a stream on a file.
674 * PARAMS
675 * lpAlloc [I] Memory allocation function
676 * lpFree [I] Memory free function
677 * ulFlags [I] Flags controlling the opening process
678 * lpszPath [I] Path of file to create stream on
679 * lpszPrefix [I] Prefix of the temporary file name (if ulFlags includes SOF_UNIQUEFILENAME)
680 * lppStream [O] Destination for created stream
682 * RETURNS
683 * Success: S_OK. lppStream contains the new stream object
684 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
685 * describing the error.
687 HRESULT WINAPI OpenStreamOnFile(LPALLOCATEBUFFER lpAlloc, LPFREEBUFFER lpFree,
688 ULONG ulFlags, LPWSTR lpszPath, LPWSTR lpszPrefix,
689 LPSTREAM *lppStream)
691 WCHAR szBuff[MAX_PATH];
692 DWORD dwMode = STGM_READWRITE, dwAttributes = 0;
693 HRESULT hRet;
695 TRACE("(%p,%p,0x%08x,%s,%s,%p)\n", lpAlloc, lpFree, ulFlags,
696 debugstr_a((LPSTR)lpszPath), debugstr_a((LPSTR)lpszPrefix), lppStream);
698 if (lppStream)
699 *lppStream = NULL;
701 if (ulFlags & SOF_UNIQUEFILENAME)
703 FIXME("Should generate a temporary name\n");
704 return E_INVALIDARG;
707 if (!lpszPath || !lppStream)
708 return E_INVALIDARG;
710 /* FIXME: Should probably munge mode and attributes, and should handle
711 * Unicode arguments (I assume MAPI_UNICODE is set in ulFlags if
712 * we are being passed Unicode strings; MSDN doesn't say).
713 * This implementation is just enough for Outlook97 to start.
715 MultiByteToWideChar(CP_ACP, 0, (LPSTR)lpszPath, -1, szBuff, MAX_PATH);
716 hRet = SHCreateStreamOnFileEx(szBuff, dwMode, dwAttributes, TRUE,
717 NULL, lppStream);
718 return hRet;
721 /*************************************************************************
722 * UlFromSzHex@4 (MAPI32.155)
724 * Read an integer from a hexadecimal string.
726 * PARAMS
727 * lpSzHex [I] String containing the hexadecimal number to read
729 * RETURNS
730 * Success: The number represented by lpszHex.
731 * Failure: 0, if lpszHex does not contain a hex string.
733 * NOTES
734 * This function does not accept whitespace and stops at the first non-hex
735 * character.
737 ULONG WINAPI UlFromSzHex(LPCWSTR lpszHex)
739 LPCSTR lpStr = (LPCSTR)lpszHex;
740 ULONG ulRet = 0;
742 TRACE("(%s)\n", debugstr_a(lpStr));
744 while (*lpStr)
746 if (lpStr[0] < '0' || lpStr[0] > 'f' || digitsToHex[lpStr[0] - '0'] == 0xff ||
747 lpStr[1] < '0' || lpStr[1] > 'f' || digitsToHex[lpStr[1] - '0'] == 0xff)
748 break;
750 ulRet = ulRet * 16 + ((digitsToHex[lpStr[0] - '0'] << 4) | digitsToHex[lpStr[1] - '0']);
751 lpStr += 2;
753 return ulRet;
756 /************************************************************************
757 * FBadEntryList@4 (MAPI32.190)
759 * Determine is an entry list is invalid.
761 * PARAMS
762 * lpEntryList [I] List to check
764 * RETURNS
765 * TRUE, if lpEntryList is invalid,
766 * FALSE, otherwise.
768 BOOL WINAPI FBadEntryList(LPENTRYLIST lpEntryList)
770 ULONG i;
772 if (IsBadReadPtr(lpEntryList, sizeof(*lpEntryList)) ||
773 IsBadReadPtr(lpEntryList->lpbin,
774 lpEntryList->cValues * sizeof(*lpEntryList->lpbin)))
775 return TRUE;
777 for (i = 0; i < lpEntryList->cValues; i++)
778 if(IsBadReadPtr(lpEntryList->lpbin[i].lpb, lpEntryList->lpbin[i].cb))
779 return TRUE;
781 return FALSE;
784 /*************************************************************************
785 * CbOfEncoded@4 (MAPI32.207)
787 * Return the length of an encoded string.
789 * PARAMS
790 * lpSzEnc [I] Encoded string to get the length of.
792 * RETURNS
793 * The length of the encoded string in bytes.
795 ULONG WINAPI CbOfEncoded(LPCSTR lpszEnc)
797 ULONG ulRet = 0;
799 TRACE("(%s)\n", debugstr_a(lpszEnc));
801 if (lpszEnc)
802 ulRet = (((strlen(lpszEnc) | 3) >> 2) + 1) * 3;
803 return ulRet;
806 /*************************************************************************
807 * cmc_query_configuration (MAPI32.235)
809 * Retrieves the configuration information for the installed CMC
811 * PARAMS
812 * session [I] MAPI session handle
813 * item [I] Enumerated variable that identifies which
814 * configuration information is being requested
815 * reference [O] Buffer where configuration information is written
816 * config_extensions[I/O] Path of file to create stream on
818 * RETURNS
819 * A CMD define
821 CMC_return_code WINAPI cmc_query_configuration(
822 CMC_session_id session,
823 CMC_enum item,
824 CMC_buffer reference,
825 CMC_extension *config_extensions)
827 FIXME("stub\n");
828 return CMC_E_NOT_SUPPORTED;
831 /**************************************************************************
832 * FGetComponentPath (MAPI32.254)
833 * FGetComponentPath@20 (MAPI32.255)
835 * Return the installed component path, usually to the private mapi32.dll.
837 * PARAMS
838 * component [I] Component ID
839 * qualifier [I] Application LCID
840 * dll_path [O] returned component path
841 * dll_path_length [I] component path length
842 * install [I] install mode
844 * RETURNS
845 * Success: TRUE.
846 * Failure: FALSE.
848 * NOTES
849 * Previously documented in Q229700 "How to locate the correct path
850 * to the Mapisvc.inf file in Microsoft Outlook".
852 BOOL WINAPI FGetComponentPath(LPCSTR component, LPCSTR qualifier, LPSTR dll_path,
853 DWORD dll_path_length, BOOL install)
855 BOOL ret = FALSE;
856 HMODULE hmsi;
858 TRACE("%s %s %p %u %d\n", component, qualifier, dll_path, dll_path_length, install);
860 dll_path[0] = 0;
862 hmsi = LoadLibraryA("msi.dll");
863 if (hmsi)
865 UINT (WINAPI *pMsiProvideQualifiedComponentA)(LPCSTR, LPCSTR, DWORD, LPSTR, LPDWORD);
867 pMsiProvideQualifiedComponentA = (void *)GetProcAddress(hmsi, "MsiProvideQualifiedComponentA");
868 if (pMsiProvideQualifiedComponentA)
870 static const char * const fmt[] = { "%d\\NT", "%d\\95", "%d" };
871 char lcid_ver[20];
872 UINT i;
874 for (i = 0; i < sizeof(fmt)/sizeof(fmt[0]); i++)
876 /* FIXME: what's the correct behaviour here? */
877 if (!qualifier || qualifier == lcid_ver)
879 sprintf(lcid_ver, fmt[i], GetUserDefaultUILanguage());
880 qualifier = lcid_ver;
883 if (pMsiProvideQualifiedComponentA(component, qualifier,
884 install ? INSTALLMODE_DEFAULT : INSTALLMODE_EXISTING,
885 dll_path, &dll_path_length) == ERROR_SUCCESS)
887 ret = TRUE;
888 break;
891 if (qualifier != lcid_ver) break;
894 FreeLibrary(hmsi);
896 return ret;
899 /**************************************************************************
900 * HrQueryAllRows (MAPI32.75)
902 HRESULT WINAPI HrQueryAllRows(LPMAPITABLE lpTable, LPSPropTagArray lpPropTags,
903 LPSRestriction lpRestriction, LPSSortOrderSet lpSortOrderSet,
904 LONG crowsMax, LPSRowSet *lppRows)
906 FIXME("(%p, %p, %p, %p, %d, %p): stub\n", lpTable, lpPropTags, lpRestriction, lpSortOrderSet, crowsMax, lppRows);
907 *lppRows = NULL;
908 return MAPI_E_CALL_FAILED;
911 static HMODULE mapi_provider;
912 static HMODULE mapi_ex_provider;
914 /**************************************************************************
915 * load_mapi_provider
917 * Attempts to load a MAPI provider from the specified registry key.
919 * Returns a handle to the loaded module in `mapi_provider' if successful.
921 static void load_mapi_provider(HKEY hkeyMail, LPCWSTR valueName, HMODULE *mapi_provider)
923 static const WCHAR mapi32_dll[] = {'m','a','p','i','3','2','.','d','l','l',0 };
925 DWORD dwType, dwLen = 0;
926 LPWSTR dllPath;
928 /* Check if we have a value set for DLLPath */
929 if ((RegQueryValueExW(hkeyMail, valueName, NULL, &dwType, NULL, &dwLen) == ERROR_SUCCESS) &&
930 ((dwType == REG_SZ) || (dwType == REG_EXPAND_SZ)) && (dwLen > 0))
932 dllPath = HeapAlloc(GetProcessHeap(), 0, dwLen);
934 if (dllPath)
936 RegQueryValueExW(hkeyMail, valueName, NULL, NULL, (LPBYTE)dllPath, &dwLen);
938 /* Check that this value doesn't refer to mapi32.dll (eg, as Outlook does) */
939 if (lstrcmpiW(dllPath, mapi32_dll) != 0)
941 if (dwType == REG_EXPAND_SZ)
943 DWORD dwExpandLen;
944 LPWSTR dllPathExpanded;
946 /* Expand the path if necessary */
947 dwExpandLen = ExpandEnvironmentStringsW(dllPath, NULL, 0);
948 dllPathExpanded = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * dwExpandLen + 1);
950 if (dllPathExpanded)
952 ExpandEnvironmentStringsW(dllPath, dllPathExpanded, dwExpandLen + 1);
954 HeapFree(GetProcessHeap(), 0, dllPath);
955 dllPath = dllPathExpanded;
959 /* Load the DLL */
960 TRACE("loading %s\n", debugstr_w(dllPath));
961 *mapi_provider = LoadLibraryW(dllPath);
964 HeapFree(GetProcessHeap(), 0, dllPath);
969 /**************************************************************************
970 * load_mapi_providers
972 * Scans the registry for MAPI providers and attempts to load a Simple and
973 * Extended MAPI library.
975 * Returns TRUE if at least one library loaded, FALSE otherwise.
977 void load_mapi_providers(void)
979 static const WCHAR regkey_mail[] = {
980 'S','o','f','t','w','a','r','e','\\','C','l','i','e','n','t','s','\\',
981 'M','a','i','l',0 };
983 static const WCHAR regkey_dllpath[] = {'D','L','L','P','a','t','h',0 };
984 static const WCHAR regkey_dllpath_ex[] = {'D','L','L','P','a','t','h','E','x',0 };
985 static const WCHAR regkey_backslash[] = { '\\', 0 };
987 HKEY hkeyMail;
988 DWORD dwType, dwLen = 0;
989 LPWSTR appName = NULL, appKey = NULL;
991 TRACE("()\n");
993 /* Open the Mail key */
994 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, regkey_mail, 0, KEY_READ, &hkeyMail) != ERROR_SUCCESS)
995 return;
997 /* Check if we have a default value set, and the length of it */
998 if ((RegQueryValueExW(hkeyMail, NULL, NULL, &dwType, NULL, &dwLen) != ERROR_SUCCESS) ||
999 !((dwType == REG_SZ) || (dwType == REG_EXPAND_SZ)) || (dwLen == 0))
1000 goto cleanUp;
1002 appName = HeapAlloc(GetProcessHeap(), 0, dwLen);
1004 if (!appName)
1005 goto cleanUp;
1007 /* Get the value, and get the path to the app key */
1008 RegQueryValueExW(hkeyMail, NULL, NULL, NULL, (LPBYTE)appName, &dwLen);
1010 TRACE("appName: %s\n", debugstr_w(appName));
1012 appKey = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * (lstrlenW(regkey_mail) +
1013 lstrlenW(regkey_backslash) + lstrlenW(appName)));
1015 if (!appKey)
1016 goto cleanUp;
1018 lstrcpyW(appKey, regkey_mail);
1019 lstrcatW(appKey, regkey_backslash);
1020 lstrcatW(appKey, appName);
1022 RegCloseKey(hkeyMail);
1024 TRACE("appKey: %s\n", debugstr_w(appKey));
1026 /* Open the app's key */
1027 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, appKey, 0, KEY_READ, &hkeyMail) != ERROR_SUCCESS)
1028 goto cleanUp;
1030 /* Try to load the providers */
1031 load_mapi_provider(hkeyMail, regkey_dllpath, &mapi_provider);
1032 load_mapi_provider(hkeyMail, regkey_dllpath_ex, &mapi_ex_provider);
1034 /* Now try to load our function pointers */
1035 ZeroMemory(&mapiFunctions, sizeof(mapiFunctions));
1037 /* Simple MAPI functions */
1038 if (mapi_provider)
1040 mapiFunctions.MAPIAddress = (void*) GetProcAddress(mapi_provider, "MAPIAddress");
1041 mapiFunctions.MAPIDeleteMail = (void*) GetProcAddress(mapi_provider, "MAPIDeleteMail");
1042 mapiFunctions.MAPIDetails = (void*) GetProcAddress(mapi_provider, "MAPIDetails");
1043 mapiFunctions.MAPIFindNext = (void*) GetProcAddress(mapi_provider, "MAPIFindNext");
1044 mapiFunctions.MAPILogoff = (void*) GetProcAddress(mapi_provider, "MAPILogoff");
1045 mapiFunctions.MAPILogon = (void*) GetProcAddress(mapi_provider, "MAPILogon");
1046 mapiFunctions.MAPIReadMail = (void*) GetProcAddress(mapi_provider, "MAPIReadMail");
1047 mapiFunctions.MAPIResolveName = (void*) GetProcAddress(mapi_provider, "MAPIResolveName");
1048 mapiFunctions.MAPISaveMail = (void*) GetProcAddress(mapi_provider, "MAPISaveMail");
1049 mapiFunctions.MAPISendDocuments = (void*) GetProcAddress(mapi_provider, "MAPISendDocuments");
1050 mapiFunctions.MAPISendMail = (void*) GetProcAddress(mapi_provider, "MAPISendMail");
1053 /* Extended MAPI functions */
1054 if (mapi_ex_provider)
1056 mapiFunctions.MAPIInitialize = (void*) GetProcAddress(mapi_ex_provider, "MAPIInitialize");
1057 mapiFunctions.MAPILogonEx = (void*) GetProcAddress(mapi_ex_provider, "MAPILogonEx");
1058 mapiFunctions.MAPIUninitialize = (void*) GetProcAddress(mapi_ex_provider, "MAPIUninitialize");
1061 cleanUp:
1062 RegCloseKey(hkeyMail);
1063 HeapFree(GetProcessHeap(), 0, appKey);
1064 HeapFree(GetProcessHeap(), 0, appName);
1067 /**************************************************************************
1068 * unload_mapi_providers
1070 * Unloads any loaded MAPI libraries.
1072 void unload_mapi_providers(void)
1074 TRACE("()\n");
1076 FreeLibrary(mapi_provider);
1077 FreeLibrary(mapi_ex_provider);