winmm: Fix a failing mixer test on 98 and ME.
[wine] / dlls / shlwapi / ordinal.c
1 /*
2  * SHLWAPI ordinal functions
3  *
4  * Copyright 1997 Marcus Meissner
5  *           1998 Jürgen Schmied
6  *           2001-2003 Jon Griffiths
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #define COBJMACROS
31 #define NONAMELESSUNION
32 #define NONAMELESSSTRUCT
33
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winnls.h"
37 #include "winreg.h"
38 #include "wingdi.h"
39 #include "winuser.h"
40 #include "winver.h"
41 #include "winnetwk.h"
42 #include "mmsystem.h"
43 #include "objbase.h"
44 #include "exdisp.h"
45 #include "shlobj.h"
46 #include "shlwapi.h"
47 #include "shellapi.h"
48 #include "commdlg.h"
49 #include "mshtmhst.h"
50 #include "wine/unicode.h"
51 #include "wine/debug.h"
52
53
54 WINE_DEFAULT_DEBUG_CHANNEL(shell);
55
56 /* DLL handles for late bound calls */
57 extern HINSTANCE shlwapi_hInstance;
58 extern DWORD SHLWAPI_ThreadRef_index;
59
60 HRESULT WINAPI IUnknown_QueryService(IUnknown*,REFGUID,REFIID,LPVOID*);
61 HRESULT WINAPI SHInvokeCommand(HWND,IShellFolder*,LPCITEMIDLIST,BOOL);
62 BOOL    WINAPI SHAboutInfoW(LPWSTR,DWORD);
63
64 /*
65  NOTES: Most functions exported by ordinal seem to be superfluous.
66  The reason for these functions to be there is to provide a wrapper
67  for unicode functions to provide these functions on systems without
68  unicode functions eg. win95/win98. Since we have such functions we just
69  call these. If running Wine with native DLLs, some late bound calls may
70  fail. However, it is better to implement the functions in the forward DLL
71  and recommend the builtin rather than reimplementing the calls here!
72 */
73
74 /*************************************************************************
75  * SHLWAPI_DupSharedHandle
76  *
77  * Internal implemetation of SHLWAPI_11.
78  */
79 static
80 HANDLE WINAPI SHLWAPI_DupSharedHandle(HANDLE hShared, DWORD dwDstProcId,
81                                        DWORD dwSrcProcId, DWORD dwAccess,
82                                        DWORD dwOptions)
83 {
84   HANDLE hDst, hSrc;
85   DWORD dwMyProcId = GetCurrentProcessId();
86   HANDLE hRet = NULL;
87
88   TRACE("(%p,%d,%d,%08x,%08x)\n", hShared, dwDstProcId, dwSrcProcId,
89         dwAccess, dwOptions);
90
91   /* Get dest process handle */
92   if (dwDstProcId == dwMyProcId)
93     hDst = GetCurrentProcess();
94   else
95     hDst = OpenProcess(PROCESS_DUP_HANDLE, 0, dwDstProcId);
96
97   if (hDst)
98   {
99     /* Get src process handle */
100     if (dwSrcProcId == dwMyProcId)
101       hSrc = GetCurrentProcess();
102     else
103       hSrc = OpenProcess(PROCESS_DUP_HANDLE, 0, dwSrcProcId);
104
105     if (hSrc)
106     {
107       /* Make handle available to dest process */
108       if (!DuplicateHandle(hDst, hShared, hSrc, &hRet,
109                            dwAccess, 0, dwOptions | DUPLICATE_SAME_ACCESS))
110         hRet = NULL;
111
112       if (dwSrcProcId != dwMyProcId)
113         CloseHandle(hSrc);
114     }
115
116     if (dwDstProcId != dwMyProcId)
117       CloseHandle(hDst);
118   }
119
120   TRACE("Returning handle %p\n", hRet);
121   return hRet;
122 }
123
124 /*************************************************************************
125  * @  [SHLWAPI.7]
126  *
127  * Create a block of sharable memory and initialise it with data.
128  *
129  * PARAMS
130  * lpvData  [I] Pointer to data to write
131  * dwSize   [I] Size of data
132  * dwProcId [I] ID of process owning data
133  *
134  * RETURNS
135  * Success: A shared memory handle
136  * Failure: NULL
137  *
138  * NOTES
139  * Ordinals 7-11 provide a set of calls to create shared memory between a
140  * group of processes. The shared memory is treated opaquely in that its size
141  * is not exposed to clients who map it. This is accomplished by storing
142  * the size of the map as the first DWORD of mapped data, and then offsetting
143  * the view pointer returned by this size.
144  *
145  */
146 HANDLE WINAPI SHAllocShared(LPCVOID lpvData, DWORD dwSize, DWORD dwProcId)
147 {
148   HANDLE hMap;
149   LPVOID pMapped;
150   HANDLE hRet = NULL;
151
152   TRACE("(%p,%d,%d)\n", lpvData, dwSize, dwProcId);
153
154   /* Create file mapping of the correct length */
155   hMap = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, FILE_MAP_READ, 0,
156                             dwSize + sizeof(dwSize), NULL);
157   if (!hMap)
158     return hRet;
159
160   /* Get a view in our process address space */
161   pMapped = MapViewOfFile(hMap, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
162
163   if (pMapped)
164   {
165     /* Write size of data, followed by the data, to the view */
166     *((DWORD*)pMapped) = dwSize;
167     if (lpvData)
168       memcpy((char *) pMapped + sizeof(dwSize), lpvData, dwSize);
169
170     /* Release view. All further views mapped will be opaque */
171     UnmapViewOfFile(pMapped);
172     hRet = SHLWAPI_DupSharedHandle(hMap, dwProcId,
173                                    GetCurrentProcessId(), FILE_MAP_ALL_ACCESS,
174                                    DUPLICATE_SAME_ACCESS);
175   }
176
177   CloseHandle(hMap);
178   return hRet;
179 }
180
181 /*************************************************************************
182  * @ [SHLWAPI.8]
183  *
184  * Get a pointer to a block of shared memory from a shared memory handle.
185  *
186  * PARAMS
187  * hShared  [I] Shared memory handle
188  * dwProcId [I] ID of process owning hShared
189  *
190  * RETURNS
191  * Success: A pointer to the shared memory
192  * Failure: NULL
193  *
194  */
195 PVOID WINAPI SHLockShared(HANDLE hShared, DWORD dwProcId)
196 {
197   HANDLE hDup;
198   LPVOID pMapped;
199
200   TRACE("(%p %d)\n", hShared, dwProcId);
201
202   /* Get handle to shared memory for current process */
203   hDup = SHLWAPI_DupSharedHandle(hShared, dwProcId, GetCurrentProcessId(),
204                                  FILE_MAP_ALL_ACCESS, 0);
205   /* Get View */
206   pMapped = MapViewOfFile(hDup, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
207   CloseHandle(hDup);
208
209   if (pMapped)
210     return (char *) pMapped + sizeof(DWORD); /* Hide size */
211   return NULL;
212 }
213
214 /*************************************************************************
215  * @ [SHLWAPI.9]
216  *
217  * Release a pointer to a block of shared memory.
218  *
219  * PARAMS
220  * lpView [I] Shared memory pointer
221  *
222  * RETURNS
223  * Success: TRUE
224  * Failure: FALSE
225  *
226  */
227 BOOL WINAPI SHUnlockShared(LPVOID lpView)
228 {
229   TRACE("(%p)\n", lpView);
230   return UnmapViewOfFile((char *) lpView - sizeof(DWORD)); /* Include size */
231 }
232
233 /*************************************************************************
234  * @ [SHLWAPI.10]
235  *
236  * Destroy a block of sharable memory.
237  *
238  * PARAMS
239  * hShared  [I] Shared memory handle
240  * dwProcId [I] ID of process owning hShared
241  *
242  * RETURNS
243  * Success: TRUE
244  * Failure: FALSE
245  *
246  */
247 BOOL WINAPI SHFreeShared(HANDLE hShared, DWORD dwProcId)
248 {
249   HANDLE hClose;
250
251   TRACE("(%p %d)\n", hShared, dwProcId);
252
253   /* Get a copy of the handle for our process, closing the source handle */
254   hClose = SHLWAPI_DupSharedHandle(hShared, dwProcId, GetCurrentProcessId(),
255                                    FILE_MAP_ALL_ACCESS,DUPLICATE_CLOSE_SOURCE);
256   /* Close local copy */
257   return CloseHandle(hClose);
258 }
259
260 /*************************************************************************
261  * @   [SHLWAPI.11]
262  *
263  * Copy a sharable memory handle from one process to another.
264  *
265  * PARAMS
266  * hShared     [I] Shared memory handle to duplicate
267  * dwDstProcId [I] ID of the process wanting the duplicated handle
268  * dwSrcProcId [I] ID of the process owning hShared
269  * dwAccess    [I] Desired DuplicateHandle() access
270  * dwOptions   [I] Desired DuplicateHandle() options
271  *
272  * RETURNS
273  * Success: A handle suitable for use by the dwDstProcId process.
274  * Failure: A NULL handle.
275  *
276  */
277 HANDLE WINAPI SHMapHandle(HANDLE hShared, DWORD dwDstProcId, DWORD dwSrcProcId,
278                           DWORD dwAccess, DWORD dwOptions)
279 {
280   HANDLE hRet;
281
282   hRet = SHLWAPI_DupSharedHandle(hShared, dwDstProcId, dwSrcProcId,
283                                  dwAccess, dwOptions);
284   return hRet;
285 }
286
287 /*************************************************************************
288  *      @       [SHLWAPI.13]
289  *
290  * Create and register a clipboard enumerator for a web browser.
291  *
292  * PARAMS
293  *  lpBC      [I] Binding context
294  *  lpUnknown [I] An object exposing the IWebBrowserApp interface
295  *
296  * RETURNS
297  *  Success: S_OK.
298  *  Failure: An HRESULT error code.
299  *
300  * NOTES
301  *  The enumerator is stored as a property of the web browser. If it does not
302  *  yet exist, it is created and set before being registered.
303  */
304 HRESULT WINAPI RegisterDefaultAcceptHeaders(LPBC lpBC, IUnknown *lpUnknown)
305 {
306   static const WCHAR szProperty[] = { '{','D','0','F','C','A','4','2','0',
307       '-','D','3','F','5','-','1','1','C','F', '-','B','2','1','1','-','0',
308       '0','A','A','0','0','4','A','E','8','3','7','}','\0' };
309   BSTR property;
310   IEnumFORMATETC* pIEnumFormatEtc = NULL;
311   VARIANTARG var;
312   HRESULT hRet;
313   IWebBrowserApp* pBrowser = NULL;
314
315   TRACE("(%p, %p)\n", lpBC, lpUnknown);
316
317   /* Get An IWebBrowserApp interface from  lpUnknown */
318   hRet = IUnknown_QueryService(lpUnknown, &IID_IWebBrowserApp, &IID_IWebBrowserApp, (PVOID)&pBrowser);
319   if (FAILED(hRet) || !pBrowser)
320     return E_NOINTERFACE;
321
322   V_VT(&var) = VT_EMPTY;
323
324   /* The property we get is the browsers clipboard enumerator */
325   property = SysAllocString(szProperty);
326   hRet = IWebBrowserApp_GetProperty(pBrowser, property, &var);
327   SysFreeString(property);
328   if (FAILED(hRet))
329     return hRet;
330
331   if (V_VT(&var) == VT_EMPTY)
332   {
333     /* Iterate through accepted documents and RegisterClipBoardFormatA() them */
334     char szKeyBuff[128], szValueBuff[128];
335     DWORD dwKeySize, dwValueSize, dwRet = 0, dwCount = 0, dwNumValues, dwType;
336     FORMATETC* formatList, *format;
337     HKEY hDocs;
338
339     TRACE("Registering formats and creating IEnumFORMATETC instance\n");
340
341     if (!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\Current"
342                      "Version\\Internet Settings\\Accepted Documents", &hDocs))
343       return E_FAIL;
344
345     /* Get count of values in key */
346     while (!dwRet)
347     {
348       dwKeySize = sizeof(szKeyBuff);
349       dwRet = RegEnumValueA(hDocs,dwCount,szKeyBuff,&dwKeySize,0,&dwType,0,0);
350       dwCount++;
351     }
352
353     dwNumValues = dwCount;
354
355     /* Note: dwCount = number of items + 1; The extra item is the end node */
356     format = formatList = HeapAlloc(GetProcessHeap(), 0, dwCount * sizeof(FORMATETC));
357     if (!formatList)
358       return E_OUTOFMEMORY;
359
360     if (dwNumValues > 1)
361     {
362       dwRet = 0;
363       dwCount = 0;
364
365       dwNumValues--;
366
367       /* Register clipboard formats for the values and populate format list */
368       while(!dwRet && dwCount < dwNumValues)
369       {
370         dwKeySize = sizeof(szKeyBuff);
371         dwValueSize = sizeof(szValueBuff);
372         dwRet = RegEnumValueA(hDocs, dwCount, szKeyBuff, &dwKeySize, 0, &dwType,
373                               (PBYTE)szValueBuff, &dwValueSize);
374         if (!dwRet)
375           return E_FAIL;
376
377         format->cfFormat = RegisterClipboardFormatA(szValueBuff);
378         format->ptd = NULL;
379         format->dwAspect = 1;
380         format->lindex = 4;
381         format->tymed = -1;
382
383         format++;
384         dwCount++;
385       }
386     }
387
388     /* Terminate the (maybe empty) list, last entry has a cfFormat of 0 */
389     format->cfFormat = 0;
390     format->ptd = NULL;
391     format->dwAspect = 1;
392     format->lindex = 4;
393     format->tymed = -1;
394
395     /* Create a clipboard enumerator */
396     hRet = CreateFormatEnumerator(dwNumValues, formatList, &pIEnumFormatEtc);
397
398     if (FAILED(hRet) || !pIEnumFormatEtc)
399       return hRet;
400
401     /* Set our enumerator as the browsers property */
402     V_VT(&var) = VT_UNKNOWN;
403     V_UNKNOWN(&var) = (IUnknown*)pIEnumFormatEtc;
404
405     hRet = IWebBrowserApp_PutProperty(pBrowser, (BSTR)szProperty, var);
406     if (FAILED(hRet))
407     {
408        IEnumFORMATETC_Release(pIEnumFormatEtc);
409        goto RegisterDefaultAcceptHeaders_Exit;
410     }
411   }
412
413   if (V_VT(&var) == VT_UNKNOWN)
414   {
415     /* Our variant is holding the clipboard enumerator */
416     IUnknown* pIUnknown = V_UNKNOWN(&var);
417     IEnumFORMATETC* pClone = NULL;
418
419     TRACE("Retrieved IEnumFORMATETC property\n");
420
421     /* Get an IEnumFormatEtc interface from the variants value */
422     pIEnumFormatEtc = NULL;
423     hRet = IUnknown_QueryInterface(pIUnknown, &IID_IEnumFORMATETC,
424                                    (PVOID)&pIEnumFormatEtc);
425     if (hRet == S_OK && pIEnumFormatEtc)
426     {
427       /* Clone and register the enumerator */
428       hRet = IEnumFORMATETC_Clone(pIEnumFormatEtc, &pClone);
429       if (hRet == S_OK && pClone)
430       {
431         RegisterFormatEnumerator(lpBC, pClone, 0);
432
433         IEnumFORMATETC_Release(pClone);
434       }
435
436       /* Release the IEnumFormatEtc interface */
437       IEnumFORMATETC_Release(pIUnknown);
438     }
439     IUnknown_Release(V_UNKNOWN(&var));
440   }
441
442 RegisterDefaultAcceptHeaders_Exit:
443   IWebBrowserApp_Release(pBrowser);
444   return hRet;
445 }
446
447 /*************************************************************************
448  *      @       [SHLWAPI.15]
449  *
450  * Get Explorers "AcceptLanguage" setting.
451  *
452  * PARAMS
453  *  langbuf [O] Destination for language string
454  *  buflen  [I] Length of langbuf
455  *          [0] Success: used length of langbuf
456  *
457  * RETURNS
458  *  Success: S_OK.   langbuf is set to the language string found.
459  *  Failure: E_FAIL, If any arguments are invalid, error occurred, or Explorer
460  *           does not contain the setting.
461  *           E_INVALIDARG, If the buffer is not big enough
462  */
463 HRESULT WINAPI GetAcceptLanguagesW( LPWSTR langbuf, LPDWORD buflen)
464 {
465     static const WCHAR szkeyW[] = {
466         'S','o','f','t','w','a','r','e','\\',
467         'M','i','c','r','o','s','o','f','t','\\',
468         'I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r','\\',
469         'I','n','t','e','r','n','a','t','i','o','n','a','l',0};
470     static const WCHAR valueW[] = {
471         'A','c','c','e','p','t','L','a','n','g','u','a','g','e',0};
472     static const WCHAR enusW[] = {'e','n','-','u','s',0};
473     DWORD mystrlen, mytype;
474     HKEY mykey;
475     HRESULT retval;
476     LCID mylcid;
477     WCHAR *mystr;
478
479     if(!langbuf || !buflen || !*buflen)
480         return E_FAIL;
481
482     mystrlen = (*buflen > 20) ? *buflen : 20 ;
483     mystr = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * mystrlen);
484     RegOpenKeyW(HKEY_CURRENT_USER, szkeyW, &mykey);
485     if(RegQueryValueExW(mykey, valueW, 0, &mytype, (PBYTE)mystr, &mystrlen)) {
486         /* Did not find value */
487         mylcid = GetUserDefaultLCID();
488         /* somehow the mylcid translates into "en-us"
489          *  this is similar to "LOCALE_SABBREVLANGNAME"
490          *  which could be gotten via GetLocaleInfo.
491          *  The only problem is LOCALE_SABBREVLANGUAGE" is
492          *  a 3 char string (first 2 are country code and third is
493          *  letter for "sublanguage", which does not come close to
494          *  "en-us"
495          */
496         lstrcpyW(mystr, enusW);
497         mystrlen = lstrlenW(mystr);
498     } else {
499         /* handle returned string */
500         FIXME("missing code\n");
501     }
502     memcpy( langbuf, mystr, min(*buflen,strlenW(mystr)+1)*sizeof(WCHAR) );
503
504     if(*buflen > strlenW(mystr)) {
505         *buflen = strlenW(mystr);
506         retval = S_OK;
507     } else {
508         *buflen = 0;
509         retval = E_INVALIDARG;
510         SetLastError(ERROR_INSUFFICIENT_BUFFER);
511     }
512     RegCloseKey(mykey);
513     HeapFree(GetProcessHeap(), 0, mystr);
514     return retval;
515 }
516
517 /*************************************************************************
518  *      @       [SHLWAPI.14]
519  *
520  * Ascii version of GetAcceptLanguagesW.
521  */
522 HRESULT WINAPI GetAcceptLanguagesA( LPSTR langbuf, LPDWORD buflen)
523 {
524     WCHAR *langbufW;
525     DWORD buflenW, convlen;
526     HRESULT retval;
527
528     if(!langbuf || !buflen || !*buflen) return E_FAIL;
529
530     buflenW = *buflen;
531     langbufW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * buflenW);
532     retval = GetAcceptLanguagesW(langbufW, &buflenW);
533
534     if (retval == S_OK)
535     {
536         convlen = WideCharToMultiByte(CP_ACP, 0, langbufW, -1, langbuf, *buflen, NULL, NULL);
537     }
538     else  /* copy partial string anyway */
539     {
540         convlen = WideCharToMultiByte(CP_ACP, 0, langbufW, *buflen, langbuf, *buflen, NULL, NULL);
541         if (convlen < *buflen) langbuf[convlen] = 0;
542     }
543     *buflen = buflenW ? convlen : 0;
544
545     HeapFree(GetProcessHeap(), 0, langbufW);
546     return retval;
547 }
548
549 /*************************************************************************
550  *      @       [SHLWAPI.23]
551  *
552  * Convert a GUID to a string.
553  *
554  * PARAMS
555  *  guid     [I] GUID to convert
556  *  lpszDest [O] Destination for string
557  *  cchMax   [I] Length of output buffer
558  *
559  * RETURNS
560  *  The length of the string created.
561  */
562 INT WINAPI SHStringFromGUIDA(REFGUID guid, LPSTR lpszDest, INT cchMax)
563 {
564   char xguid[40];
565   INT iLen;
566
567   TRACE("(%s,%p,%d)\n", debugstr_guid(guid), lpszDest, cchMax);
568
569   sprintf(xguid, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
570           guid->Data1, guid->Data2, guid->Data3,
571           guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
572           guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
573
574   iLen = strlen(xguid) + 1;
575
576   if (iLen > cchMax)
577     return 0;
578   memcpy(lpszDest, xguid, iLen);
579   return iLen;
580 }
581
582 /*************************************************************************
583  *      @       [SHLWAPI.24]
584  *
585  * Convert a GUID to a string.
586  *
587  * PARAMS
588  *  guid [I] GUID to convert
589  *  str  [O] Destination for string
590  *  cmax [I] Length of output buffer
591  *
592  * RETURNS
593  *  The length of the string created.
594  */
595 INT WINAPI SHStringFromGUIDW(REFGUID guid, LPWSTR lpszDest, INT cchMax)
596 {
597   WCHAR xguid[40];
598   INT iLen;
599   static const WCHAR wszFormat[] = {'{','%','0','8','l','X','-','%','0','4','X','-','%','0','4','X','-',
600       '%','0','2','X','%','0','2','X','-','%','0','2','X','%','0','2','X','%','0','2','X','%','0','2',
601       'X','%','0','2','X','%','0','2','X','}',0};
602
603   TRACE("(%s,%p,%d)\n", debugstr_guid(guid), lpszDest, cchMax);
604
605   sprintfW(xguid, wszFormat, guid->Data1, guid->Data2, guid->Data3,
606           guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
607           guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
608
609   iLen = strlenW(xguid) + 1;
610
611   if (iLen > cchMax)
612     return 0;
613   memcpy(lpszDest, xguid, iLen*sizeof(WCHAR));
614   return iLen;
615 }
616
617 /*************************************************************************
618  *      @       [SHLWAPI.29]
619  *
620  * Determine if a Unicode character is a space.
621  *
622  * PARAMS
623  *  wc [I] Character to check.
624  *
625  * RETURNS
626  *  TRUE, if wc is a space,
627  *  FALSE otherwise.
628  */
629 BOOL WINAPI IsCharSpaceW(WCHAR wc)
630 {
631     WORD CharType;
632
633     return GetStringTypeW(CT_CTYPE1, &wc, 1, &CharType) && (CharType & C1_SPACE);
634 }
635
636 /*************************************************************************
637  *      @       [SHLWAPI.30]
638  *
639  * Determine if a Unicode character is a blank.
640  *
641  * PARAMS
642  *  wc [I] Character to check.
643  *
644  * RETURNS
645  *  TRUE, if wc is a blank,
646  *  FALSE otherwise.
647  *
648  */
649 BOOL WINAPI IsCharBlankW(WCHAR wc)
650 {
651     WORD CharType;
652
653     return GetStringTypeW(CT_CTYPE1, &wc, 1, &CharType) && (CharType & C1_BLANK);
654 }
655
656 /*************************************************************************
657  *      @       [SHLWAPI.31]
658  *
659  * Determine if a Unicode character is punctuation.
660  *
661  * PARAMS
662  *  wc [I] Character to check.
663  *
664  * RETURNS
665  *  TRUE, if wc is punctuation,
666  *  FALSE otherwise.
667  */
668 BOOL WINAPI IsCharPunctW(WCHAR wc)
669 {
670     WORD CharType;
671
672     return GetStringTypeW(CT_CTYPE1, &wc, 1, &CharType) && (CharType & C1_PUNCT);
673 }
674
675 /*************************************************************************
676  *      @       [SHLWAPI.32]
677  *
678  * Determine if a Unicode character is a control character.
679  *
680  * PARAMS
681  *  wc [I] Character to check.
682  *
683  * RETURNS
684  *  TRUE, if wc is a control character,
685  *  FALSE otherwise.
686  */
687 BOOL WINAPI IsCharCntrlW(WCHAR wc)
688 {
689     WORD CharType;
690
691     return GetStringTypeW(CT_CTYPE1, &wc, 1, &CharType) && (CharType & C1_CNTRL);
692 }
693
694 /*************************************************************************
695  *      @       [SHLWAPI.33]
696  *
697  * Determine if a Unicode character is a digit.
698  *
699  * PARAMS
700  *  wc [I] Character to check.
701  *
702  * RETURNS
703  *  TRUE, if wc is a digit,
704  *  FALSE otherwise.
705  */
706 BOOL WINAPI IsCharDigitW(WCHAR wc)
707 {
708     WORD CharType;
709
710     return GetStringTypeW(CT_CTYPE1, &wc, 1, &CharType) && (CharType & C1_DIGIT);
711 }
712
713 /*************************************************************************
714  *      @       [SHLWAPI.34]
715  *
716  * Determine if a Unicode character is a hex digit.
717  *
718  * PARAMS
719  *  wc [I] Character to check.
720  *
721  * RETURNS
722  *  TRUE, if wc is a hex digit,
723  *  FALSE otherwise.
724  */
725 BOOL WINAPI IsCharXDigitW(WCHAR wc)
726 {
727     WORD CharType;
728
729     return GetStringTypeW(CT_CTYPE1, &wc, 1, &CharType) && (CharType & C1_XDIGIT);
730 }
731
732 /*************************************************************************
733  *      @       [SHLWAPI.35]
734  *
735  */
736 BOOL WINAPI GetStringType3ExW(LPWSTR src, INT count, LPWORD type)
737 {
738     return GetStringTypeW(CT_CTYPE3, src, count, type);
739 }
740
741 /*************************************************************************
742  *      @       [SHLWAPI.151]
743  *
744  * Compare two Ascii strings up to a given length.
745  *
746  * PARAMS
747  *  lpszSrc [I] Source string
748  *  lpszCmp [I] String to compare to lpszSrc
749  *  len     [I] Maximum length
750  *
751  * RETURNS
752  *  A number greater than, less than or equal to 0 depending on whether
753  *  lpszSrc is greater than, less than or equal to lpszCmp.
754  */
755 DWORD WINAPI StrCmpNCA(LPCSTR lpszSrc, LPCSTR lpszCmp, INT len)
756 {
757     return StrCmpNA(lpszSrc, lpszCmp, len);
758 }
759
760 /*************************************************************************
761  *      @       [SHLWAPI.152]
762  *
763  * Unicode version of StrCmpNCA.
764  */
765 DWORD WINAPI StrCmpNCW(LPCWSTR lpszSrc, LPCWSTR lpszCmp, INT len)
766 {
767     return StrCmpNW(lpszSrc, lpszCmp, len);
768 }
769
770 /*************************************************************************
771  *      @       [SHLWAPI.153]
772  *
773  * Compare two Ascii strings up to a given length, ignoring case.
774  *
775  * PARAMS
776  *  lpszSrc [I] Source string
777  *  lpszCmp [I] String to compare to lpszSrc
778  *  len     [I] Maximum length
779  *
780  * RETURNS
781  *  A number greater than, less than or equal to 0 depending on whether
782  *  lpszSrc is greater than, less than or equal to lpszCmp.
783  */
784 DWORD WINAPI StrCmpNICA(LPCSTR lpszSrc, LPCSTR lpszCmp, DWORD len)
785 {
786     return StrCmpNIA(lpszSrc, lpszCmp, len);
787 }
788
789 /*************************************************************************
790  *      @       [SHLWAPI.154]
791  *
792  * Unicode version of StrCmpNICA.
793  */
794 DWORD WINAPI StrCmpNICW(LPCWSTR lpszSrc, LPCWSTR lpszCmp, DWORD len)
795 {
796     return StrCmpNIW(lpszSrc, lpszCmp, len);
797 }
798
799 /*************************************************************************
800  *      @       [SHLWAPI.155]
801  *
802  * Compare two Ascii strings.
803  *
804  * PARAMS
805  *  lpszSrc [I] Source string
806  *  lpszCmp [I] String to compare to lpszSrc
807  *
808  * RETURNS
809  *  A number greater than, less than or equal to 0 depending on whether
810  *  lpszSrc is greater than, less than or equal to lpszCmp.
811  */
812 DWORD WINAPI StrCmpCA(LPCSTR lpszSrc, LPCSTR lpszCmp)
813 {
814     return lstrcmpA(lpszSrc, lpszCmp);
815 }
816
817 /*************************************************************************
818  *      @       [SHLWAPI.156]
819  *
820  * Unicode version of StrCmpCA.
821  */
822 DWORD WINAPI StrCmpCW(LPCWSTR lpszSrc, LPCWSTR lpszCmp)
823 {
824     return lstrcmpW(lpszSrc, lpszCmp);
825 }
826
827 /*************************************************************************
828  *      @       [SHLWAPI.157]
829  *
830  * Compare two Ascii strings, ignoring case.
831  *
832  * PARAMS
833  *  lpszSrc [I] Source string
834  *  lpszCmp [I] String to compare to lpszSrc
835  *
836  * RETURNS
837  *  A number greater than, less than or equal to 0 depending on whether
838  *  lpszSrc is greater than, less than or equal to lpszCmp.
839  */
840 DWORD WINAPI StrCmpICA(LPCSTR lpszSrc, LPCSTR lpszCmp)
841 {
842     return lstrcmpiA(lpszSrc, lpszCmp);
843 }
844
845 /*************************************************************************
846  *      @       [SHLWAPI.158]
847  *
848  * Unicode version of StrCmpICA.
849  */
850 DWORD WINAPI StrCmpICW(LPCWSTR lpszSrc, LPCWSTR lpszCmp)
851 {
852     return lstrcmpiW(lpszSrc, lpszCmp);
853 }
854
855 /*************************************************************************
856  *      @       [SHLWAPI.160]
857  *
858  * Get an identification string for the OS and explorer.
859  *
860  * PARAMS
861  *  lpszDest  [O] Destination for Id string
862  *  dwDestLen [I] Length of lpszDest
863  *
864  * RETURNS
865  *  TRUE,  If the string was created successfully
866  *  FALSE, Otherwise
867  */
868 BOOL WINAPI SHAboutInfoA(LPSTR lpszDest, DWORD dwDestLen)
869 {
870   WCHAR buff[2084];
871
872   TRACE("(%p,%d)\n", lpszDest, dwDestLen);
873
874   if (lpszDest && SHAboutInfoW(buff, dwDestLen))
875   {
876     WideCharToMultiByte(CP_ACP, 0, buff, -1, lpszDest, dwDestLen, NULL, NULL);
877     return TRUE;
878   }
879   return FALSE;
880 }
881
882 /*************************************************************************
883  *      @       [SHLWAPI.161]
884  *
885  * Unicode version of SHAboutInfoA.
886  */
887 BOOL WINAPI SHAboutInfoW(LPWSTR lpszDest, DWORD dwDestLen)
888 {
889   static const WCHAR szIEKey[] = { 'S','O','F','T','W','A','R','E','\\',
890     'M','i','c','r','o','s','o','f','t','\\','I','n','t','e','r','n','e','t',
891     ' ','E','x','p','l','o','r','e','r','\0' };
892   static const WCHAR szWinNtKey[] = { 'S','O','F','T','W','A','R','E','\\',
893     'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s',' ',
894     'N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\0' };
895   static const WCHAR szWinKey[] = { 'S','O','F','T','W','A','R','E','\\',
896     'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
897     'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\0' };
898   static const WCHAR szRegKey[] = { 'S','O','F','T','W','A','R','E','\\',
899     'M','i','c','r','o','s','o','f','t','\\','I','n','t','e','r','n','e','t',
900     ' ','E','x','p','l','o','r','e','r','\\',
901     'R','e','g','i','s','t','r','a','t','i','o','n','\0' };
902   static const WCHAR szVersion[] = { 'V','e','r','s','i','o','n','\0' };
903   static const WCHAR szCustomized[] = { 'C','u','s','t','o','m','i','z','e','d',
904     'V','e','r','s','i','o','n','\0' };
905   static const WCHAR szOwner[] = { 'R','e','g','i','s','t','e','r','e','d',
906     'O','w','n','e','r','\0' };
907   static const WCHAR szOrg[] = { 'R','e','g','i','s','t','e','r','e','d',
908     'O','r','g','a','n','i','z','a','t','i','o','n','\0' };
909   static const WCHAR szProduct[] = { 'P','r','o','d','u','c','t','I','d','\0' };
910   static const WCHAR szUpdate[] = { 'I','E','A','K',
911     'U','p','d','a','t','e','U','r','l','\0' };
912   static const WCHAR szHelp[] = { 'I','E','A','K',
913     'H','e','l','p','S','t','r','i','n','g','\0' };
914   WCHAR buff[2084];
915   HKEY hReg;
916   DWORD dwType, dwLen;
917
918   TRACE("(%p,%d)\n", lpszDest, dwDestLen);
919
920   if (!lpszDest)
921     return FALSE;
922
923   *lpszDest = '\0';
924
925   /* Try the NT key first, followed by 95/98 key */
926   if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szWinNtKey, 0, KEY_READ, &hReg) &&
927       RegOpenKeyExW(HKEY_LOCAL_MACHINE, szWinKey, 0, KEY_READ, &hReg))
928     return FALSE;
929
930   /* OS Version */
931   buff[0] = '\0';
932   dwLen = 30;
933   if (!SHGetValueW(HKEY_LOCAL_MACHINE, szIEKey, szVersion, &dwType, buff, &dwLen))
934   {
935     DWORD dwStrLen = strlenW(buff);
936     dwLen = 30 - dwStrLen;
937     SHGetValueW(HKEY_LOCAL_MACHINE, szIEKey,
938                 szCustomized, &dwType, buff+dwStrLen, &dwLen);
939   }
940   StrCatBuffW(lpszDest, buff, dwDestLen);
941
942   /* ~Registered Owner */
943   buff[0] = '~';
944   dwLen = 256;
945   if (SHGetValueW(hReg, szOwner, 0, &dwType, buff+1, &dwLen))
946     buff[1] = '\0';
947   StrCatBuffW(lpszDest, buff, dwDestLen);
948
949   /* ~Registered Organization */
950   dwLen = 256;
951   if (SHGetValueW(hReg, szOrg, 0, &dwType, buff+1, &dwLen))
952     buff[1] = '\0';
953   StrCatBuffW(lpszDest, buff, dwDestLen);
954
955   /* FIXME: Not sure where this number comes from  */
956   buff[0] = '~';
957   buff[1] = '0';
958   buff[2] = '\0';
959   StrCatBuffW(lpszDest, buff, dwDestLen);
960
961   /* ~Product Id */
962   dwLen = 256;
963   if (SHGetValueW(HKEY_LOCAL_MACHINE, szRegKey, szProduct, &dwType, buff+1, &dwLen))
964     buff[1] = '\0';
965   StrCatBuffW(lpszDest, buff, dwDestLen);
966
967   /* ~IE Update Url */
968   dwLen = 2048;
969   if(SHGetValueW(HKEY_LOCAL_MACHINE, szWinKey, szUpdate, &dwType, buff+1, &dwLen))
970     buff[1] = '\0';
971   StrCatBuffW(lpszDest, buff, dwDestLen);
972
973   /* ~IE Help String */
974   dwLen = 256;
975   if(SHGetValueW(hReg, szHelp, 0, &dwType, buff+1, &dwLen))
976     buff[1] = '\0';
977   StrCatBuffW(lpszDest, buff, dwDestLen);
978
979   RegCloseKey(hReg);
980   return TRUE;
981 }
982
983 /*************************************************************************
984  *      @       [SHLWAPI.163]
985  *
986  * Call IOleCommandTarget_QueryStatus() on an object.
987  *
988  * PARAMS
989  *  lpUnknown     [I] Object supporting the IOleCommandTarget interface
990  *  pguidCmdGroup [I] GUID for the command group
991  *  cCmds         [I]
992  *  prgCmds       [O] Commands
993  *  pCmdText      [O] Command text
994  *
995  * RETURNS
996  *  Success: S_OK.
997  *  Failure: E_FAIL, if lpUnknown is NULL.
998  *           E_NOINTERFACE, if lpUnknown does not support IOleCommandTarget.
999  *           Otherwise, an error code from IOleCommandTarget_QueryStatus().
1000  */
1001 HRESULT WINAPI IUnknown_QueryStatus(IUnknown* lpUnknown, REFGUID pguidCmdGroup,
1002                            ULONG cCmds, OLECMD *prgCmds, OLECMDTEXT* pCmdText)
1003 {
1004   HRESULT hRet = E_FAIL;
1005
1006   TRACE("(%p,%p,%d,%p,%p)\n",lpUnknown, pguidCmdGroup, cCmds, prgCmds, pCmdText);
1007
1008   if (lpUnknown)
1009   {
1010     IOleCommandTarget* lpOle;
1011
1012     hRet = IUnknown_QueryInterface(lpUnknown, &IID_IOleCommandTarget,
1013                                    (void**)&lpOle);
1014
1015     if (SUCCEEDED(hRet) && lpOle)
1016     {
1017       hRet = IOleCommandTarget_QueryStatus(lpOle, pguidCmdGroup, cCmds,
1018                                            prgCmds, pCmdText);
1019       IOleCommandTarget_Release(lpOle);
1020     }
1021   }
1022   return hRet;
1023 }
1024
1025 /*************************************************************************
1026  *      @               [SHLWAPI.164]
1027  *
1028  * Call IOleCommandTarget_Exec() on an object.
1029  *
1030  * PARAMS
1031  *  lpUnknown     [I] Object supporting the IOleCommandTarget interface
1032  *  pguidCmdGroup [I] GUID for the command group
1033  *
1034  * RETURNS
1035  *  Success: S_OK.
1036  *  Failure: E_FAIL, if lpUnknown is NULL.
1037  *           E_NOINTERFACE, if lpUnknown does not support IOleCommandTarget.
1038  *           Otherwise, an error code from IOleCommandTarget_Exec().
1039  */
1040 HRESULT WINAPI IUnknown_Exec(IUnknown* lpUnknown, REFGUID pguidCmdGroup,
1041                            DWORD nCmdID, DWORD nCmdexecopt, VARIANT* pvaIn,
1042                            VARIANT* pvaOut)
1043 {
1044   HRESULT hRet = E_FAIL;
1045
1046   TRACE("(%p,%p,%d,%d,%p,%p)\n",lpUnknown, pguidCmdGroup, nCmdID,
1047         nCmdexecopt, pvaIn, pvaOut);
1048
1049   if (lpUnknown)
1050   {
1051     IOleCommandTarget* lpOle;
1052
1053     hRet = IUnknown_QueryInterface(lpUnknown, &IID_IOleCommandTarget,
1054                                    (void**)&lpOle);
1055     if (SUCCEEDED(hRet) && lpOle)
1056     {
1057       hRet = IOleCommandTarget_Exec(lpOle, pguidCmdGroup, nCmdID,
1058                                     nCmdexecopt, pvaIn, pvaOut);
1059       IOleCommandTarget_Release(lpOle);
1060     }
1061   }
1062   return hRet;
1063 }
1064
1065 /*************************************************************************
1066  *      @       [SHLWAPI.165]
1067  *
1068  * Retrieve, modify, and re-set a value from a window.
1069  *
1070  * PARAMS
1071  *  hWnd   [I] Window to get value from
1072  *  offset [I] Offset of value
1073  *  wMask  [I] Mask for uiFlags
1074  *  wFlags [I] Bits to set in window value
1075  *
1076  * RETURNS
1077  *  The new value as it was set, or 0 if any parameter is invalid.
1078  *
1079  * NOTES
1080  *  Any bits set in uiMask are cleared from the value, then any bits set in
1081  *  uiFlags are set in the value.
1082  */
1083 LONG WINAPI SHSetWindowBits(HWND hwnd, INT offset, UINT wMask, UINT wFlags)
1084 {
1085   LONG ret = GetWindowLongA(hwnd, offset);
1086   LONG newFlags = (wFlags & wMask) | (ret & ~wFlags);
1087
1088   if (newFlags != ret)
1089     ret = SetWindowLongA(hwnd, offset, newFlags);
1090   return ret;
1091 }
1092
1093 /*************************************************************************
1094  *      @       [SHLWAPI.167]
1095  *
1096  * Change a window's parent.
1097  *
1098  * PARAMS
1099  *  hWnd       [I] Window to change parent of
1100  *  hWndParent [I] New parent window
1101  *
1102  * RETURNS
1103  *  The old parent of hWnd.
1104  *
1105  * NOTES
1106  *  If hWndParent is NULL (desktop), the window style is changed to WS_POPUP.
1107  *  If hWndParent is NOT NULL then we set the WS_CHILD style.
1108  */
1109 HWND WINAPI SHSetParentHwnd(HWND hWnd, HWND hWndParent)
1110 {
1111   TRACE("%p, %p\n", hWnd, hWndParent);
1112
1113   if(GetParent(hWnd) == hWndParent)
1114     return 0;
1115
1116   if(hWndParent)
1117     SHSetWindowBits(hWnd, GWL_STYLE, WS_CHILD, WS_CHILD);
1118   else
1119     SHSetWindowBits(hWnd, GWL_STYLE, WS_POPUP, WS_POPUP);
1120
1121   return SetParent(hWnd, hWndParent);
1122 }
1123
1124 /*************************************************************************
1125  *      @       [SHLWAPI.168]
1126  *
1127  * Locate and advise a connection point in an IConnectionPointContainer object.
1128  *
1129  * PARAMS
1130  *  lpUnkSink   [I] Sink for the connection point advise call
1131  *  riid        [I] REFIID of connection point to advise
1132  *  bAdviseOnly [I] TRUE = Advise only, FALSE = Unadvise first
1133  *  lpUnknown   [I] Object supporting the IConnectionPointContainer interface
1134  *  lpCookie    [O] Pointer to connection point cookie
1135  *  lppCP       [O] Destination for the IConnectionPoint found
1136  *
1137  * RETURNS
1138  *  Success: S_OK. If lppCP is non-NULL, it is filled with the IConnectionPoint
1139  *           that was advised. The caller is responsible for releasing it.
1140  *  Failure: E_FAIL, if any arguments are invalid.
1141  *           E_NOINTERFACE, if lpUnknown isn't an IConnectionPointContainer,
1142  *           Or an HRESULT error code if any call fails.
1143  */
1144 HRESULT WINAPI ConnectToConnectionPoint(IUnknown* lpUnkSink, REFIID riid, BOOL bAdviseOnly,
1145                            IUnknown* lpUnknown, LPDWORD lpCookie,
1146                            IConnectionPoint **lppCP)
1147 {
1148   HRESULT hRet;
1149   IConnectionPointContainer* lpContainer;
1150   IConnectionPoint *lpCP;
1151
1152   if(!lpUnknown || (bAdviseOnly && !lpUnkSink))
1153     return E_FAIL;
1154
1155   if(lppCP)
1156     *lppCP = NULL;
1157
1158   hRet = IUnknown_QueryInterface(lpUnknown, &IID_IConnectionPointContainer,
1159                                  (void**)&lpContainer);
1160   if (SUCCEEDED(hRet))
1161   {
1162     hRet = IConnectionPointContainer_FindConnectionPoint(lpContainer, riid, &lpCP);
1163
1164     if (SUCCEEDED(hRet))
1165     {
1166       if(!bAdviseOnly)
1167         hRet = IConnectionPoint_Unadvise(lpCP, *lpCookie);
1168       hRet = IConnectionPoint_Advise(lpCP, lpUnkSink, lpCookie);
1169
1170       if (FAILED(hRet))
1171         *lpCookie = 0;
1172
1173       if (lppCP && SUCCEEDED(hRet))
1174         *lppCP = lpCP; /* Caller keeps the interface */
1175       else
1176         IConnectionPoint_Release(lpCP); /* Release it */
1177     }
1178
1179     IUnknown_Release(lpContainer);
1180   }
1181   return hRet;
1182 }
1183
1184 /*************************************************************************
1185  *      @       [SHLWAPI.169]
1186  *
1187  * Release an interface.
1188  *
1189  * PARAMS
1190  *  lpUnknown [I] Object to release
1191  *
1192  * RETURNS
1193  *  Nothing.
1194  */
1195 DWORD WINAPI IUnknown_AtomicRelease(IUnknown ** lpUnknown)
1196 {
1197     IUnknown *temp;
1198
1199     TRACE("(%p)\n",lpUnknown);
1200
1201     if(!lpUnknown || !*((LPDWORD)lpUnknown)) return 0;
1202     temp = *lpUnknown;
1203     *lpUnknown = NULL;
1204
1205     TRACE("doing Release\n");
1206
1207     return IUnknown_Release(temp);
1208 }
1209
1210 /*************************************************************************
1211  *      @       [SHLWAPI.170]
1212  *
1213  * Skip '//' if present in a string.
1214  *
1215  * PARAMS
1216  *  lpszSrc [I] String to check for '//'
1217  *
1218  * RETURNS
1219  *  Success: The next character after the '//' or the string if not present
1220  *  Failure: NULL, if lpszStr is NULL.
1221  */
1222 LPCSTR WINAPI PathSkipLeadingSlashesA(LPCSTR lpszSrc)
1223 {
1224   if (lpszSrc && lpszSrc[0] == '/' && lpszSrc[1] == '/')
1225     lpszSrc += 2;
1226   return lpszSrc;
1227 }
1228
1229 /*************************************************************************
1230  *      @               [SHLWAPI.171]
1231  *
1232  * Check if two interfaces come from the same object.
1233  *
1234  * PARAMS
1235  *   lpInt1 [I] Interface to check against lpInt2.
1236  *   lpInt2 [I] Interface to check against lpInt1.
1237  *
1238  * RETURNS
1239  *   TRUE, If the interfaces come from the same object.
1240  *   FALSE Otherwise.
1241  */
1242 BOOL WINAPI SHIsSameObject(IUnknown* lpInt1, IUnknown* lpInt2)
1243 {
1244   LPVOID lpUnknown1, lpUnknown2;
1245
1246   TRACE("%p %p\n", lpInt1, lpInt2);
1247
1248   if (!lpInt1 || !lpInt2)
1249     return FALSE;
1250
1251   if (lpInt1 == lpInt2)
1252     return TRUE;
1253
1254   if (FAILED(IUnknown_QueryInterface(lpInt1, &IID_IUnknown,
1255                                        (LPVOID *)&lpUnknown1)))
1256     return FALSE;
1257
1258   if (FAILED(IUnknown_QueryInterface(lpInt2, &IID_IUnknown,
1259                                        (LPVOID *)&lpUnknown2)))
1260     return FALSE;
1261
1262   if (lpUnknown1 == lpUnknown2)
1263     return TRUE;
1264
1265   return FALSE;
1266 }
1267
1268 /*************************************************************************
1269  *      @       [SHLWAPI.172]
1270  *
1271  * Get the window handle of an object.
1272  *
1273  * PARAMS
1274  *  lpUnknown [I] Object to get the window handle of
1275  *  lphWnd    [O] Destination for window handle
1276  *
1277  * RETURNS
1278  *  Success: S_OK. lphWnd contains the objects window handle.
1279  *  Failure: An HRESULT error code.
1280  *
1281  * NOTES
1282  *  lpUnknown is expected to support one of the following interfaces:
1283  *  IOleWindow(), IInternetSecurityMgrSite(), or IShellView().
1284  */
1285 HRESULT WINAPI IUnknown_GetWindow(IUnknown *lpUnknown, HWND *lphWnd)
1286 {
1287   IUnknown *lpOle;
1288   HRESULT hRet = E_FAIL;
1289
1290   TRACE("(%p,%p)\n", lpUnknown, lphWnd);
1291
1292   if (!lpUnknown)
1293     return hRet;
1294
1295   hRet = IUnknown_QueryInterface(lpUnknown, &IID_IOleWindow, (void**)&lpOle);
1296
1297   if (FAILED(hRet))
1298   {
1299     hRet = IUnknown_QueryInterface(lpUnknown,&IID_IShellView, (void**)&lpOle);
1300
1301     if (FAILED(hRet))
1302     {
1303       hRet = IUnknown_QueryInterface(lpUnknown, &IID_IInternetSecurityMgrSite,
1304                                       (void**)&lpOle);
1305     }
1306   }
1307
1308   if (SUCCEEDED(hRet))
1309   {
1310     /* Lazyness here - Since GetWindow() is the first method for the above 3
1311      * interfaces, we use the same call for them all.
1312      */
1313     hRet = IOleWindow_GetWindow((IOleWindow*)lpOle, lphWnd);
1314     IUnknown_Release(lpOle);
1315     if (lphWnd)
1316       TRACE("Returning HWND=%p\n", *lphWnd);
1317   }
1318
1319   return hRet;
1320 }
1321
1322 /*************************************************************************
1323  *      @       [SHLWAPI.173]
1324  *
1325  * Call a method on as as yet unidentified object.
1326  *
1327  * PARAMS
1328  *  pUnk [I] Object supporting the unidentified interface,
1329  *  arg  [I] Argument for the call on the object.
1330  *
1331  * RETURNS
1332  *  S_OK.
1333  */
1334 HRESULT WINAPI IUnknown_SetOwner(IUnknown *pUnk, ULONG arg)
1335 {
1336   static const GUID guid_173 = {
1337     0x5836fb00, 0x8187, 0x11cf, { 0xa1,0x2b,0x00,0xaa,0x00,0x4a,0xe8,0x37 }
1338   };
1339   IMalloc *pUnk2;
1340
1341   TRACE("(%p,%d)\n", pUnk, arg);
1342
1343   /* Note: arg may not be a ULONG and pUnk2 is for sure not an IMalloc -
1344    *       We use this interface as its vtable entry is compatible with the
1345    *       object in question.
1346    * FIXME: Find out what this object is and where it should be defined.
1347    */
1348   if (pUnk &&
1349       SUCCEEDED(IUnknown_QueryInterface(pUnk, &guid_173, (void**)&pUnk2)))
1350   {
1351     IMalloc_Alloc(pUnk2, arg); /* Faked call!! */
1352     IMalloc_Release(pUnk2);
1353   }
1354   return S_OK;
1355 }
1356
1357 /*************************************************************************
1358  *      @       [SHLWAPI.174]
1359  *
1360  * Call either IObjectWithSite_SetSite() or IInternetSecurityManager_SetSecuritySite() on
1361  * an object.
1362  *
1363  */
1364 HRESULT WINAPI IUnknown_SetSite(
1365         IUnknown *obj,        /* [in]   OLE object     */
1366         IUnknown *site)       /* [in]   Site interface */
1367 {
1368     HRESULT hr;
1369     IObjectWithSite *iobjwithsite;
1370     IInternetSecurityManager *isecmgr;
1371
1372     if (!obj) return E_FAIL;
1373
1374     hr = IUnknown_QueryInterface(obj, &IID_IObjectWithSite, (LPVOID *)&iobjwithsite);
1375     TRACE("IID_IObjectWithSite QI ret=%08x, %p\n", hr, iobjwithsite);
1376     if (SUCCEEDED(hr))
1377     {
1378         hr = IObjectWithSite_SetSite(iobjwithsite, site);
1379         TRACE("done IObjectWithSite_SetSite ret=%08x\n", hr);
1380         IUnknown_Release(iobjwithsite);
1381     }
1382     else
1383     {
1384         hr = IUnknown_QueryInterface(obj, &IID_IInternetSecurityManager, (LPVOID *)&isecmgr);
1385         TRACE("IID_IInternetSecurityManager QI ret=%08x, %p\n", hr, isecmgr);
1386         if (FAILED(hr)) return hr;
1387
1388         hr = IInternetSecurityManager_SetSecuritySite(isecmgr, (IInternetSecurityMgrSite *)site);
1389         TRACE("done IInternetSecurityManager_SetSecuritySite ret=%08x\n", hr);
1390         IUnknown_Release(isecmgr);
1391     }
1392     return hr;
1393 }
1394
1395 /*************************************************************************
1396  *      @       [SHLWAPI.175]
1397  *
1398  * Call IPersist_GetClassID() on an object.
1399  *
1400  * PARAMS
1401  *  lpUnknown [I] Object supporting the IPersist interface
1402  *  lpClassId [O] Destination for Class Id
1403  *
1404  * RETURNS
1405  *  Success: S_OK. lpClassId contains the Class Id requested.
1406  *  Failure: E_FAIL, If lpUnknown is NULL,
1407  *           E_NOINTERFACE If lpUnknown does not support IPersist,
1408  *           Or an HRESULT error code.
1409  */
1410 HRESULT WINAPI IUnknown_GetClassID(IUnknown *lpUnknown, CLSID* lpClassId)
1411 {
1412   IPersist* lpPersist;
1413   HRESULT hRet = E_FAIL;
1414
1415   TRACE("(%p,%p)\n", lpUnknown, debugstr_guid(lpClassId));
1416
1417   if (lpUnknown)
1418   {
1419     hRet = IUnknown_QueryInterface(lpUnknown,&IID_IPersist,(void**)&lpPersist);
1420     if (SUCCEEDED(hRet))
1421     {
1422       IPersist_GetClassID(lpPersist, lpClassId);
1423       IPersist_Release(lpPersist);
1424     }
1425   }
1426   return hRet;
1427 }
1428
1429 /*************************************************************************
1430  *      @       [SHLWAPI.176]
1431  *
1432  * Retrieve a Service Interface from an object.
1433  *
1434  * PARAMS
1435  *  lpUnknown [I] Object to get an IServiceProvider interface from
1436  *  sid       [I] Service ID for IServiceProvider_QueryService() call
1437  *  riid      [I] Function requested for QueryService call
1438  *  lppOut    [O] Destination for the service interface pointer
1439  *
1440  * RETURNS
1441  *  Success: S_OK. lppOut contains an object providing the requested service
1442  *  Failure: An HRESULT error code
1443  *
1444  * NOTES
1445  *  lpUnknown is expected to support the IServiceProvider interface.
1446  */
1447 HRESULT WINAPI IUnknown_QueryService(IUnknown* lpUnknown, REFGUID sid, REFIID riid,
1448                            LPVOID *lppOut)
1449 {
1450   IServiceProvider* pService = NULL;
1451   HRESULT hRet;
1452
1453   if (!lppOut)
1454     return E_FAIL;
1455
1456   *lppOut = NULL;
1457
1458   if (!lpUnknown)
1459     return E_FAIL;
1460
1461   /* Get an IServiceProvider interface from the object */
1462   hRet = IUnknown_QueryInterface(lpUnknown, &IID_IServiceProvider,
1463                                  (LPVOID*)&pService);
1464
1465   if (hRet == S_OK && pService)
1466   {
1467     TRACE("QueryInterface returned (IServiceProvider*)%p\n", pService);
1468
1469     /* Get a Service interface from the object */
1470     hRet = IServiceProvider_QueryService(pService, sid, riid, lppOut);
1471
1472     TRACE("(IServiceProvider*)%p returned (IUnknown*)%p\n", pService, *lppOut);
1473
1474     /* Release the IServiceProvider interface */
1475     IUnknown_Release(pService);
1476   }
1477   return hRet;
1478 }
1479
1480 /*************************************************************************
1481  *      @       [SHLWAPI.177]
1482  *
1483  * Loads a popup menu.
1484  *
1485  * PARAMS
1486  *  hInst  [I] Instance handle
1487  *  szName [I] Menu name
1488  *
1489  * RETURNS
1490  *  Success: TRUE.
1491  *  Failure: FALSE.
1492  */
1493 BOOL WINAPI SHLoadMenuPopup(HINSTANCE hInst, LPCWSTR szName)
1494 {
1495   HMENU hMenu;
1496
1497   if ((hMenu = LoadMenuW(hInst, szName)))
1498   {
1499     if (GetSubMenu(hMenu, 0))
1500       RemoveMenu(hMenu, 0, MF_BYPOSITION);
1501
1502     DestroyMenu(hMenu);
1503     return TRUE;
1504   }
1505   return FALSE;
1506 }
1507
1508 typedef struct _enumWndData
1509 {
1510   UINT   uiMsgId;
1511   WPARAM wParam;
1512   LPARAM lParam;
1513   LRESULT (WINAPI *pfnPost)(HWND,UINT,WPARAM,LPARAM);
1514 } enumWndData;
1515
1516 /* Callback for SHLWAPI_178 */
1517 static BOOL CALLBACK SHLWAPI_EnumChildProc(HWND hWnd, LPARAM lParam)
1518 {
1519   enumWndData *data = (enumWndData *)lParam;
1520
1521   TRACE("(%p,%p)\n", hWnd, data);
1522   data->pfnPost(hWnd, data->uiMsgId, data->wParam, data->lParam);
1523   return TRUE;
1524 }
1525
1526 /*************************************************************************
1527  * @  [SHLWAPI.178]
1528  *
1529  * Send or post a message to every child of a window.
1530  *
1531  * PARAMS
1532  *  hWnd    [I] Window whose children will get the messages
1533  *  uiMsgId [I] Message Id
1534  *  wParam  [I] WPARAM of message
1535  *  lParam  [I] LPARAM of message
1536  *  bSend   [I] TRUE = Use SendMessageA(), FALSE = Use PostMessageA()
1537  *
1538  * RETURNS
1539  *  Nothing.
1540  *
1541  * NOTES
1542  *  The appropriate ASCII or Unicode function is called for the window.
1543  */
1544 void WINAPI SHPropagateMessage(HWND hWnd, UINT uiMsgId, WPARAM wParam, LPARAM lParam, BOOL bSend)
1545 {
1546   enumWndData data;
1547
1548   TRACE("(%p,%u,%ld,%ld,%d)\n", hWnd, uiMsgId, wParam, lParam, bSend);
1549
1550   if(hWnd)
1551   {
1552     data.uiMsgId = uiMsgId;
1553     data.wParam  = wParam;
1554     data.lParam  = lParam;
1555
1556     if (bSend)
1557       data.pfnPost = IsWindowUnicode(hWnd) ? (void*)SendMessageW : (void*)SendMessageA;
1558     else
1559       data.pfnPost = IsWindowUnicode(hWnd) ? (void*)PostMessageW : (void*)PostMessageA;
1560
1561     EnumChildWindows(hWnd, SHLWAPI_EnumChildProc, (LPARAM)&data);
1562   }
1563 }
1564
1565 /*************************************************************************
1566  *      @       [SHLWAPI.180]
1567  *
1568  * Remove all sub-menus from a menu.
1569  *
1570  * PARAMS
1571  *  hMenu [I] Menu to remove sub-menus from
1572  *
1573  * RETURNS
1574  *  Success: 0.  All sub-menus under hMenu are removed
1575  *  Failure: -1, if any parameter is invalid
1576  */
1577 DWORD WINAPI SHRemoveAllSubMenus(HMENU hMenu)
1578 {
1579   int iItemCount = GetMenuItemCount(hMenu) - 1;
1580   while (iItemCount >= 0)
1581   {
1582     HMENU hSubMenu = GetSubMenu(hMenu, iItemCount);
1583     if (hSubMenu)
1584       RemoveMenu(hMenu, iItemCount, MF_BYPOSITION);
1585     iItemCount--;
1586   }
1587   return iItemCount;
1588 }
1589
1590 /*************************************************************************
1591  *      @       [SHLWAPI.181]
1592  *
1593  * Enable or disable a menu item.
1594  *
1595  * PARAMS
1596  *  hMenu   [I] Menu holding menu item
1597  *  uID     [I] ID of menu item to enable/disable
1598  *  bEnable [I] Whether to enable (TRUE) or disable (FALSE) the item.
1599  *
1600  * RETURNS
1601  *  The return code from EnableMenuItem.
1602  */
1603 UINT WINAPI SHEnableMenuItem(HMENU hMenu, UINT wItemID, BOOL bEnable)
1604 {
1605   return EnableMenuItem(hMenu, wItemID, bEnable ? MF_ENABLED : MF_GRAYED);
1606 }
1607
1608 /*************************************************************************
1609  * @    [SHLWAPI.182]
1610  *
1611  * Check or uncheck a menu item.
1612  *
1613  * PARAMS
1614  *  hMenu  [I] Menu holding menu item
1615  *  uID    [I] ID of menu item to check/uncheck
1616  *  bCheck [I] Whether to check (TRUE) or uncheck (FALSE) the item.
1617  *
1618  * RETURNS
1619  *  The return code from CheckMenuItem.
1620  */
1621 DWORD WINAPI SHCheckMenuItem(HMENU hMenu, UINT uID, BOOL bCheck)
1622 {
1623   return CheckMenuItem(hMenu, uID, bCheck ? MF_CHECKED : MF_UNCHECKED);
1624 }
1625
1626 /*************************************************************************
1627  *      @       [SHLWAPI.183]
1628  *
1629  * Register a window class if it isn't already.
1630  *
1631  * PARAMS
1632  *  lpWndClass [I] Window class to register
1633  *
1634  * RETURNS
1635  *  The result of the RegisterClassA call.
1636  */
1637 DWORD WINAPI SHRegisterClassA(WNDCLASSA *wndclass)
1638 {
1639   WNDCLASSA wca;
1640   if (GetClassInfoA(wndclass->hInstance, wndclass->lpszClassName, &wca))
1641     return TRUE;
1642   return (DWORD)RegisterClassA(wndclass);
1643 }
1644
1645 /*************************************************************************
1646  *      @       [SHLWAPI.186]
1647  */
1648 BOOL WINAPI SHSimulateDrop(IDropTarget *pDrop, IDataObject *pDataObj,
1649                            DWORD grfKeyState, PPOINTL lpPt, DWORD* pdwEffect)
1650 {
1651   DWORD dwEffect = DROPEFFECT_LINK | DROPEFFECT_MOVE | DROPEFFECT_COPY;
1652   POINTL pt = { 0, 0 };
1653
1654   if (!lpPt)
1655     lpPt = &pt;
1656
1657   if (!pdwEffect)
1658     pdwEffect = &dwEffect;
1659
1660   IDropTarget_DragEnter(pDrop, pDataObj, grfKeyState, *lpPt, pdwEffect);
1661
1662   if (*pdwEffect)
1663     return IDropTarget_Drop(pDrop, pDataObj, grfKeyState, *lpPt, pdwEffect);
1664
1665   IDropTarget_DragLeave(pDrop);
1666   return TRUE;
1667 }
1668
1669 /*************************************************************************
1670  *      @       [SHLWAPI.187]
1671  *
1672  * Call IPersistPropertyBag_Load() on an object.
1673  *
1674  * PARAMS
1675  *  lpUnknown [I] Object supporting the IPersistPropertyBag interface
1676  *  lpPropBag [O] Destination for loaded IPropertyBag
1677  *
1678  * RETURNS
1679  *  Success: S_OK.
1680  *  Failure: An HRESULT error code, or E_FAIL if lpUnknown is NULL.
1681  */
1682 DWORD WINAPI SHLoadFromPropertyBag(IUnknown *lpUnknown, IPropertyBag* lpPropBag)
1683 {
1684   IPersistPropertyBag* lpPPBag;
1685   HRESULT hRet = E_FAIL;
1686
1687   TRACE("(%p,%p)\n", lpUnknown, lpPropBag);
1688
1689   if (lpUnknown)
1690   {
1691     hRet = IUnknown_QueryInterface(lpUnknown, &IID_IPersistPropertyBag,
1692                                    (void**)&lpPPBag);
1693     if (SUCCEEDED(hRet) && lpPPBag)
1694     {
1695       hRet = IPersistPropertyBag_Load(lpPPBag, lpPropBag, NULL);
1696       IPersistPropertyBag_Release(lpPPBag);
1697     }
1698   }
1699   return hRet;
1700 }
1701
1702 /*************************************************************************
1703  * @  [SHLWAPI.188]
1704  *
1705  * Call IOleControlSite_TranslateAccelerator()  on an object.
1706  *
1707  * PARAMS
1708  *  lpUnknown   [I] Object supporting the IOleControlSite interface.
1709  *  lpMsg       [I] Key message to be processed.
1710  *  dwModifiers [I] Flags containing the state of the modifier keys.
1711  *
1712  * RETURNS
1713  *  Success: S_OK.
1714  *  Failure: An HRESULT error code, or E_INVALIDARG if lpUnknown is NULL.
1715  */
1716 HRESULT WINAPI IUnknown_TranslateAcceleratorOCS(IUnknown *lpUnknown, LPMSG lpMsg, DWORD dwModifiers)
1717 {
1718   IOleControlSite* lpCSite = NULL;
1719   HRESULT hRet = E_INVALIDARG;
1720
1721   TRACE("(%p,%p,0x%08x)\n", lpUnknown, lpMsg, dwModifiers);
1722   if (lpUnknown)
1723   {
1724     hRet = IUnknown_QueryInterface(lpUnknown, &IID_IOleControlSite,
1725                                    (void**)&lpCSite);
1726     if (SUCCEEDED(hRet) && lpCSite)
1727     {
1728       hRet = IOleControlSite_TranslateAccelerator(lpCSite, lpMsg, dwModifiers);
1729       IOleControlSite_Release(lpCSite);
1730     }
1731   }
1732   return hRet;
1733 }
1734
1735
1736 /*************************************************************************
1737  * @  [SHLWAPI.189]
1738  *
1739  * Call IOleControlSite_OnFocus() on an object.
1740  *
1741  * PARAMS
1742  *  lpUnknown [I] Object supporting the IOleControlSite interface.
1743  *  fGotFocus [I] Whether focus was gained (TRUE) or lost (FALSE).
1744  *
1745  * RETURNS
1746  *  Success: S_OK.
1747  *  Failure: An HRESULT error code, or E_FAIL if lpUnknown is NULL.
1748  */
1749 HRESULT WINAPI IUnknown_OnFocusOCS(IUnknown *lpUnknown, BOOL fGotFocus)
1750 {
1751   IOleControlSite* lpCSite = NULL;
1752   HRESULT hRet = E_FAIL;
1753
1754   TRACE("(%p,%s)\n", lpUnknown, fGotFocus ? "TRUE" : "FALSE");
1755   if (lpUnknown)
1756   {
1757     hRet = IUnknown_QueryInterface(lpUnknown, &IID_IOleControlSite,
1758                                    (void**)&lpCSite);
1759     if (SUCCEEDED(hRet) && lpCSite)
1760     {
1761       hRet = IOleControlSite_OnFocus(lpCSite, fGotFocus);
1762       IOleControlSite_Release(lpCSite);
1763     }
1764   }
1765   return hRet;
1766 }
1767
1768 /*************************************************************************
1769  * @    [SHLWAPI.190]
1770  */
1771 HRESULT WINAPI IUnknown_HandleIRestrict(LPUNKNOWN lpUnknown, PVOID lpArg1,
1772                                         PVOID lpArg2, PVOID lpArg3, PVOID lpArg4)
1773 {
1774   /* FIXME: {D12F26B2-D90A-11D0-830D-00AA005B4383} - What object does this represent? */
1775   static const DWORD service_id[] = { 0xd12f26b2, 0x11d0d90a, 0xaa000d83, 0x83435b00 };
1776   /* FIXME: {D12F26B1-D90A-11D0-830D-00AA005B4383} - Also Unknown/undocumented */
1777   static const DWORD function_id[] = { 0xd12f26b1, 0x11d0d90a, 0xaa000d83, 0x83435b00 };
1778   HRESULT hRet = E_INVALIDARG;
1779   LPUNKNOWN lpUnkInner = NULL; /* FIXME: Real type is unknown */
1780
1781   TRACE("(%p,%p,%p,%p,%p)\n", lpUnknown, lpArg1, lpArg2, lpArg3, lpArg4);
1782
1783   if (lpUnknown && lpArg4)
1784   {
1785      hRet = IUnknown_QueryService(lpUnknown, (REFGUID)service_id,
1786                                   (REFGUID)function_id, (void**)&lpUnkInner);
1787
1788      if (SUCCEEDED(hRet) && lpUnkInner)
1789      {
1790        /* FIXME: The type of service object requested is unknown, however
1791         * testing shows that its first method is called with 4 parameters.
1792         * Fake this by using IParseDisplayName_ParseDisplayName since the
1793         * signature and position in the vtable matches our unknown object type.
1794         */
1795        hRet = IParseDisplayName_ParseDisplayName((LPPARSEDISPLAYNAME)lpUnkInner,
1796                                                  lpArg1, lpArg2, lpArg3, lpArg4);
1797        IUnknown_Release(lpUnkInner);
1798      }
1799   }
1800   return hRet;
1801 }
1802
1803 /*************************************************************************
1804  * @    [SHLWAPI.192]
1805  *
1806  * Get a sub-menu from a menu item.
1807  *
1808  * PARAMS
1809  *  hMenu [I] Menu to get sub-menu from
1810  *  uID   [I] ID of menu item containing sub-menu
1811  *
1812  * RETURNS
1813  *  The sub-menu of the item, or a NULL handle if any parameters are invalid.
1814  */
1815 HMENU WINAPI SHGetMenuFromID(HMENU hMenu, UINT uID)
1816 {
1817   MENUITEMINFOW mi;
1818
1819   TRACE("(%p,%u)\n", hMenu, uID);
1820
1821   mi.cbSize = sizeof(mi);
1822   mi.fMask = MIIM_SUBMENU;
1823
1824   if (!GetMenuItemInfoW(hMenu, uID, FALSE, &mi))
1825     return NULL;
1826
1827   return mi.hSubMenu;
1828 }
1829
1830 /*************************************************************************
1831  *      @       [SHLWAPI.193]
1832  *
1833  * Get the color depth of the primary display.
1834  *
1835  * PARAMS
1836  *  None.
1837  *
1838  * RETURNS
1839  *  The color depth of the primary display.
1840  */
1841 DWORD WINAPI SHGetCurColorRes(void)
1842 {
1843     HDC hdc;
1844     DWORD ret;
1845
1846     TRACE("()\n");
1847
1848     hdc = GetDC(0);
1849     ret = GetDeviceCaps(hdc, BITSPIXEL) * GetDeviceCaps(hdc, PLANES);
1850     ReleaseDC(0, hdc);
1851     return ret;
1852 }
1853
1854 /*************************************************************************
1855  *      @       [SHLWAPI.194]
1856  *
1857  * Wait for a message to arrive, with a timeout.
1858  *
1859  * PARAMS
1860  *  hand      [I] Handle to query
1861  *  dwTimeout [I] Timeout in ticks or INFINITE to never timeout
1862  *
1863  * RETURNS
1864  *  STATUS_TIMEOUT if no message is received before dwTimeout ticks passes.
1865  *  Otherwise returns the value from MsgWaitForMultipleObjectsEx when a
1866  *  message is available.
1867  */
1868 DWORD WINAPI SHWaitForSendMessageThread(HANDLE hand, DWORD dwTimeout)
1869 {
1870   DWORD dwEndTicks = GetTickCount() + dwTimeout;
1871   DWORD dwRet;
1872
1873   while ((dwRet = MsgWaitForMultipleObjectsEx(1, &hand, dwTimeout, QS_SENDMESSAGE, 0)) == 1)
1874   {
1875     MSG msg;
1876
1877     PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE);
1878
1879     if (dwTimeout != INFINITE)
1880     {
1881         if ((int)(dwTimeout = dwEndTicks - GetTickCount()) <= 0)
1882             return WAIT_TIMEOUT;
1883     }
1884   }
1885
1886   return dwRet;
1887 }
1888
1889 /*************************************************************************
1890  *      @       [SHLWAPI.195]
1891  *
1892  * Determine if a shell folder can be expanded.
1893  *
1894  * PARAMS
1895  *  lpFolder [I] Parent folder containing the object to test.
1896  *  pidl     [I] Id of the object to test.
1897  *
1898  * RETURNS
1899  *  Success: S_OK, if the object is expandable, S_FALSE otherwise.
1900  *  Failure: E_INVALIDARG, if any argument is invalid.
1901  *
1902  * NOTES
1903  *  If the object to be tested does not expose the IQueryInfo() interface it
1904  *  will not be identified as an expandable folder.
1905  */
1906 HRESULT WINAPI SHIsExpandableFolder(LPSHELLFOLDER lpFolder, LPCITEMIDLIST pidl)
1907 {
1908   HRESULT hRet = E_INVALIDARG;
1909   IQueryInfo *lpInfo;
1910
1911   if (lpFolder && pidl)
1912   {
1913     hRet = IShellFolder_GetUIObjectOf(lpFolder, NULL, 1, &pidl, &IID_IQueryInfo,
1914                                       NULL, (void**)&lpInfo);
1915     if (FAILED(hRet))
1916       hRet = S_FALSE; /* Doesn't expose IQueryInfo */
1917     else
1918     {
1919       DWORD dwFlags = 0;
1920
1921       /* MSDN states of IQueryInfo_GetInfoFlags() that "This method is not
1922        * currently used". Really? You wouldn't be holding out on me would you?
1923        */
1924       hRet = IQueryInfo_GetInfoFlags(lpInfo, &dwFlags);
1925
1926       if (SUCCEEDED(hRet))
1927       {
1928         /* 0x2 is an undocumented flag apparently indicating expandability */
1929         hRet = dwFlags & 0x2 ? S_OK : S_FALSE;
1930       }
1931
1932       IQueryInfo_Release(lpInfo);
1933     }
1934   }
1935   return hRet;
1936 }
1937
1938 /*************************************************************************
1939  *      @       [SHLWAPI.197]
1940  *
1941  * Blank out a region of text by drawing the background only.
1942  *
1943  * PARAMS
1944  *  hDC   [I] Device context to draw in
1945  *  pRect [I] Area to draw in
1946  *  cRef  [I] Color to draw in
1947  *
1948  * RETURNS
1949  *  Nothing.
1950  */
1951 DWORD WINAPI SHFillRectClr(HDC hDC, LPCRECT pRect, COLORREF cRef)
1952 {
1953     COLORREF cOldColor = SetBkColor(hDC, cRef);
1954     ExtTextOutA(hDC, 0, 0, ETO_OPAQUE, pRect, 0, 0, 0);
1955     SetBkColor(hDC, cOldColor);
1956     return 0;
1957 }
1958
1959 /*************************************************************************
1960  *      @       [SHLWAPI.198]
1961  *
1962  * Return the value associated with a key in a map.
1963  *
1964  * PARAMS
1965  *  lpKeys   [I] A list of keys of length iLen
1966  *  lpValues [I] A list of values associated with lpKeys, of length iLen
1967  *  iLen     [I] Length of both lpKeys and lpValues
1968  *  iKey     [I] The key value to look up in lpKeys
1969  *
1970  * RETURNS
1971  *  The value in lpValues associated with iKey, or -1 if iKey is not
1972  *  found in lpKeys.
1973  *
1974  * NOTES
1975  *  - If two elements in the map share the same key, this function returns
1976  *    the value closest to the start of the map
1977  *  - The native version of this function crashes if lpKeys or lpValues is NULL.
1978  */
1979 int WINAPI SHSearchMapInt(const int *lpKeys, const int *lpValues, int iLen, int iKey)
1980 {
1981   if (lpKeys && lpValues)
1982   {
1983     int i = 0;
1984
1985     while (i < iLen)
1986     {
1987       if (lpKeys[i] == iKey)
1988         return lpValues[i]; /* Found */
1989       i++;
1990     }
1991   }
1992   return -1; /* Not found */
1993 }
1994
1995
1996 /*************************************************************************
1997  *      @       [SHLWAPI.199]
1998  *
1999  * Copy an interface pointer
2000  *
2001  * PARAMS
2002  *   lppDest   [O] Destination for copy
2003  *   lpUnknown [I] Source for copy
2004  *
2005  * RETURNS
2006  *  Nothing.
2007  */
2008 VOID WINAPI IUnknown_Set(IUnknown **lppDest, IUnknown *lpUnknown)
2009 {
2010   TRACE("(%p,%p)\n", lppDest, lpUnknown);
2011
2012   if (lppDest)
2013     IUnknown_AtomicRelease(lppDest); /* Release existing interface */
2014
2015   if (lpUnknown)
2016   {
2017     /* Copy */
2018     IUnknown_AddRef(lpUnknown);
2019     *lppDest = lpUnknown;
2020   }
2021 }
2022
2023 /*************************************************************************
2024  *      @       [SHLWAPI.200]
2025  *
2026  */
2027 HRESULT WINAPI MayQSForward(IUnknown* lpUnknown, PVOID lpReserved,
2028                             REFGUID riidCmdGrp, ULONG cCmds,
2029                             OLECMD *prgCmds, OLECMDTEXT* pCmdText)
2030 {
2031   FIXME("(%p,%p,%p,%d,%p,%p) - stub\n",
2032         lpUnknown, lpReserved, riidCmdGrp, cCmds, prgCmds, pCmdText);
2033
2034   /* FIXME: Calls IsQSForward & IUnknown_QueryStatus */
2035   return DRAGDROP_E_NOTREGISTERED;
2036 }
2037
2038 /*************************************************************************
2039  *      @       [SHLWAPI.201]
2040  *
2041  */
2042 HRESULT WINAPI MayExecForward(IUnknown* lpUnknown, INT iUnk, REFGUID pguidCmdGroup,
2043                            DWORD nCmdID, DWORD nCmdexecopt, VARIANT* pvaIn,
2044                            VARIANT* pvaOut)
2045 {
2046   FIXME("(%p,%d,%p,%d,%d,%p,%p) - stub!\n", lpUnknown, iUnk, pguidCmdGroup,
2047         nCmdID, nCmdexecopt, pvaIn, pvaOut);
2048   return DRAGDROP_E_NOTREGISTERED;
2049 }
2050
2051 /*************************************************************************
2052  *      @       [SHLWAPI.202]
2053  *
2054  */
2055 HRESULT WINAPI IsQSForward(REFGUID pguidCmdGroup,ULONG cCmds, OLECMD *prgCmds)
2056 {
2057   FIXME("(%p,%d,%p) - stub!\n", pguidCmdGroup, cCmds, prgCmds);
2058   return DRAGDROP_E_NOTREGISTERED;
2059 }
2060
2061 /*************************************************************************
2062  * @    [SHLWAPI.204]
2063  *
2064  * Determine if a window is not a child of another window.
2065  *
2066  * PARAMS
2067  * hParent [I] Suspected parent window
2068  * hChild  [I] Suspected child window
2069  *
2070  * RETURNS
2071  * TRUE:  If hChild is a child window of hParent
2072  * FALSE: If hChild is not a child window of hParent, or they are equal
2073  */
2074 BOOL WINAPI SHIsChildOrSelf(HWND hParent, HWND hChild)
2075 {
2076   TRACE("(%p,%p)\n", hParent, hChild);
2077
2078   if (!hParent || !hChild)
2079     return TRUE;
2080   else if(hParent == hChild)
2081     return FALSE;
2082   return !IsChild(hParent, hChild);
2083 }
2084
2085 /*************************************************************************
2086  *    FDSA functions.  Manage a dynamic array of fixed size memory blocks.
2087  */
2088
2089 typedef struct
2090 {
2091     DWORD num_items;       /* Number of elements inserted */
2092     void *mem;             /* Ptr to array */
2093     DWORD blocks_alloced;  /* Number of elements allocated */
2094     BYTE inc;              /* Number of elements to grow by when we need to expand */
2095     BYTE block_size;       /* Size in bytes of an element */
2096     BYTE flags;            /* Flags */
2097 } FDSA_info;
2098
2099 #define FDSA_FLAG_INTERNAL_ALLOC 0x01 /* When set we have allocated mem internally */
2100
2101 /*************************************************************************
2102  *      @       [SHLWAPI.208]
2103  *
2104  * Initialize an FDSA array.
2105  */
2106 BOOL WINAPI FDSA_Initialize(DWORD block_size, DWORD inc, FDSA_info *info, void *mem,
2107                             DWORD init_blocks)
2108 {
2109     TRACE("(0x%08x 0x%08x %p %p 0x%08x)\n", block_size, inc, info, mem, init_blocks);
2110
2111     if(inc == 0)
2112         inc = 1;
2113
2114     if(mem)
2115         memset(mem, 0, block_size * init_blocks);
2116     
2117     info->num_items = 0;
2118     info->inc = inc;
2119     info->mem = mem;
2120     info->blocks_alloced = init_blocks;
2121     info->block_size = block_size;
2122     info->flags = 0;
2123
2124     return TRUE;
2125 }
2126
2127 /*************************************************************************
2128  *      @       [SHLWAPI.209]
2129  *
2130  * Destroy an FDSA array
2131  */
2132 BOOL WINAPI FDSA_Destroy(FDSA_info *info)
2133 {
2134     TRACE("(%p)\n", info);
2135
2136     if(info->flags & FDSA_FLAG_INTERNAL_ALLOC)
2137     {
2138         HeapFree(GetProcessHeap(), 0, info->mem);
2139         return FALSE;
2140     }
2141
2142     return TRUE;
2143 }
2144
2145 /*************************************************************************
2146  *      @       [SHLWAPI.210]
2147  *
2148  * Insert element into an FDSA array
2149  */
2150 DWORD WINAPI FDSA_InsertItem(FDSA_info *info, DWORD where, const void *block)
2151 {
2152     TRACE("(%p 0x%08x %p)\n", info, where, block);
2153     if(where > info->num_items)
2154         where = info->num_items;
2155
2156     if(info->num_items >= info->blocks_alloced)
2157     {
2158         DWORD size = (info->blocks_alloced + info->inc) * info->block_size;
2159         if(info->flags & 0x1)
2160             info->mem = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, info->mem, size);
2161         else
2162         {
2163             void *old_mem = info->mem;
2164             info->mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
2165             memcpy(info->mem, old_mem, info->blocks_alloced * info->block_size);
2166         }
2167         info->blocks_alloced += info->inc;
2168         info->flags |= 0x1;
2169     }
2170
2171     if(where < info->num_items)
2172     {
2173         memmove((char*)info->mem + (where + 1) * info->block_size,
2174                 (char*)info->mem + where * info->block_size,
2175                 (info->num_items - where) * info->block_size);
2176     }
2177     memcpy((char*)info->mem + where * info->block_size, block, info->block_size);
2178
2179     info->num_items++;
2180     return where;
2181 }
2182
2183 /*************************************************************************
2184  *      @       [SHLWAPI.211]
2185  *
2186  * Delete an element from an FDSA array.
2187  */
2188 BOOL WINAPI FDSA_DeleteItem(FDSA_info *info, DWORD where)
2189 {
2190     TRACE("(%p 0x%08x)\n", info, where);
2191
2192     if(where >= info->num_items)
2193         return FALSE;
2194
2195     if(where < info->num_items - 1)
2196     {
2197         memmove((char*)info->mem + where * info->block_size,
2198                 (char*)info->mem + (where + 1) * info->block_size,
2199                 (info->num_items - where - 1) * info->block_size);
2200     }
2201     memset((char*)info->mem + (info->num_items - 1) * info->block_size,
2202            0, info->block_size);
2203     info->num_items--;
2204     return TRUE;
2205 }
2206
2207
2208 typedef struct {
2209     REFIID   refid;
2210     DWORD    indx;
2211 } IFACE_INDEX_TBL;
2212
2213 /*************************************************************************
2214  *      @       [SHLWAPI.219]
2215  *
2216  * Call IUnknown_QueryInterface() on a table of objects.
2217  *
2218  * RETURNS
2219  *  Success: S_OK.
2220  *  Failure: E_POINTER or E_NOINTERFACE.
2221  */
2222 HRESULT WINAPI QISearch(
2223         LPVOID w,           /* [in]   Table of interfaces */
2224         IFACE_INDEX_TBL *x, /* [in]   Array of REFIIDs and indexes into the table */
2225         REFIID riid,        /* [in]   REFIID to get interface for */
2226         LPVOID *ppv)          /* [out]  Destination for interface pointer */
2227 {
2228         HRESULT ret;
2229         IUnknown *a_vtbl;
2230         IFACE_INDEX_TBL *xmove;
2231
2232         TRACE("(%p %p %s %p)\n", w,x,debugstr_guid(riid),ppv);
2233         if (ppv) {
2234             xmove = x;
2235             while (xmove->refid) {
2236                 TRACE("trying (indx %d) %s\n", xmove->indx, debugstr_guid(xmove->refid));
2237                 if (IsEqualIID(riid, xmove->refid)) {
2238                     a_vtbl = (IUnknown*)(xmove->indx + (LPBYTE)w);
2239                     TRACE("matched, returning (%p)\n", a_vtbl);
2240                     *ppv = (LPVOID)a_vtbl;
2241                     IUnknown_AddRef(a_vtbl);
2242                     return S_OK;
2243                 }
2244                 xmove++;
2245             }
2246
2247             if (IsEqualIID(riid, &IID_IUnknown)) {
2248                 a_vtbl = (IUnknown*)(x->indx + (LPBYTE)w);
2249                 TRACE("returning first for IUnknown (%p)\n", a_vtbl);
2250                 *ppv = (LPVOID)a_vtbl;
2251                 IUnknown_AddRef(a_vtbl);
2252                 return S_OK;
2253             }
2254             *ppv = 0;
2255             ret = E_NOINTERFACE;
2256         } else
2257             ret = E_POINTER;
2258
2259         TRACE("-- 0x%08x\n", ret);
2260         return ret;
2261 }
2262
2263 /*************************************************************************
2264  * @ [SHLWAPI.220]
2265  *
2266  * Set the Font for a window and the "PropDlgFont" property of the parent window.
2267  *
2268  * PARAMS
2269  *  hWnd [I] Parent Window to set the property
2270  *  id   [I] Index of child Window to set the Font
2271  *
2272  * RETURNS
2273  *  Success: S_OK
2274  *
2275  */
2276 HRESULT WINAPI SHSetDefaultDialogFont(HWND hWnd, INT id)
2277 {
2278     FIXME("(%p, %d) stub\n", hWnd, id);
2279     return S_OK;
2280 }
2281
2282 /*************************************************************************
2283  *      @       [SHLWAPI.221]
2284  *
2285  * Remove the "PropDlgFont" property from a window.
2286  *
2287  * PARAMS
2288  *  hWnd [I] Window to remove the property from
2289  *
2290  * RETURNS
2291  *  A handle to the removed property, or NULL if it did not exist.
2292  */
2293 HANDLE WINAPI SHRemoveDefaultDialogFont(HWND hWnd)
2294 {
2295   HANDLE hProp;
2296
2297   TRACE("(%p)\n", hWnd);
2298
2299   hProp = GetPropA(hWnd, "PropDlgFont");
2300
2301   if(hProp)
2302   {
2303     DeleteObject(hProp);
2304     hProp = RemovePropA(hWnd, "PropDlgFont");
2305   }
2306   return hProp;
2307 }
2308
2309 /*************************************************************************
2310  *      @       [SHLWAPI.236]
2311  *
2312  * Load the in-process server of a given GUID.
2313  *
2314  * PARAMS
2315  *  refiid [I] GUID of the server to load.
2316  *
2317  * RETURNS
2318  *  Success: A handle to the loaded server dll.
2319  *  Failure: A NULL handle.
2320  */
2321 HMODULE WINAPI SHPinDllOfCLSID(REFIID refiid)
2322 {
2323     HKEY newkey;
2324     DWORD type, count;
2325     CHAR value[MAX_PATH], string[MAX_PATH];
2326
2327     strcpy(string, "CLSID\\");
2328     SHStringFromGUIDA(refiid, string + 6, sizeof(string)/sizeof(char) - 6);
2329     strcat(string, "\\InProcServer32");
2330
2331     count = MAX_PATH;
2332     RegOpenKeyExA(HKEY_CLASSES_ROOT, string, 0, 1, &newkey);
2333     RegQueryValueExA(newkey, 0, 0, &type, (PBYTE)value, &count);
2334     RegCloseKey(newkey);
2335     return LoadLibraryExA(value, 0, 0);
2336 }
2337
2338 /*************************************************************************
2339  *      @       [SHLWAPI.237]
2340  *
2341  * Unicode version of SHLWAPI_183.
2342  */
2343 DWORD WINAPI SHRegisterClassW(WNDCLASSW * lpWndClass)
2344 {
2345         WNDCLASSW WndClass;
2346
2347         TRACE("(%p %s)\n",lpWndClass->hInstance, debugstr_w(lpWndClass->lpszClassName));
2348
2349         if (GetClassInfoW(lpWndClass->hInstance, lpWndClass->lpszClassName, &WndClass))
2350                 return TRUE;
2351         return RegisterClassW(lpWndClass);
2352 }
2353
2354 /*************************************************************************
2355  *      @       [SHLWAPI.238]
2356  *
2357  * Unregister a list of classes.
2358  *
2359  * PARAMS
2360  *  hInst      [I] Application instance that registered the classes
2361  *  lppClasses [I] List of class names
2362  *  iCount     [I] Number of names in lppClasses
2363  *
2364  * RETURNS
2365  *  Nothing.
2366  */
2367 void WINAPI SHUnregisterClassesA(HINSTANCE hInst, LPCSTR *lppClasses, INT iCount)
2368 {
2369   WNDCLASSA WndClass;
2370
2371   TRACE("(%p,%p,%d)\n", hInst, lppClasses, iCount);
2372
2373   while (iCount > 0)
2374   {
2375     if (GetClassInfoA(hInst, *lppClasses, &WndClass))
2376       UnregisterClassA(*lppClasses, hInst);
2377     lppClasses++;
2378     iCount--;
2379   }
2380 }
2381
2382 /*************************************************************************
2383  *      @       [SHLWAPI.239]
2384  *
2385  * Unicode version of SHUnregisterClassesA.
2386  */
2387 void WINAPI SHUnregisterClassesW(HINSTANCE hInst, LPCWSTR *lppClasses, INT iCount)
2388 {
2389   WNDCLASSW WndClass;
2390
2391   TRACE("(%p,%p,%d)\n", hInst, lppClasses, iCount);
2392
2393   while (iCount > 0)
2394   {
2395     if (GetClassInfoW(hInst, *lppClasses, &WndClass))
2396       UnregisterClassW(*lppClasses, hInst);
2397     lppClasses++;
2398     iCount--;
2399   }
2400 }
2401
2402 /*************************************************************************
2403  *      @       [SHLWAPI.240]
2404  *
2405  * Call The correct (Ascii/Unicode) default window procedure for a window.
2406  *
2407  * PARAMS
2408  *  hWnd     [I] Window to call the default procedure for
2409  *  uMessage [I] Message ID
2410  *  wParam   [I] WPARAM of message
2411  *  lParam   [I] LPARAM of message
2412  *
2413  * RETURNS
2414  *  The result of calling DefWindowProcA() or DefWindowProcW().
2415  */
2416 LRESULT CALLBACK SHDefWindowProc(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
2417 {
2418         if (IsWindowUnicode(hWnd))
2419                 return DefWindowProcW(hWnd, uMessage, wParam, lParam);
2420         return DefWindowProcA(hWnd, uMessage, wParam, lParam);
2421 }
2422
2423 /*************************************************************************
2424  *      @       [SHLWAPI.256]
2425  */
2426 HRESULT WINAPI IUnknown_GetSite(LPUNKNOWN lpUnknown, REFIID iid, PVOID *lppSite)
2427 {
2428   HRESULT hRet = E_INVALIDARG;
2429   LPOBJECTWITHSITE lpSite = NULL;
2430
2431   TRACE("(%p,%s,%p)\n", lpUnknown, debugstr_guid(iid), lppSite);
2432
2433   if (lpUnknown && iid && lppSite)
2434   {
2435     hRet = IUnknown_QueryInterface(lpUnknown, &IID_IObjectWithSite,
2436                                    (void**)&lpSite);
2437     if (SUCCEEDED(hRet) && lpSite)
2438     {
2439       hRet = IObjectWithSite_GetSite(lpSite, iid, lppSite);
2440       IObjectWithSite_Release(lpSite);
2441     }
2442   }
2443   return hRet;
2444 }
2445
2446 /*************************************************************************
2447  *      @       [SHLWAPI.257]
2448  *
2449  * Create a worker window using CreateWindowExA().
2450  *
2451  * PARAMS
2452  *  wndProc    [I] Window procedure
2453  *  hWndParent [I] Parent window
2454  *  dwExStyle  [I] Extra style flags
2455  *  dwStyle    [I] Style flags
2456  *  hMenu      [I] Window menu
2457  *  z          [I] Unknown
2458  *
2459  * RETURNS
2460  *  Success: The window handle of the newly created window.
2461  *  Failure: 0.
2462  */
2463 HWND WINAPI SHCreateWorkerWindowA(LONG wndProc, HWND hWndParent, DWORD dwExStyle,
2464                         DWORD dwStyle, HMENU hMenu, LONG z)
2465 {
2466   static const char szClass[] = "WorkerA";
2467   WNDCLASSA wc;
2468   HWND hWnd;
2469
2470   TRACE("(0x%08x,%p,0x%08x,0x%08x,%p,0x%08x)\n",
2471          wndProc, hWndParent, dwExStyle, dwStyle, hMenu, z);
2472
2473   /* Create Window class */
2474   wc.style         = 0;
2475   wc.lpfnWndProc   = DefWindowProcA;
2476   wc.cbClsExtra    = 0;
2477   wc.cbWndExtra    = 4;
2478   wc.hInstance     = shlwapi_hInstance;
2479   wc.hIcon         = NULL;
2480   wc.hCursor       = LoadCursorA(NULL, (LPSTR)IDC_ARROW);
2481   wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
2482   wc.lpszMenuName  = NULL;
2483   wc.lpszClassName = szClass;
2484
2485   SHRegisterClassA(&wc); /* Register class */
2486
2487   /* FIXME: Set extra bits in dwExStyle */
2488
2489   hWnd = CreateWindowExA(dwExStyle, szClass, 0, dwStyle, 0, 0, 0, 0,
2490                          hWndParent, hMenu, shlwapi_hInstance, 0);
2491   if (hWnd)
2492   {
2493     SetWindowLongPtrW(hWnd, DWLP_MSGRESULT, z);
2494
2495     if (wndProc)
2496       SetWindowLongPtrA(hWnd, GWLP_WNDPROC, wndProc);
2497   }
2498   return hWnd;
2499 }
2500
2501 typedef struct tagPOLICYDATA
2502 {
2503   DWORD policy;        /* flags value passed to SHRestricted */
2504   LPCWSTR appstr;      /* application str such as "Explorer" */
2505   LPCWSTR keystr;      /* name of the actual registry key / policy */
2506 } POLICYDATA, *LPPOLICYDATA;
2507
2508 #define SHELL_NO_POLICY 0xffffffff
2509
2510 /* default shell policy registry key */
2511 static const WCHAR strRegistryPolicyW[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o',
2512                                       's','o','f','t','\\','W','i','n','d','o','w','s','\\',
2513                                       'C','u','r','r','e','n','t','V','e','r','s','i','o','n',
2514                                       '\\','P','o','l','i','c','i','e','s',0};
2515
2516 /*************************************************************************
2517  * @                          [SHLWAPI.271]
2518  *
2519  * Retrieve a policy value from the registry.
2520  *
2521  * PARAMS
2522  *  lpSubKey   [I]   registry key name
2523  *  lpSubName  [I]   subname of registry key
2524  *  lpValue    [I]   value name of registry value
2525  *
2526  * RETURNS
2527  *  the value associated with the registry key or 0 if not found
2528  */
2529 DWORD WINAPI SHGetRestriction(LPCWSTR lpSubKey, LPCWSTR lpSubName, LPCWSTR lpValue)
2530 {
2531         DWORD retval, datsize = sizeof(retval);
2532         HKEY hKey;
2533
2534         if (!lpSubKey)
2535           lpSubKey = strRegistryPolicyW;
2536
2537         retval = RegOpenKeyW(HKEY_LOCAL_MACHINE, lpSubKey, &hKey);
2538     if (retval != ERROR_SUCCESS)
2539           retval = RegOpenKeyW(HKEY_CURRENT_USER, lpSubKey, &hKey);
2540         if (retval != ERROR_SUCCESS)
2541           return 0;
2542
2543         SHGetValueW(hKey, lpSubName, lpValue, NULL, (LPBYTE)&retval, &datsize);
2544         RegCloseKey(hKey);
2545         return retval;
2546 }
2547
2548 /*************************************************************************
2549  * @                         [SHLWAPI.266]
2550  *
2551  * Helper function to retrieve the possibly cached value for a specific policy
2552  *
2553  * PARAMS
2554  *  policy     [I]   The policy to look for
2555  *  initial    [I]   Main registry key to open, if NULL use default
2556  *  polTable   [I]   Table of known policies, 0 terminated
2557  *  polArr     [I]   Cache array of policy values
2558  *
2559  * RETURNS
2560  *  The retrieved policy value or 0 if not successful
2561  *
2562  * NOTES
2563  *  This function is used by the native SHRestricted function to search for the
2564  *  policy and cache it once retrieved. The current Wine implementation uses a
2565  *  different POLICYDATA structure and implements a similar algorithm adapted to
2566  *  that structure.
2567  */
2568 DWORD WINAPI SHRestrictionLookup(
2569         DWORD policy,
2570         LPCWSTR initial,
2571         LPPOLICYDATA polTable,
2572         LPDWORD polArr)
2573 {
2574         TRACE("(0x%08x %s %p %p)\n", policy, debugstr_w(initial), polTable, polArr);
2575
2576         if (!polTable || !polArr)
2577           return 0;
2578
2579         for (;polTable->policy; polTable++, polArr++)
2580         {
2581           if (policy == polTable->policy)
2582           {
2583             /* we have a known policy */
2584
2585             /* check if this policy has been cached */
2586                 if (*polArr == SHELL_NO_POLICY)
2587               *polArr = SHGetRestriction(initial, polTable->appstr, polTable->keystr);
2588             return *polArr;
2589           }
2590         }
2591         /* we don't know this policy, return 0 */
2592         TRACE("unknown policy: (%08x)\n", policy);
2593         return 0;
2594 }
2595
2596 /*************************************************************************
2597  *      @       [SHLWAPI.267]
2598  *
2599  * Get an interface from an object.
2600  *
2601  * RETURNS
2602  *  Success: S_OK. ppv contains the requested interface.
2603  *  Failure: An HRESULT error code.
2604  *
2605  * NOTES
2606  *   This QueryInterface asks the inner object for an interface. In case
2607  *   of aggregation this request would be forwarded by the inner to the
2608  *   outer object. This function asks the inner object directly for the
2609  *   interface circumventing the forwarding to the outer object.
2610  */
2611 HRESULT WINAPI SHWeakQueryInterface(
2612         IUnknown * pUnk,   /* [in] Outer object */
2613         IUnknown * pInner, /* [in] Inner object */
2614         IID * riid, /* [in] Interface GUID to query for */
2615         LPVOID* ppv) /* [out] Destination for queried interface */
2616 {
2617         HRESULT hret = E_NOINTERFACE;
2618         TRACE("(pUnk=%p pInner=%p\n\tIID:  %s %p)\n",pUnk,pInner,debugstr_guid(riid), ppv);
2619
2620         *ppv = NULL;
2621         if(pUnk && pInner) {
2622             hret = IUnknown_QueryInterface(pInner, riid, (LPVOID*)ppv);
2623             if (SUCCEEDED(hret)) IUnknown_Release(pUnk);
2624         }
2625         TRACE("-- 0x%08x\n", hret);
2626         return hret;
2627 }
2628
2629 /*************************************************************************
2630  *      @       [SHLWAPI.268]
2631  *
2632  * Move a reference from one interface to another.
2633  *
2634  * PARAMS
2635  *   lpDest     [O] Destination to receive the reference
2636  *   lppUnknown [O] Source to give up the reference to lpDest
2637  *
2638  * RETURNS
2639  *  Nothing.
2640  */
2641 VOID WINAPI SHWeakReleaseInterface(IUnknown *lpDest, IUnknown **lppUnknown)
2642 {
2643   TRACE("(%p,%p)\n", lpDest, lppUnknown);
2644
2645   if (*lppUnknown)
2646   {
2647     /* Copy Reference*/
2648     IUnknown_AddRef(lpDest);
2649     IUnknown_AtomicRelease(lppUnknown); /* Release existing interface */
2650   }
2651 }
2652
2653 /*************************************************************************
2654  *      @       [SHLWAPI.269]
2655  *
2656  * Convert an ASCII string of a CLSID into a CLSID.
2657  *
2658  * PARAMS
2659  *  idstr [I] String representing a CLSID in registry format
2660  *  id    [O] Destination for the converted CLSID
2661  *
2662  * RETURNS
2663  *  Success: TRUE. id contains the converted CLSID.
2664  *  Failure: FALSE.
2665  */
2666 BOOL WINAPI GUIDFromStringA(LPCSTR idstr, CLSID *id)
2667 {
2668   WCHAR wClsid[40];
2669   MultiByteToWideChar(CP_ACP, 0, idstr, -1, wClsid, sizeof(wClsid)/sizeof(WCHAR));
2670   return SUCCEEDED(CLSIDFromString(wClsid, id));
2671 }
2672
2673 /*************************************************************************
2674  *      @       [SHLWAPI.270]
2675  *
2676  * Unicode version of GUIDFromStringA.
2677  */
2678 BOOL WINAPI GUIDFromStringW(LPCWSTR idstr, CLSID *id)
2679 {
2680     return SUCCEEDED(CLSIDFromString((LPOLESTR)idstr, id));
2681 }
2682
2683 /*************************************************************************
2684  *      @       [SHLWAPI.276]
2685  *
2686  * Determine if the browser is integrated into the shell, and set a registry
2687  * key accordingly.
2688  *
2689  * PARAMS
2690  *  None.
2691  *
2692  * RETURNS
2693  *  1, If the browser is not integrated.
2694  *  2, If the browser is integrated.
2695  *
2696  * NOTES
2697  *  The key "HKLM\Software\Microsoft\Internet Explorer\IntegratedBrowser" is
2698  *  either set to TRUE, or removed depending on whether the browser is deemed
2699  *  to be integrated.
2700  */
2701 DWORD WINAPI WhichPlatform(void)
2702 {
2703   static const char szIntegratedBrowser[] = "IntegratedBrowser";
2704   static DWORD dwState = 0;
2705   HKEY hKey;
2706   DWORD dwRet, dwData, dwSize;
2707   HMODULE hshell32;
2708
2709   if (dwState)
2710     return dwState;
2711
2712   /* If shell32 exports DllGetVersion(), the browser is integrated */
2713   dwState = 1;
2714   hshell32 = LoadLibraryA("shell32.dll");
2715   if (hshell32)
2716   {
2717     FARPROC pDllGetVersion;
2718     pDllGetVersion = GetProcAddress(hshell32, "DllGetVersion");
2719     dwState = pDllGetVersion ? 2 : 1;
2720     FreeLibrary(hshell32);
2721   }
2722
2723   /* Set or delete the key accordingly */
2724   dwRet = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
2725                         "Software\\Microsoft\\Internet Explorer", 0,
2726                          KEY_ALL_ACCESS, &hKey);
2727   if (!dwRet)
2728   {
2729     dwRet = RegQueryValueExA(hKey, szIntegratedBrowser, 0, 0,
2730                              (LPBYTE)&dwData, &dwSize);
2731
2732     if (!dwRet && dwState == 1)
2733     {
2734       /* Value exists but browser is not integrated */
2735       RegDeleteValueA(hKey, szIntegratedBrowser);
2736     }
2737     else if (dwRet && dwState == 2)
2738     {
2739       /* Browser is integrated but value does not exist */
2740       dwData = TRUE;
2741       RegSetValueExA(hKey, szIntegratedBrowser, 0, REG_DWORD,
2742                      (LPBYTE)&dwData, sizeof(dwData));
2743     }
2744     RegCloseKey(hKey);
2745   }
2746   return dwState;
2747 }
2748
2749 /*************************************************************************
2750  *      @       [SHLWAPI.278]
2751  *
2752  * Unicode version of SHCreateWorkerWindowA.
2753  */
2754 HWND WINAPI SHCreateWorkerWindowW(LONG wndProc, HWND hWndParent, DWORD dwExStyle,
2755                         DWORD dwStyle, HMENU hMenu, LONG z)
2756 {
2757   static const WCHAR szClass[] = { 'W', 'o', 'r', 'k', 'e', 'r', 'W', '\0' };
2758   WNDCLASSW wc;
2759   HWND hWnd;
2760
2761   TRACE("(0x%08x,%p,0x%08x,0x%08x,%p,0x%08x)\n",
2762          wndProc, hWndParent, dwExStyle, dwStyle, hMenu, z);
2763
2764   /* If our OS is natively ASCII, use the ASCII version */
2765   if (!(GetVersion() & 0x80000000))  /* NT */
2766     return SHCreateWorkerWindowA(wndProc, hWndParent, dwExStyle, dwStyle, hMenu, z);
2767
2768   /* Create Window class */
2769   wc.style         = 0;
2770   wc.lpfnWndProc   = DefWindowProcW;
2771   wc.cbClsExtra    = 0;
2772   wc.cbWndExtra    = 4;
2773   wc.hInstance     = shlwapi_hInstance;
2774   wc.hIcon         = NULL;
2775   wc.hCursor       = LoadCursorW(NULL, (LPWSTR)IDC_ARROW);
2776   wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
2777   wc.lpszMenuName  = NULL;
2778   wc.lpszClassName = szClass;
2779
2780   SHRegisterClassW(&wc); /* Register class */
2781
2782   /* FIXME: Set extra bits in dwExStyle */
2783
2784   hWnd = CreateWindowExW(dwExStyle, szClass, 0, dwStyle, 0, 0, 0, 0,
2785                          hWndParent, hMenu, shlwapi_hInstance, 0);
2786   if (hWnd)
2787   {
2788     SetWindowLongPtrW(hWnd, DWLP_MSGRESULT, z);
2789
2790     if (wndProc)
2791       SetWindowLongPtrW(hWnd, GWLP_WNDPROC, wndProc);
2792   }
2793   return hWnd;
2794 }
2795
2796 /*************************************************************************
2797  *      @       [SHLWAPI.279]
2798  *
2799  * Get and show a context menu from a shell folder.
2800  *
2801  * PARAMS
2802  *  hWnd           [I] Window displaying the shell folder
2803  *  lpFolder       [I] IShellFolder interface
2804  *  lpApidl        [I] Id for the particular folder desired
2805  *
2806  * RETURNS
2807  *  Success: S_OK.
2808  *  Failure: An HRESULT error code indicating the error.
2809  */
2810 HRESULT WINAPI SHInvokeDefaultCommand(HWND hWnd, IShellFolder* lpFolder, LPCITEMIDLIST lpApidl)
2811 {
2812   return SHInvokeCommand(hWnd, lpFolder, lpApidl, FALSE);
2813 }
2814
2815 /*************************************************************************
2816  *      @       [SHLWAPI.281]
2817  *
2818  * _SHPackDispParamsV
2819  */
2820 HRESULT WINAPI SHPackDispParamsV(DISPPARAMS *params, VARIANTARG *args, UINT cnt, va_list valist)
2821 {
2822   VARIANTARG *iter;
2823
2824   TRACE("(%p %p %u ...)\n", params, args, cnt);
2825
2826   params->rgvarg = args;
2827   params->rgdispidNamedArgs = NULL;
2828   params->cArgs = cnt;
2829   params->cNamedArgs = 0;
2830
2831   iter = args+cnt;
2832
2833   while(iter-- > args) {
2834     V_VT(iter) = va_arg(valist, enum VARENUM);
2835
2836     TRACE("vt=%d\n", V_VT(iter));
2837
2838     if(V_VT(iter) & VT_BYREF) {
2839       V_BYREF(iter) = va_arg(valist, LPVOID);
2840     } else {
2841       switch(V_VT(iter)) {
2842       case VT_I4:
2843         V_I4(iter) = va_arg(valist, LONG);
2844         break;
2845       case VT_BSTR:
2846         V_BSTR(iter) = va_arg(valist, BSTR);
2847         break;
2848       case VT_DISPATCH:
2849         V_DISPATCH(iter) = va_arg(valist, IDispatch*);
2850         break;
2851       case VT_BOOL:
2852         V_BOOL(iter) = va_arg(valist, int);
2853         break;
2854       case VT_UNKNOWN:
2855         V_UNKNOWN(iter) = va_arg(valist, IUnknown*);
2856         break;
2857       default:
2858         V_VT(iter) = VT_I4;
2859         V_I4(iter) = va_arg(valist, LONG);
2860       }
2861     }
2862   }
2863
2864   return S_OK;
2865 }
2866
2867 /*************************************************************************
2868  *      @       [SHLWAPI.282]
2869  *
2870  * SHPackDispParams
2871  */
2872 HRESULT WINAPIV SHPackDispParams(DISPPARAMS *params, VARIANTARG *args, UINT cnt, ...)
2873 {
2874   va_list valist;
2875   HRESULT hres;
2876
2877   va_start(valist, cnt);
2878
2879   hres = SHPackDispParamsV(params, args, cnt, valist);
2880
2881   va_end(valist);
2882   return hres;
2883 }
2884
2885 /*************************************************************************
2886  *      SHLWAPI_InvokeByIID
2887  *
2888  *   This helper function calls IDispatch::Invoke for each sink
2889  * which implements given iid or IDispatch.
2890  *
2891  */
2892 static HRESULT SHLWAPI_InvokeByIID(
2893         IConnectionPoint* iCP,
2894         REFIID iid,
2895         DISPID dispId,
2896         DISPPARAMS* dispParams)
2897 {
2898   IEnumConnections *enumerator;
2899   CONNECTDATA rgcd;
2900
2901   HRESULT result = IConnectionPoint_EnumConnections(iCP, &enumerator);
2902   if (FAILED(result))
2903     return result;
2904
2905   while(IEnumConnections_Next(enumerator, 1, &rgcd, NULL)==S_OK)
2906   {
2907     IDispatch *dispIface;
2908     if (SUCCEEDED(IUnknown_QueryInterface(rgcd.pUnk, iid, (LPVOID*)&dispIface)) ||
2909         SUCCEEDED(IUnknown_QueryInterface(rgcd.pUnk, &IID_IDispatch, (LPVOID*)&dispIface)))
2910     {
2911       IDispatch_Invoke(dispIface, dispId, &IID_NULL, 0, DISPATCH_METHOD, dispParams, NULL, NULL, NULL);
2912       IDispatch_Release(dispIface);
2913     }
2914   }
2915
2916   IEnumConnections_Release(enumerator);
2917
2918   return S_OK;
2919 }
2920
2921 /*************************************************************************
2922  *      @       [SHLWAPI.284]
2923  *
2924  *  IConnectionPoint_SimpleInvoke
2925  */
2926 HRESULT WINAPI IConnectionPoint_SimpleInvoke(
2927         IConnectionPoint* iCP,
2928         DISPID dispId,
2929         DISPPARAMS* dispParams)
2930 {
2931   IID iid;
2932   HRESULT result;
2933
2934   TRACE("(%p)->(0x%x %p)\n",iCP,dispId,dispParams);
2935
2936   result = IConnectionPoint_GetConnectionInterface(iCP, &iid);
2937   if (SUCCEEDED(result))
2938     result = SHLWAPI_InvokeByIID(iCP, &iid, dispId, dispParams);
2939
2940   return result;
2941 }
2942
2943 /*************************************************************************
2944  *      @       [SHLWAPI.285]
2945  *
2946  * Notify an IConnectionPoint object of changes.
2947  *
2948  * PARAMS
2949  *  lpCP   [I] Object to notify
2950  *  dispID [I]
2951  *
2952  * RETURNS
2953  *  Success: S_OK.
2954  *  Failure: E_NOINTERFACE, if lpCP is NULL or does not support the
2955  *           IConnectionPoint interface.
2956  */
2957 HRESULT WINAPI IConnectionPoint_OnChanged(IConnectionPoint* lpCP, DISPID dispID)
2958 {
2959   IEnumConnections *lpEnum;
2960   HRESULT hRet = E_NOINTERFACE;
2961
2962   TRACE("(%p,0x%8X)\n", lpCP, dispID);
2963
2964   /* Get an enumerator for the connections */
2965   if (lpCP)
2966     hRet = IConnectionPoint_EnumConnections(lpCP, &lpEnum);
2967
2968   if (SUCCEEDED(hRet))
2969   {
2970     IPropertyNotifySink *lpSink;
2971     CONNECTDATA connData;
2972     ULONG ulFetched;
2973
2974     /* Call OnChanged() for every notify sink in the connection point */
2975     while (IEnumConnections_Next(lpEnum, 1, &connData, &ulFetched) == S_OK)
2976     {
2977       if (SUCCEEDED(IUnknown_QueryInterface(connData.pUnk, &IID_IPropertyNotifySink, (void**)&lpSink)) &&
2978           lpSink)
2979       {
2980         IPropertyNotifySink_OnChanged(lpSink, dispID);
2981         IPropertyNotifySink_Release(lpSink);
2982       }
2983       IUnknown_Release(connData.pUnk);
2984     }
2985
2986     IEnumConnections_Release(lpEnum);
2987   }
2988   return hRet;
2989 }
2990
2991 /*************************************************************************
2992  *      @       [SHLWAPI.286]
2993  *
2994  *  IUnknown_CPContainerInvokeParam
2995  */
2996 HRESULT WINAPIV IUnknown_CPContainerInvokeParam(
2997         IUnknown *container,
2998         REFIID riid,
2999         DISPID dispId,
3000         VARIANTARG* buffer,
3001         DWORD cParams, ...)
3002 {
3003   HRESULT result;
3004   IConnectionPoint *iCP;
3005   IConnectionPointContainer *iCPC;
3006   DISPPARAMS dispParams = {buffer, NULL, cParams, 0};
3007   va_list valist;
3008
3009   if (!container)
3010     return E_NOINTERFACE;
3011
3012   result = IUnknown_QueryInterface(container, &IID_IConnectionPointContainer,(LPVOID*) &iCPC);
3013   if (FAILED(result))
3014       return result;
3015
3016   result = IConnectionPointContainer_FindConnectionPoint(iCPC, riid, &iCP);
3017   IConnectionPointContainer_Release(iCPC);
3018   if(FAILED(result))
3019       return result;
3020
3021   va_start(valist, cParams);
3022   SHPackDispParamsV(&dispParams, buffer, cParams, valist);
3023   va_end(valist);
3024
3025   result = SHLWAPI_InvokeByIID(iCP, riid, dispId, &dispParams);
3026   IConnectionPoint_Release(iCP);
3027
3028   return result;
3029 }
3030
3031 /*************************************************************************
3032  *      @       [SHLWAPI.287]
3033  *
3034  * Notify an IConnectionPointContainer object of changes.
3035  *
3036  * PARAMS
3037  *  lpUnknown [I] Object to notify
3038  *  dispID    [I]
3039  *
3040  * RETURNS
3041  *  Success: S_OK.
3042  *  Failure: E_NOINTERFACE, if lpUnknown is NULL or does not support the
3043  *           IConnectionPointContainer interface.
3044  */
3045 HRESULT WINAPI IUnknown_CPContainerOnChanged(IUnknown *lpUnknown, DISPID dispID)
3046 {
3047   IConnectionPointContainer* lpCPC = NULL;
3048   HRESULT hRet = E_NOINTERFACE;
3049
3050   TRACE("(%p,0x%8X)\n", lpUnknown, dispID);
3051
3052   if (lpUnknown)
3053     hRet = IUnknown_QueryInterface(lpUnknown, &IID_IConnectionPointContainer, (void**)&lpCPC);
3054
3055   if (SUCCEEDED(hRet))
3056   {
3057     IConnectionPoint* lpCP;
3058
3059     hRet = IConnectionPointContainer_FindConnectionPoint(lpCPC, &IID_IPropertyNotifySink, &lpCP);
3060     IConnectionPointContainer_Release(lpCPC);
3061
3062     hRet = IConnectionPoint_OnChanged(lpCP, dispID);
3063     IConnectionPoint_Release(lpCP);
3064   }
3065   return hRet;
3066 }
3067
3068 /*************************************************************************
3069  *      @       [SHLWAPI.289]
3070  *
3071  * See PlaySoundW.
3072  */
3073 BOOL WINAPI PlaySoundWrapW(LPCWSTR pszSound, HMODULE hmod, DWORD fdwSound)
3074 {
3075     return PlaySoundW(pszSound, hmod, fdwSound);
3076 }
3077
3078 /*************************************************************************
3079  *      @       [SHLWAPI.294]
3080  */
3081 BOOL WINAPI SHGetIniStringW(LPCWSTR str1, LPCWSTR str2, LPWSTR pStr, DWORD some_len, LPCWSTR lpStr2)
3082 {
3083     FIXME("(%s,%s,%p,%08x,%s): stub!\n", debugstr_w(str1), debugstr_w(str2),
3084         pStr, some_len, debugstr_w(lpStr2));
3085     return TRUE;
3086 }
3087
3088 /*************************************************************************
3089  *      @       [SHLWAPI.295]
3090  *
3091  * Called by ICQ2000b install via SHDOCVW:
3092  * str1: "InternetShortcut"
3093  * x: some unknown pointer
3094  * str2: "http://free.aol.com/tryaolfree/index.adp?139269"
3095  * str3: "C:\\WINDOWS\\Desktop.new2\\Free AOL & Unlimited Internet.url"
3096  *
3097  * In short: this one maybe creates a desktop link :-)
3098  */
3099 BOOL WINAPI SHSetIniStringW(LPWSTR str1, LPVOID x, LPWSTR str2, LPWSTR str3)
3100 {
3101     FIXME("(%s, %p, %s, %s), stub.\n", debugstr_w(str1), x, debugstr_w(str2), debugstr_w(str3));
3102     return TRUE;
3103 }
3104
3105 /*************************************************************************
3106  *      @       [SHLWAPI.313]
3107  *
3108  * See SHGetFileInfoW.
3109  */
3110 DWORD WINAPI SHGetFileInfoWrapW(LPCWSTR path, DWORD dwFileAttributes,
3111                          SHFILEINFOW *psfi, UINT sizeofpsfi, UINT flags)
3112 {
3113     return SHGetFileInfoW(path, dwFileAttributes, psfi, sizeofpsfi, flags);
3114 }
3115
3116 /*************************************************************************
3117  *      @       [SHLWAPI.318]
3118  *
3119  * See DragQueryFileW.
3120  */
3121 UINT WINAPI DragQueryFileWrapW(HDROP hDrop, UINT lFile, LPWSTR lpszFile, UINT lLength)
3122 {
3123     return DragQueryFileW(hDrop, lFile, lpszFile, lLength);
3124 }
3125
3126 /*************************************************************************
3127  *      @       [SHLWAPI.333]
3128  *
3129  * See SHBrowseForFolderW.
3130  */
3131 LPITEMIDLIST WINAPI SHBrowseForFolderWrapW(LPBROWSEINFOW lpBi)
3132 {
3133     return SHBrowseForFolderW(lpBi);
3134 }
3135
3136 /*************************************************************************
3137  *      @       [SHLWAPI.334]
3138  *
3139  * See SHGetPathFromIDListW.
3140  */
3141 BOOL WINAPI SHGetPathFromIDListWrapW(LPCITEMIDLIST pidl,LPWSTR pszPath)
3142 {
3143     return SHGetPathFromIDListW(pidl, pszPath);
3144 }
3145
3146 /*************************************************************************
3147  *      @       [SHLWAPI.335]
3148  *
3149  * See ShellExecuteExW.
3150  */
3151 BOOL WINAPI ShellExecuteExWrapW(LPSHELLEXECUTEINFOW lpExecInfo)
3152 {
3153     return ShellExecuteExW(lpExecInfo);
3154 }
3155
3156 /*************************************************************************
3157  *      @       [SHLWAPI.336]
3158  *
3159  * See SHFileOperationW.
3160  */
3161 INT WINAPI SHFileOperationWrapW(LPSHFILEOPSTRUCTW lpFileOp)
3162 {
3163     return SHFileOperationW(lpFileOp);
3164 }
3165
3166 /*************************************************************************
3167  *      @       [SHLWAPI.342]
3168  *
3169  */
3170 PVOID WINAPI SHInterlockedCompareExchange( PVOID *dest, PVOID xchg, PVOID compare )
3171 {
3172     return InterlockedCompareExchangePointer( dest, xchg, compare );
3173 }
3174
3175 /*************************************************************************
3176  *      @       [SHLWAPI.350]
3177  *
3178  * See GetFileVersionInfoSizeW.
3179  */
3180 DWORD WINAPI GetFileVersionInfoSizeWrapW( LPCWSTR filename, LPDWORD handle )
3181 {
3182     return GetFileVersionInfoSizeW( filename, handle );
3183 }
3184
3185 /*************************************************************************
3186  *      @       [SHLWAPI.351]
3187  *
3188  * See GetFileVersionInfoW.
3189  */
3190 BOOL  WINAPI GetFileVersionInfoWrapW( LPCWSTR filename, DWORD handle,
3191                                       DWORD datasize, LPVOID data )
3192 {
3193     return GetFileVersionInfoW( filename, handle, datasize, data );
3194 }
3195
3196 /*************************************************************************
3197  *      @       [SHLWAPI.352]
3198  *
3199  * See VerQueryValueW.
3200  */
3201 WORD WINAPI VerQueryValueWrapW( LPVOID pBlock, LPCWSTR lpSubBlock,
3202                                 LPVOID *lplpBuffer, UINT *puLen )
3203 {
3204     return VerQueryValueW( pBlock, lpSubBlock, lplpBuffer, puLen );
3205 }
3206
3207 #define IsIface(type) SUCCEEDED((hRet = IUnknown_QueryInterface(lpUnknown, &IID_##type, (void**)&lpObj)))
3208 #define IShellBrowser_EnableModeless IShellBrowser_EnableModelessSB
3209 #define EnableModeless(type) type##_EnableModeless((type*)lpObj, bModeless)
3210
3211 /*************************************************************************
3212  *      @       [SHLWAPI.355]
3213  *
3214  * Change the modality of a shell object.
3215  *
3216  * PARAMS
3217  *  lpUnknown [I] Object to make modeless
3218  *  bModeless [I] TRUE=Make modeless, FALSE=Make modal
3219  *
3220  * RETURNS
3221  *  Success: S_OK. The modality lpUnknown is changed.
3222  *  Failure: An HRESULT error code indicating the error.
3223  *
3224  * NOTES
3225  *  lpUnknown must support the IOleInPlaceFrame interface, the
3226  *  IInternetSecurityMgrSite interface, the IShellBrowser interface
3227  *  the IDocHostUIHandler interface, or the IOleInPlaceActiveObject interface,
3228  *  or this call will fail.
3229  */
3230 HRESULT WINAPI IUnknown_EnableModeless(IUnknown *lpUnknown, BOOL bModeless)
3231 {
3232   IUnknown *lpObj;
3233   HRESULT hRet;
3234
3235   TRACE("(%p,%d)\n", lpUnknown, bModeless);
3236
3237   if (!lpUnknown)
3238     return E_FAIL;
3239
3240   if (IsIface(IOleInPlaceActiveObject))
3241     EnableModeless(IOleInPlaceActiveObject);
3242   else if (IsIface(IOleInPlaceFrame))
3243     EnableModeless(IOleInPlaceFrame);
3244   else if (IsIface(IShellBrowser))
3245     EnableModeless(IShellBrowser);
3246   else if (IsIface(IInternetSecurityMgrSite))
3247     EnableModeless(IInternetSecurityMgrSite);
3248   else if (IsIface(IDocHostUIHandler))
3249     EnableModeless(IDocHostUIHandler);
3250   else
3251     return hRet;
3252
3253   IUnknown_Release(lpObj);
3254   return S_OK;
3255 }
3256
3257 /*************************************************************************
3258  *      @       [SHLWAPI.357]
3259  *
3260  * See SHGetNewLinkInfoW.
3261  */
3262 BOOL WINAPI SHGetNewLinkInfoWrapW(LPCWSTR pszLinkTo, LPCWSTR pszDir, LPWSTR pszName,
3263                         BOOL *pfMustCopy, UINT uFlags)
3264 {
3265     return SHGetNewLinkInfoW(pszLinkTo, pszDir, pszName, pfMustCopy, uFlags);
3266 }
3267
3268 /*************************************************************************
3269  *      @       [SHLWAPI.358]
3270  *
3271  * See SHDefExtractIconW.
3272  */
3273 UINT WINAPI SHDefExtractIconWrapW(LPCWSTR pszIconFile, int iIndex, UINT uFlags, HICON* phiconLarge,
3274                          HICON* phiconSmall, UINT nIconSize)
3275 {
3276     return SHDefExtractIconW(pszIconFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
3277 }
3278
3279 /*************************************************************************
3280  *      @       [SHLWAPI.363]
3281  *
3282  * Get and show a context menu from a shell folder.
3283  *
3284  * PARAMS
3285  *  hWnd           [I] Window displaying the shell folder
3286  *  lpFolder       [I] IShellFolder interface
3287  *  lpApidl        [I] Id for the particular folder desired
3288  *  bInvokeDefault [I] Whether to invoke the default menu item
3289  *
3290  * RETURNS
3291  *  Success: S_OK. If bInvokeDefault is TRUE, the default menu action was
3292  *           executed.
3293  *  Failure: An HRESULT error code indicating the error.
3294  */
3295 HRESULT WINAPI SHInvokeCommand(HWND hWnd, IShellFolder* lpFolder, LPCITEMIDLIST lpApidl, BOOL bInvokeDefault)
3296 {
3297   IContextMenu *iContext;
3298   HRESULT hRet = E_FAIL;
3299
3300   TRACE("(%p,%p,%p,%d)\n", hWnd, lpFolder, lpApidl, bInvokeDefault);
3301
3302   if (!lpFolder)
3303     return hRet;
3304
3305   /* Get the context menu from the shell folder */
3306   hRet = IShellFolder_GetUIObjectOf(lpFolder, hWnd, 1, &lpApidl,
3307                                     &IID_IContextMenu, 0, (void**)&iContext);
3308   if (SUCCEEDED(hRet))
3309   {
3310     HMENU hMenu;
3311     if ((hMenu = CreatePopupMenu()))
3312     {
3313       HRESULT hQuery;
3314       DWORD dwDefaultId = 0;
3315
3316       /* Add the context menu entries to the popup */
3317       hQuery = IContextMenu_QueryContextMenu(iContext, hMenu, 0, 1, 0x7FFF,
3318                                              bInvokeDefault ? CMF_NORMAL : CMF_DEFAULTONLY);
3319
3320       if (SUCCEEDED(hQuery))
3321       {
3322         if (bInvokeDefault &&
3323             (dwDefaultId = GetMenuDefaultItem(hMenu, 0, 0)) != 0xFFFFFFFF)
3324         {
3325           CMINVOKECOMMANDINFO cmIci;
3326           /* Invoke the default item */
3327           memset(&cmIci,0,sizeof(cmIci));
3328           cmIci.cbSize = sizeof(cmIci);
3329           cmIci.fMask = CMIC_MASK_ASYNCOK;
3330           cmIci.hwnd = hWnd;
3331           cmIci.lpVerb = MAKEINTRESOURCEA(dwDefaultId);
3332           cmIci.nShow = SW_SCROLLCHILDREN;
3333
3334           hRet = IContextMenu_InvokeCommand(iContext, &cmIci);
3335         }
3336       }
3337       DestroyMenu(hMenu);
3338     }
3339     IContextMenu_Release(iContext);
3340   }
3341   return hRet;
3342 }
3343
3344 /*************************************************************************
3345  *      @       [SHLWAPI.370]
3346  *
3347  * See ExtractIconW.
3348  */
3349 HICON WINAPI ExtractIconWrapW(HINSTANCE hInstance, LPCWSTR lpszExeFileName,
3350                          UINT nIconIndex)
3351 {
3352     return ExtractIconW(hInstance, lpszExeFileName, nIconIndex);
3353 }
3354
3355 /*************************************************************************
3356  *      @       [SHLWAPI.377]
3357  *
3358  * Load a library from the directory of a particular process.
3359  *
3360  * PARAMS
3361  *  new_mod        [I] Library name
3362  *  inst_hwnd      [I] Module whose directory is to be used
3363  *  dwCrossCodePage [I] Should be FALSE (currently ignored)
3364  *
3365  * RETURNS
3366  *  Success: A handle to the loaded module
3367  *  Failure: A NULL handle.
3368  */
3369 HMODULE WINAPI MLLoadLibraryA(LPCSTR new_mod, HMODULE inst_hwnd, DWORD dwCrossCodePage)
3370 {
3371   /* FIXME: Native appears to do DPA_Create and a DPA_InsertPtr for
3372    *        each call here.
3373    * FIXME: Native shows calls to:
3374    *  SHRegGetUSValue for "Software\Microsoft\Internet Explorer\International"
3375    *                      CheckVersion
3376    *  RegOpenKeyExA for "HKLM\Software\Microsoft\Internet Explorer"
3377    *  RegQueryValueExA for "LPKInstalled"
3378    *  RegCloseKey
3379    *  RegOpenKeyExA for "HKCU\Software\Microsoft\Internet Explorer\International"
3380    *  RegQueryValueExA for "ResourceLocale"
3381    *  RegCloseKey
3382    *  RegOpenKeyExA for "HKLM\Software\Microsoft\Active Setup\Installed Components\{guid}"
3383    *  RegQueryValueExA for "Locale"
3384    *  RegCloseKey
3385    *  and then tests the Locale ("en" for me).
3386    *     code below
3387    *  after the code then a DPA_Create (first time) and DPA_InsertPtr are done.
3388    */
3389     CHAR mod_path[2*MAX_PATH];
3390     LPSTR ptr;
3391     DWORD len;
3392
3393     FIXME("(%s,%p,%d) semi-stub!\n", debugstr_a(new_mod), inst_hwnd, dwCrossCodePage);
3394     len = GetModuleFileNameA(inst_hwnd, mod_path, sizeof(mod_path));
3395     if (!len || len >= sizeof(mod_path)) return NULL;
3396
3397     ptr = strrchr(mod_path, '\\');
3398     if (ptr) {
3399         strcpy(ptr+1, new_mod);
3400         TRACE("loading %s\n", debugstr_a(mod_path));
3401         return LoadLibraryA(mod_path);
3402     }
3403     return NULL;
3404 }
3405
3406 /*************************************************************************
3407  *      @       [SHLWAPI.378]
3408  *
3409  * Unicode version of MLLoadLibraryA.
3410  */
3411 HMODULE WINAPI MLLoadLibraryW(LPCWSTR new_mod, HMODULE inst_hwnd, DWORD dwCrossCodePage)
3412 {
3413     WCHAR mod_path[2*MAX_PATH];
3414     LPWSTR ptr;
3415     DWORD len;
3416
3417     FIXME("(%s,%p,%d) semi-stub!\n", debugstr_w(new_mod), inst_hwnd, dwCrossCodePage);
3418     len = GetModuleFileNameW(inst_hwnd, mod_path, sizeof(mod_path) / sizeof(WCHAR));
3419     if (!len || len >= sizeof(mod_path) / sizeof(WCHAR)) return NULL;
3420
3421     ptr = strrchrW(mod_path, '\\');
3422     if (ptr) {
3423         strcpyW(ptr+1, new_mod);
3424         TRACE("loading %s\n", debugstr_w(mod_path));
3425         return LoadLibraryW(mod_path);
3426     }
3427     return NULL;
3428 }
3429
3430 /*************************************************************************
3431  * ColorAdjustLuma      [SHLWAPI.@]
3432  *
3433  * Adjust the luminosity of a color
3434  *
3435  * PARAMS
3436  *  cRGB         [I] RGB value to convert
3437  *  dwLuma       [I] Luma adjustment
3438  *  bUnknown     [I] Unknown
3439  *
3440  * RETURNS
3441  *  The adjusted RGB color.
3442  */
3443 COLORREF WINAPI ColorAdjustLuma(COLORREF cRGB, int dwLuma, BOOL bUnknown)
3444 {
3445   TRACE("(0x%8x,%d,%d)\n", cRGB, dwLuma, bUnknown);
3446
3447   if (dwLuma)
3448   {
3449     WORD wH, wL, wS;
3450
3451     ColorRGBToHLS(cRGB, &wH, &wL, &wS);
3452
3453     FIXME("Ignoring luma adjustment\n");
3454
3455     /* FIXME: The adjustment is not linear */
3456
3457     cRGB = ColorHLSToRGB(wH, wL, wS);
3458   }
3459   return cRGB;
3460 }
3461
3462 /*************************************************************************
3463  *      @       [SHLWAPI.389]
3464  *
3465  * See GetSaveFileNameW.
3466  */
3467 BOOL WINAPI GetSaveFileNameWrapW(LPOPENFILENAMEW ofn)
3468 {
3469     return GetSaveFileNameW(ofn);
3470 }
3471
3472 /*************************************************************************
3473  *      @       [SHLWAPI.390]
3474  *
3475  * See WNetRestoreConnectionW.
3476  */
3477 DWORD WINAPI WNetRestoreConnectionWrapW(HWND hwndOwner, LPWSTR lpszDevice)
3478 {
3479     return WNetRestoreConnectionW(hwndOwner, lpszDevice);
3480 }
3481
3482 /*************************************************************************
3483  *      @       [SHLWAPI.391]
3484  *
3485  * See WNetGetLastErrorW.
3486  */
3487 DWORD WINAPI WNetGetLastErrorWrapW(LPDWORD lpError, LPWSTR lpErrorBuf, DWORD nErrorBufSize,
3488                          LPWSTR lpNameBuf, DWORD nNameBufSize)
3489 {
3490     return WNetGetLastErrorW(lpError, lpErrorBuf, nErrorBufSize, lpNameBuf, nNameBufSize);
3491 }
3492
3493 /*************************************************************************
3494  *      @       [SHLWAPI.401]
3495  *
3496  * See PageSetupDlgW.
3497  */
3498 BOOL WINAPI PageSetupDlgWrapW(LPPAGESETUPDLGW pagedlg)
3499 {
3500     return PageSetupDlgW(pagedlg);
3501 }
3502
3503 /*************************************************************************
3504  *      @       [SHLWAPI.402]
3505  *
3506  * See PrintDlgW.
3507  */
3508 BOOL WINAPI PrintDlgWrapW(LPPRINTDLGW printdlg)
3509 {
3510     return PrintDlgW(printdlg);
3511 }
3512
3513 /*************************************************************************
3514  *      @       [SHLWAPI.403]
3515  *
3516  * See GetOpenFileNameW.
3517  */
3518 BOOL WINAPI GetOpenFileNameWrapW(LPOPENFILENAMEW ofn)
3519 {
3520     return GetOpenFileNameW(ofn);
3521 }
3522
3523 /*************************************************************************
3524  *      @       [SHLWAPI.404]
3525  */
3526 HRESULT WINAPI SHIShellFolder_EnumObjects(LPSHELLFOLDER lpFolder, HWND hwnd, SHCONTF flags, IEnumIDList **ppenum)
3527 {
3528     IPersist *persist;
3529     HRESULT hr;
3530
3531     hr = IShellFolder_QueryInterface(lpFolder, &IID_IPersist, (LPVOID)&persist);
3532     if(SUCCEEDED(hr))
3533     {
3534         CLSID clsid;
3535         hr = IPersist_GetClassID(persist, &clsid);
3536         if(SUCCEEDED(hr))
3537         {
3538             if(IsEqualCLSID(&clsid, &CLSID_ShellFSFolder))
3539                 hr = IShellFolder_EnumObjects(lpFolder, hwnd, flags, ppenum);
3540             else
3541                 hr = E_FAIL;
3542         }
3543         IPersist_Release(persist);
3544     }
3545     return hr;
3546 }
3547
3548 /* INTERNAL: Map from HLS color space to RGB */
3549 static WORD WINAPI ConvertHue(int wHue, WORD wMid1, WORD wMid2)
3550 {
3551   wHue = wHue > 240 ? wHue - 240 : wHue < 0 ? wHue + 240 : wHue;
3552
3553   if (wHue > 160)
3554     return wMid1;
3555   else if (wHue > 120)
3556     wHue = 160 - wHue;
3557   else if (wHue > 40)
3558     return wMid2;
3559
3560   return ((wHue * (wMid2 - wMid1) + 20) / 40) + wMid1;
3561 }
3562
3563 /* Convert to RGB and scale into RGB range (0..255) */
3564 #define GET_RGB(h) (ConvertHue(h, wMid1, wMid2) * 255 + 120) / 240
3565
3566 /*************************************************************************
3567  *      ColorHLSToRGB   [SHLWAPI.@]
3568  *
3569  * Convert from hls color space into an rgb COLORREF.
3570  *
3571  * PARAMS
3572  *  wHue        [I] Hue amount
3573  *  wLuminosity [I] Luminosity amount
3574  *  wSaturation [I] Saturation amount
3575  *
3576  * RETURNS
3577  *  A COLORREF representing the converted color.
3578  *
3579  * NOTES
3580  *  Input hls values are constrained to the range (0..240).
3581  */
3582 COLORREF WINAPI ColorHLSToRGB(WORD wHue, WORD wLuminosity, WORD wSaturation)
3583 {
3584   WORD wRed;
3585
3586   if (wSaturation)
3587   {
3588     WORD wGreen, wBlue, wMid1, wMid2;
3589
3590     if (wLuminosity > 120)
3591       wMid2 = wSaturation + wLuminosity - (wSaturation * wLuminosity + 120) / 240;
3592     else
3593       wMid2 = ((wSaturation + 240) * wLuminosity + 120) / 240;
3594
3595     wMid1 = wLuminosity * 2 - wMid2;
3596
3597     wRed   = GET_RGB(wHue + 80);
3598     wGreen = GET_RGB(wHue);
3599     wBlue  = GET_RGB(wHue - 80);
3600
3601     return RGB(wRed, wGreen, wBlue);
3602   }
3603
3604   wRed = wLuminosity * 255 / 240;
3605   return RGB(wRed, wRed, wRed);
3606 }
3607
3608 /*************************************************************************
3609  *      @       [SHLWAPI.413]
3610  *
3611  * Get the current docking status of the system.
3612  *
3613  * PARAMS
3614  *  dwFlags [I] DOCKINFO_ flags from "winbase.h", unused
3615  *
3616  * RETURNS
3617  *  One of DOCKINFO_UNDOCKED, DOCKINFO_UNDOCKED, or 0 if the system is not
3618  *  a notebook.
3619  */
3620 DWORD WINAPI SHGetMachineInfo(DWORD dwFlags)
3621 {
3622   HW_PROFILE_INFOA hwInfo;
3623
3624   TRACE("(0x%08x)\n", dwFlags);
3625
3626   GetCurrentHwProfileA(&hwInfo);
3627   switch (hwInfo.dwDockInfo & (DOCKINFO_DOCKED|DOCKINFO_UNDOCKED))
3628   {
3629   case DOCKINFO_DOCKED:
3630   case DOCKINFO_UNDOCKED:
3631     return hwInfo.dwDockInfo & (DOCKINFO_DOCKED|DOCKINFO_UNDOCKED);
3632   default:
3633     return 0;
3634   }
3635 }
3636
3637 /*************************************************************************
3638  *      @       [SHLWAPI.418]
3639  *
3640  * Function seems to do FreeLibrary plus other things.
3641  *
3642  * FIXME native shows the following calls:
3643  *   RtlEnterCriticalSection
3644  *   LocalFree
3645  *   GetProcAddress(Comctl32??, 150L)
3646  *   DPA_DeletePtr
3647  *   RtlLeaveCriticalSection
3648  *  followed by the FreeLibrary.
3649  *  The above code may be related to .377 above.
3650  */
3651 BOOL WINAPI MLFreeLibrary(HMODULE hModule)
3652 {
3653         FIXME("(%p) semi-stub\n", hModule);
3654         return FreeLibrary(hModule);
3655 }
3656
3657 /*************************************************************************
3658  *      @       [SHLWAPI.419]
3659  */
3660 BOOL WINAPI SHFlushSFCacheWrap(void) {
3661   FIXME(": stub\n");
3662   return TRUE;
3663 }
3664
3665 /*************************************************************************
3666  *      @      [SHLWAPI.429]
3667  * FIXME I have no idea what this function does or what its arguments are.
3668  */
3669 BOOL WINAPI MLIsMLHInstance(HINSTANCE hInst)
3670 {
3671        FIXME("(%p) stub\n", hInst);
3672        return FALSE;
3673 }
3674
3675
3676 /*************************************************************************
3677  *      @       [SHLWAPI.430]
3678  */
3679 DWORD WINAPI MLSetMLHInstance(HINSTANCE hInst, HANDLE hHeap)
3680 {
3681         FIXME("(%p,%p) stub\n", hInst, hHeap);
3682         return E_FAIL;   /* This is what is used if shlwapi not loaded */
3683 }
3684
3685 /*************************************************************************
3686  *      @       [SHLWAPI.431]
3687  */
3688 DWORD WINAPI MLClearMLHInstance(DWORD x)
3689 {
3690         FIXME("(0x%08x)stub\n", x);
3691         return 0xabba1247;
3692 }
3693
3694 /*************************************************************************
3695  * @ [SHLWAPI.432]
3696  *
3697  * See SHSendMessageBroadcastW
3698  *
3699  */
3700 DWORD WINAPI SHSendMessageBroadcastA(UINT uMsg, WPARAM wParam, LPARAM lParam)
3701 {
3702     return SendMessageTimeoutA(HWND_BROADCAST, uMsg, wParam, lParam,
3703                                SMTO_ABORTIFHUNG, 2000, NULL);
3704 }
3705
3706 /*************************************************************************
3707  * @ [SHLWAPI.433]
3708  *
3709  * A wrapper for sending Broadcast Messages to all top level Windows
3710  *
3711  */
3712 DWORD WINAPI SHSendMessageBroadcastW(UINT uMsg, WPARAM wParam, LPARAM lParam)
3713 {
3714     return SendMessageTimeoutW(HWND_BROADCAST, uMsg, wParam, lParam,
3715                                SMTO_ABORTIFHUNG, 2000, NULL);
3716 }
3717
3718 /*************************************************************************
3719  *      @       [SHLWAPI.436]
3720  *
3721  * Convert an Unicode string CLSID into a CLSID.
3722  *
3723  * PARAMS
3724  *  idstr      [I]   string containing a CLSID in text form
3725  *  id         [O]   CLSID extracted from the string
3726  *
3727  * RETURNS
3728  *  S_OK on success or E_INVALIDARG on failure
3729  */
3730 HRESULT WINAPI CLSIDFromStringWrap(LPCWSTR idstr, CLSID *id)
3731 {
3732     return CLSIDFromString((LPOLESTR)idstr, id);
3733 }
3734
3735 /*************************************************************************
3736  *      @       [SHLWAPI.437]
3737  *
3738  * Determine if the OS supports a given feature.
3739  *
3740  * PARAMS
3741  *  dwFeature [I] Feature requested (undocumented)
3742  *
3743  * RETURNS
3744  *  TRUE  If the feature is available.
3745  *  FALSE If the feature is not available.
3746  */
3747 BOOL WINAPI IsOS(DWORD feature)
3748 {
3749     OSVERSIONINFOA osvi;
3750     DWORD platform, majorv, minorv;
3751
3752     osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
3753     if(!GetVersionExA(&osvi))  {
3754         ERR("GetVersionEx failed\n");
3755         return FALSE;
3756     }
3757
3758     majorv = osvi.dwMajorVersion;
3759     minorv = osvi.dwMinorVersion;
3760     platform = osvi.dwPlatformId;
3761
3762 #define ISOS_RETURN(x) \
3763     TRACE("(0x%x) ret=%d\n",feature,(x)); \
3764     return (x);
3765
3766     switch(feature)  {
3767     case OS_WIN32SORGREATER:
3768         ISOS_RETURN(platform == VER_PLATFORM_WIN32s
3769                  || platform == VER_PLATFORM_WIN32_WINDOWS)
3770     case OS_NT:
3771         ISOS_RETURN(platform == VER_PLATFORM_WIN32_NT)
3772     case OS_WIN95ORGREATER:
3773         ISOS_RETURN(platform == VER_PLATFORM_WIN32_WINDOWS)
3774     case OS_NT4ORGREATER:
3775         ISOS_RETURN(platform == VER_PLATFORM_WIN32_NT && majorv >= 4)
3776     case OS_WIN2000ORGREATER_ALT:
3777     case OS_WIN2000ORGREATER:
3778         ISOS_RETURN(platform == VER_PLATFORM_WIN32_NT && majorv >= 5)
3779     case OS_WIN98ORGREATER:
3780         ISOS_RETURN(platform == VER_PLATFORM_WIN32_WINDOWS && minorv >= 10)
3781     case OS_WIN98_GOLD:
3782         ISOS_RETURN(platform == VER_PLATFORM_WIN32_WINDOWS && minorv == 10)
3783     case OS_WIN2000PRO:
3784         ISOS_RETURN(platform == VER_PLATFORM_WIN32_NT && majorv >= 5)
3785     case OS_WIN2000SERVER:
3786         ISOS_RETURN(platform == VER_PLATFORM_WIN32_NT && (minorv == 0 || minorv == 1))
3787     case OS_WIN2000ADVSERVER:
3788         ISOS_RETURN(platform == VER_PLATFORM_WIN32_NT && (minorv == 0 || minorv == 1))
3789     case OS_WIN2000DATACENTER:
3790         ISOS_RETURN(platform == VER_PLATFORM_WIN32_NT && (minorv == 0 || minorv == 1))
3791     case OS_WIN2000TERMINAL:
3792         ISOS_RETURN(platform == VER_PLATFORM_WIN32_NT && (minorv == 0 || minorv == 1))
3793     case OS_EMBEDDED:
3794         FIXME("(OS_EMBEDDED) What should we return here?\n");
3795         return FALSE;
3796     case OS_TERMINALCLIENT:
3797         FIXME("(OS_TERMINALCLIENT) What should we return here?\n");
3798         return FALSE;
3799     case OS_TERMINALREMOTEADMIN:
3800         FIXME("(OS_TERMINALREMOTEADMIN) What should we return here?\n");
3801         return FALSE;
3802     case OS_WIN95_GOLD:
3803         ISOS_RETURN(platform == VER_PLATFORM_WIN32_WINDOWS && minorv == 0)
3804     case OS_MEORGREATER:
3805         ISOS_RETURN(platform == VER_PLATFORM_WIN32_WINDOWS && minorv >= 90)
3806     case OS_XPORGREATER:
3807         ISOS_RETURN(platform == VER_PLATFORM_WIN32_NT && majorv >= 5 && minorv >= 1)
3808     case OS_HOME:
3809         ISOS_RETURN(platform == VER_PLATFORM_WIN32_NT && majorv >= 5 && minorv >= 1)
3810     case OS_PROFESSIONAL:
3811         ISOS_RETURN(platform == VER_PLATFORM_WIN32_NT)
3812     case OS_DATACENTER:
3813         ISOS_RETURN(platform == VER_PLATFORM_WIN32_NT)
3814     case OS_ADVSERVER:
3815         ISOS_RETURN(platform == VER_PLATFORM_WIN32_NT && majorv >= 5)
3816     case OS_SERVER:
3817         ISOS_RETURN(platform == VER_PLATFORM_WIN32_NT)
3818     case OS_TERMINALSERVER:
3819         ISOS_RETURN(platform == VER_PLATFORM_WIN32_NT)
3820     case OS_PERSONALTERMINALSERVER:
3821         ISOS_RETURN(platform == VER_PLATFORM_WIN32_NT && minorv >= 1 && majorv >= 5)
3822     case OS_FASTUSERSWITCHING:
3823         FIXME("(OS_FASTUSERSWITCHING) What should we return here?\n");
3824         return TRUE;
3825     case OS_WELCOMELOGONUI:
3826         FIXME("(OS_WELCOMELOGONUI) What should we return here?\n");
3827         return FALSE;
3828     case OS_DOMAINMEMBER:
3829         FIXME("(OS_DOMAINMEMBER) What should we return here?\n");
3830         return TRUE;
3831     case OS_ANYSERVER:
3832         ISOS_RETURN(platform == VER_PLATFORM_WIN32_NT)
3833     case OS_WOW6432:
3834         FIXME("(OS_WOW6432) Should we check this?\n");
3835         return FALSE;
3836     case OS_WEBSERVER:
3837         ISOS_RETURN(platform == VER_PLATFORM_WIN32_NT)
3838     case OS_SMALLBUSINESSSERVER:
3839         ISOS_RETURN(platform == VER_PLATFORM_WIN32_NT)
3840     case OS_TABLETPC:
3841         FIXME("(OS_TABLEPC) What should we return here?\n");
3842         return FALSE;
3843     case OS_SERVERADMINUI:
3844         FIXME("(OS_SERVERADMINUI) What should we return here?\n");
3845         return FALSE;
3846     case OS_MEDIACENTER:
3847         FIXME("(OS_MEDIACENTER) What should we return here?\n");
3848         return FALSE;
3849     case OS_APPLIANCE:
3850         FIXME("(OS_APPLIANCE) What should we return here?\n");
3851         return FALSE;
3852     }
3853
3854 #undef ISOS_RETURN
3855
3856     WARN("(0x%x) unknown parameter\n",feature);
3857
3858     return FALSE;
3859 }
3860
3861 /*************************************************************************
3862  * @  [SHLWAPI.439]
3863  */
3864 HRESULT WINAPI SHLoadRegUIStringW(HKEY hkey, LPCWSTR value, LPWSTR buf, DWORD size)
3865 {
3866     DWORD type, sz = size;
3867
3868     if(RegQueryValueExW(hkey, value, NULL, &type, (LPBYTE)buf, &sz) != ERROR_SUCCESS)
3869         return E_FAIL;
3870
3871     return SHLoadIndirectString(buf, buf, size, NULL);
3872 }
3873
3874 /*************************************************************************
3875  * @  [SHLWAPI.478]
3876  *
3877  * Call IInputObject_TranslateAcceleratorIO() on an object.
3878  *
3879  * PARAMS
3880  *  lpUnknown [I] Object supporting the IInputObject interface.
3881  *  lpMsg     [I] Key message to be processed.
3882  *
3883  * RETURNS
3884  *  Success: S_OK.
3885  *  Failure: An HRESULT error code, or E_INVALIDARG if lpUnknown is NULL.
3886  */
3887 HRESULT WINAPI IUnknown_TranslateAcceleratorIO(IUnknown *lpUnknown, LPMSG lpMsg)
3888 {
3889   IInputObject* lpInput = NULL;
3890   HRESULT hRet = E_INVALIDARG;
3891
3892   TRACE("(%p,%p)\n", lpUnknown, lpMsg);
3893   if (lpUnknown)
3894   {
3895     hRet = IUnknown_QueryInterface(lpUnknown, &IID_IInputObject,
3896                                    (void**)&lpInput);
3897     if (SUCCEEDED(hRet) && lpInput)
3898     {
3899       hRet = IInputObject_TranslateAcceleratorIO(lpInput, lpMsg);
3900       IInputObject_Release(lpInput);
3901     }
3902   }
3903   return hRet;
3904 }
3905
3906 /*************************************************************************
3907  * @  [SHLWAPI.481]
3908  *
3909  * Call IInputObject_HasFocusIO() on an object.
3910  *
3911  * PARAMS
3912  *  lpUnknown [I] Object supporting the IInputObject interface.
3913  *
3914  * RETURNS
3915  *  Success: S_OK, if lpUnknown is an IInputObject object and has the focus,
3916  *           or S_FALSE otherwise.
3917  *  Failure: An HRESULT error code, or E_INVALIDARG if lpUnknown is NULL.
3918  */
3919 HRESULT WINAPI IUnknown_HasFocusIO(IUnknown *lpUnknown)
3920 {
3921   IInputObject* lpInput = NULL;
3922   HRESULT hRet = E_INVALIDARG;
3923
3924   TRACE("(%p)\n", lpUnknown);
3925   if (lpUnknown)
3926   {
3927     hRet = IUnknown_QueryInterface(lpUnknown, &IID_IInputObject,
3928                                    (void**)&lpInput);
3929     if (SUCCEEDED(hRet) && lpInput)
3930     {
3931       hRet = IInputObject_HasFocusIO(lpInput);
3932       IInputObject_Release(lpInput);
3933     }
3934   }
3935   return hRet;
3936 }
3937
3938 /*************************************************************************
3939  *      ColorRGBToHLS   [SHLWAPI.@]
3940  *
3941  * Convert an rgb COLORREF into the hls color space.
3942  *
3943  * PARAMS
3944  *  cRGB         [I] Source rgb value
3945  *  pwHue        [O] Destination for converted hue
3946  *  pwLuminance  [O] Destination for converted luminance
3947  *  pwSaturation [O] Destination for converted saturation
3948  *
3949  * RETURNS
3950  *  Nothing. pwHue, pwLuminance and pwSaturation are set to the converted
3951  *  values.
3952  *
3953  * NOTES
3954  *  Output HLS values are constrained to the range (0..240).
3955  *  For Achromatic conversions, Hue is set to 160.
3956  */
3957 VOID WINAPI ColorRGBToHLS(COLORREF cRGB, LPWORD pwHue,
3958                           LPWORD pwLuminance, LPWORD pwSaturation)
3959 {
3960   int wR, wG, wB, wMax, wMin, wHue, wLuminosity, wSaturation;
3961
3962   TRACE("(%08x,%p,%p,%p)\n", cRGB, pwHue, pwLuminance, pwSaturation);
3963
3964   wR = GetRValue(cRGB);
3965   wG = GetGValue(cRGB);
3966   wB = GetBValue(cRGB);
3967
3968   wMax = max(wR, max(wG, wB));
3969   wMin = min(wR, min(wG, wB));
3970
3971   /* Luminosity */
3972   wLuminosity = ((wMax + wMin) * 240 + 255) / 510;
3973
3974   if (wMax == wMin)
3975   {
3976     /* Achromatic case */
3977     wSaturation = 0;
3978     /* Hue is now unrepresentable, but this is what native returns... */
3979     wHue = 160;
3980   }
3981   else
3982   {
3983     /* Chromatic case */
3984     int wDelta = wMax - wMin, wRNorm, wGNorm, wBNorm;
3985
3986     /* Saturation */
3987     if (wLuminosity <= 120)
3988       wSaturation = ((wMax + wMin)/2 + wDelta * 240) / (wMax + wMin);
3989     else
3990       wSaturation = ((510 - wMax - wMin)/2 + wDelta * 240) / (510 - wMax - wMin);
3991
3992     /* Hue */
3993     wRNorm = (wDelta/2 + wMax * 40 - wR * 40) / wDelta;
3994     wGNorm = (wDelta/2 + wMax * 40 - wG * 40) / wDelta;
3995     wBNorm = (wDelta/2 + wMax * 40 - wB * 40) / wDelta;
3996
3997     if (wR == wMax)
3998       wHue = wBNorm - wGNorm;
3999     else if (wG == wMax)
4000       wHue = 80 + wRNorm - wBNorm;
4001     else
4002       wHue = 160 + wGNorm - wRNorm;
4003     if (wHue < 0)
4004       wHue += 240;
4005     else if (wHue > 240)
4006       wHue -= 240;
4007   }
4008   if (pwHue)
4009     *pwHue = wHue;
4010   if (pwLuminance)
4011     *pwLuminance = wLuminosity;
4012   if (pwSaturation)
4013     *pwSaturation = wSaturation;
4014 }
4015
4016 /*************************************************************************
4017  *      SHCreateShellPalette    [SHLWAPI.@]
4018  */
4019 HPALETTE WINAPI SHCreateShellPalette(HDC hdc)
4020 {
4021         FIXME("stub\n");
4022         return CreateHalftonePalette(hdc);
4023 }
4024
4025 /*************************************************************************
4026  *      SHGetInverseCMAP (SHLWAPI.@)
4027  *
4028  * Get an inverse color map table.
4029  *
4030  * PARAMS
4031  *  lpCmap  [O] Destination for color map
4032  *  dwSize  [I] Size of memory pointed to by lpCmap
4033  *
4034  * RETURNS
4035  *  Success: S_OK.
4036  *  Failure: E_POINTER,    If lpCmap is invalid.
4037  *           E_INVALIDARG, If dwFlags is invalid
4038  *           E_OUTOFMEMORY, If there is no memory available
4039  *
4040  * NOTES
4041  *  dwSize may only be CMAP_PTR_SIZE (4) or CMAP_SIZE (8192).
4042  *  If dwSize = CMAP_PTR_SIZE, *lpCmap is set to the address of this DLL's
4043  *  internal CMap.
4044  *  If dwSize = CMAP_SIZE, lpCmap is filled with a copy of the data from
4045  *  this DLL's internal CMap.
4046  */
4047 HRESULT WINAPI SHGetInverseCMAP(LPDWORD dest, DWORD dwSize)
4048 {
4049     if (dwSize == 4) {
4050         FIXME(" - returning bogus address for SHGetInverseCMAP\n");
4051         *dest = (DWORD)0xabba1249;
4052         return 0;
4053     }
4054     FIXME("(%p, %#x) stub\n", dest, dwSize);
4055     return 0;
4056 }
4057
4058 /*************************************************************************
4059  *      SHIsLowMemoryMachine    [SHLWAPI.@]
4060  *
4061  * Determine if the current computer has low memory.
4062  *
4063  * PARAMS
4064  *  x [I] FIXME
4065  *
4066  * RETURNS
4067  *  TRUE if the users machine has 16 Megabytes of memory or less,
4068  *  FALSE otherwise.
4069  */
4070 BOOL WINAPI SHIsLowMemoryMachine (DWORD x)
4071 {
4072   FIXME("(0x%08x) stub\n", x);
4073   return FALSE;
4074 }
4075
4076 /*************************************************************************
4077  *      GetMenuPosFromID        [SHLWAPI.@]
4078  *
4079  * Return the position of a menu item from its Id.
4080  *
4081  * PARAMS
4082  *   hMenu [I] Menu containing the item
4083  *   wID   [I] Id of the menu item
4084  *
4085  * RETURNS
4086  *  Success: The index of the menu item in hMenu.
4087  *  Failure: -1, If the item is not found.
4088  */
4089 INT WINAPI GetMenuPosFromID(HMENU hMenu, UINT wID)
4090 {
4091  MENUITEMINFOW mi;
4092  INT nCount = GetMenuItemCount(hMenu), nIter = 0;
4093
4094  while (nIter < nCount)
4095  {
4096    mi.cbSize = sizeof(mi);
4097    mi.fMask = MIIM_ID;
4098    if (GetMenuItemInfoW(hMenu, nIter, TRUE, &mi) && mi.wID == wID)
4099      return nIter;
4100    nIter++;
4101  }
4102  return -1;
4103 }
4104
4105 /*************************************************************************
4106  *      @       [SHLWAPI.179]
4107  *
4108  * Same as SHLWAPI.GetMenuPosFromID
4109  */
4110 DWORD WINAPI SHMenuIndexFromID(HMENU hMenu, UINT uID)
4111 {
4112     return GetMenuPosFromID(hMenu, uID);
4113 }
4114
4115
4116 /*************************************************************************
4117  *      @       [SHLWAPI.448]
4118  */
4119 VOID WINAPI FixSlashesAndColonW(LPWSTR lpwstr)
4120 {
4121     while (*lpwstr)
4122     {
4123         if (*lpwstr == '/')
4124             *lpwstr = '\\';
4125         lpwstr++;
4126     }
4127 }
4128
4129
4130 /*************************************************************************
4131  *      @       [SHLWAPI.461]
4132  */
4133 DWORD WINAPI SHGetAppCompatFlags(DWORD dwUnknown)
4134 {
4135   FIXME("(0x%08x) stub\n", dwUnknown);
4136   return 0;
4137 }
4138
4139
4140 /*************************************************************************
4141  *      @       [SHLWAPI.549]
4142  */
4143 HRESULT WINAPI SHCoCreateInstanceAC(REFCLSID rclsid, LPUNKNOWN pUnkOuter,
4144                                     DWORD dwClsContext, REFIID iid, LPVOID *ppv)
4145 {
4146     return CoCreateInstance(rclsid, pUnkOuter, dwClsContext, iid, ppv);
4147 }
4148
4149 /*************************************************************************
4150  * SHSkipJunction       [SHLWAPI.@]
4151  *
4152  * Determine if a bind context can be bound to an object
4153  *
4154  * PARAMS
4155  *  pbc    [I] Bind context to check
4156  *  pclsid [I] CLSID of object to be bound to
4157  *
4158  * RETURNS
4159  *  TRUE: If it is safe to bind
4160  *  FALSE: If pbc is invalid or binding would not be safe
4161  *
4162  */
4163 BOOL WINAPI SHSkipJunction(IBindCtx *pbc, const CLSID *pclsid)
4164 {
4165   static WCHAR szSkipBinding[] = { 'S','k','i','p',' ',
4166     'B','i','n','d','i','n','g',' ','C','L','S','I','D','\0' };
4167   BOOL bRet = FALSE;
4168
4169   if (pbc)
4170   {
4171     IUnknown* lpUnk;
4172
4173     if (SUCCEEDED(IBindCtx_GetObjectParam(pbc, (LPOLESTR)szSkipBinding, &lpUnk)))
4174     {
4175       CLSID clsid;
4176
4177       if (SUCCEEDED(IUnknown_GetClassID(lpUnk, &clsid)) &&
4178           IsEqualGUID(pclsid, &clsid))
4179         bRet = TRUE;
4180
4181       IUnknown_Release(lpUnk);
4182     }
4183   }
4184   return bRet;
4185 }
4186
4187 /***********************************************************************
4188  *              SHGetShellKey (SHLWAPI.@)
4189  */
4190 DWORD WINAPI SHGetShellKey(DWORD a, DWORD b, DWORD c)
4191 {
4192     FIXME("(%x, %x, %x): stub\n", a, b, c);
4193     return 0x50;
4194 }
4195
4196 /***********************************************************************
4197  *              SHQueueUserWorkItem (SHLWAPI.@)
4198  */
4199 BOOL WINAPI SHQueueUserWorkItem(LPTHREAD_START_ROUTINE pfnCallback, 
4200         LPVOID pContext, LONG lPriority, DWORD_PTR dwTag,
4201         DWORD_PTR *pdwId, LPCSTR pszModule, DWORD dwFlags)
4202 {
4203     TRACE("(%p, %p, %d, %lx, %p, %s, %08x)\n", pfnCallback, pContext,
4204           lPriority, dwTag, pdwId, debugstr_a(pszModule), dwFlags);
4205
4206     if(lPriority || dwTag || pdwId || pszModule || dwFlags)
4207         FIXME("Unsupported arguments\n");
4208
4209     return QueueUserWorkItem(pfnCallback, pContext, 0);
4210 }
4211
4212 /***********************************************************************
4213  *              SHSetTimerQueueTimer (SHLWAPI.263)
4214  */
4215 HANDLE WINAPI SHSetTimerQueueTimer(HANDLE hQueue,
4216         WAITORTIMERCALLBACK pfnCallback, LPVOID pContext, DWORD dwDueTime,
4217         DWORD dwPeriod, LPCSTR lpszLibrary, DWORD dwFlags)
4218 {
4219     HANDLE hNewTimer;
4220
4221     /* SHSetTimerQueueTimer flags -> CreateTimerQueueTimer flags */
4222     if (dwFlags & TPS_LONGEXECTIME) {
4223         dwFlags &= ~TPS_LONGEXECTIME;
4224         dwFlags |= WT_EXECUTELONGFUNCTION;
4225     }
4226     if (dwFlags & TPS_EXECUTEIO) {
4227         dwFlags &= ~TPS_EXECUTEIO;
4228         dwFlags |= WT_EXECUTEINIOTHREAD;
4229     }
4230
4231     if (!CreateTimerQueueTimer(&hNewTimer, hQueue, pfnCallback, pContext,
4232                                dwDueTime, dwPeriod, dwFlags))
4233         return NULL;
4234
4235     return hNewTimer;
4236 }
4237
4238 /***********************************************************************
4239  *              IUnknown_OnFocusChangeIS (SHLWAPI.@)
4240  */
4241 HRESULT WINAPI IUnknown_OnFocusChangeIS(LPUNKNOWN lpUnknown, LPUNKNOWN pFocusObject, BOOL bFocus)
4242 {
4243     IInputObjectSite *pIOS = NULL;
4244     HRESULT hRet = E_INVALIDARG;
4245
4246     TRACE("(%p, %p, %s)\n", lpUnknown, pFocusObject, bFocus ? "TRUE" : "FALSE");
4247
4248     if (lpUnknown)
4249     {
4250         hRet = IUnknown_QueryInterface(lpUnknown, &IID_IInputObjectSite,
4251                                        (void **)&pIOS);
4252         if (SUCCEEDED(hRet) && pIOS)
4253         {
4254             hRet = IInputObjectSite_OnFocusChangeIS(pIOS, pFocusObject, bFocus);
4255             IInputObjectSite_Release(pIOS);
4256         }
4257     }
4258     return hRet;
4259 }
4260
4261 /***********************************************************************
4262  *              SHGetValueW (SHLWAPI.@)
4263  */
4264 HRESULT WINAPI SKGetValueW(DWORD a, LPWSTR b, LPWSTR c, DWORD d, DWORD e, DWORD f)
4265 {
4266     FIXME("(%x, %s, %s, %x, %x, %x): stub\n", a, debugstr_w(b), debugstr_w(c), d, e, f);
4267     return E_FAIL;
4268 }
4269
4270 typedef HRESULT (WINAPI *DllGetVersion_func)(DLLVERSIONINFO *);
4271
4272 /***********************************************************************
4273  *              GetUIVersion (SHLWAPI.452)
4274  */
4275 DWORD WINAPI GetUIVersion(void)
4276 {
4277     static DWORD version;
4278
4279     if (!version)
4280     {
4281         DllGetVersion_func pDllGetVersion;
4282         HMODULE dll = LoadLibraryA("shell32.dll");
4283         if (!dll) return 0;
4284
4285         pDllGetVersion = (DllGetVersion_func)GetProcAddress(dll, "DllGetVersion");
4286         if (pDllGetVersion)
4287         {
4288             DLLVERSIONINFO dvi;
4289             dvi.cbSize = sizeof(DLLVERSIONINFO);
4290             if (pDllGetVersion(&dvi) == S_OK) version = dvi.dwMajorVersion;
4291         }
4292         FreeLibrary( dll );
4293         if (!version) version = 3;  /* old shell dlls don't have DllGetVersion */
4294     }
4295     return version;
4296 }
4297
4298 /***********************************************************************
4299  *              ShellMessageBoxWrapW [SHLWAPI.388]
4300  *
4301  * See shell32.ShellMessageBoxW
4302  *
4303  * NOTE:
4304  * shlwapi.ShellMessageBoxWrapW is a duplicate of shell32.ShellMessageBoxW
4305  * because we can't forward to it in the .spec file since it's exported by
4306  * ordinal. If you change the implementation here please update the code in
4307  * shell32 as well.
4308  */
4309 INT WINAPIV ShellMessageBoxWrapW(HINSTANCE hInstance, HWND hWnd, LPCWSTR lpText,
4310                                  LPCWSTR lpCaption, UINT uType, ...)
4311 {
4312     WCHAR szText[100], szTitle[100];
4313     LPCWSTR pszText = szText, pszTitle = szTitle;
4314     LPWSTR pszTemp;
4315     va_list args;
4316     int ret;
4317
4318     va_start(args, uType);
4319
4320     TRACE("(%p,%p,%p,%p,%08x)\n", hInstance, hWnd, lpText, lpCaption, uType);
4321
4322     if (IS_INTRESOURCE(lpCaption))
4323         LoadStringW(hInstance, LOWORD(lpCaption), szTitle, sizeof(szTitle)/sizeof(szTitle[0]));
4324     else
4325         pszTitle = lpCaption;
4326
4327     if (IS_INTRESOURCE(lpText))
4328         LoadStringW(hInstance, LOWORD(lpText), szText, sizeof(szText)/sizeof(szText[0]));
4329     else
4330         pszText = lpText;
4331
4332     FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
4333                    pszText, 0, 0, (LPWSTR)&pszTemp, 0, &args);
4334
4335     va_end(args);
4336
4337     ret = MessageBoxW(hWnd, pszTemp, pszTitle, uType);
4338     LocalFree((HLOCAL)pszTemp);
4339     return ret;
4340 }
4341
4342 HRESULT WINAPI IUnknown_QueryServiceExec(IUnknown *unk, REFIID service, REFIID clsid,
4343                                          DWORD x1, DWORD x2, DWORD x3, void **ppvOut)
4344 {
4345     FIXME("%p %s %s %08x %08x %08x %p\n", unk,
4346           debugstr_guid(service), debugstr_guid(clsid), x1, x2, x3, ppvOut);
4347     return E_NOTIMPL;
4348 }
4349
4350 HRESULT WINAPI IUnknown_ProfferService(IUnknown *unk, void *x0, void *x1, void *x2)
4351 {
4352     FIXME("%p %p %p %p\n", unk, x0, x1, x2);
4353     return E_NOTIMPL;
4354 }
4355
4356 /***********************************************************************
4357  *              ZoneComputePaneSize [SHLWAPI.382]
4358  */
4359 UINT WINAPI ZoneComputePaneSize(HWND hwnd)
4360 {
4361     FIXME("\n");
4362     return 0x95;
4363 }
4364
4365 /***********************************************************************
4366  *              SHChangeNotifyWrap [SHLWAPI.394]
4367  */
4368 void WINAPI SHChangeNotifyWrap(LONG wEventId, UINT uFlags, LPCVOID dwItem1, LPCVOID dwItem2)
4369 {
4370     SHChangeNotify(wEventId, uFlags, dwItem1, dwItem2);
4371 }
4372
4373 typedef struct SHELL_USER_SID {   /* according to MSDN this should be in shlobj.h... */
4374     SID_IDENTIFIER_AUTHORITY sidAuthority;
4375     DWORD                    dwUserGroupID;
4376     DWORD                    dwUserID;
4377 } SHELL_USER_SID, *PSHELL_USER_SID;
4378
4379 typedef struct SHELL_USER_PERMISSION { /* ...and this should be in shlwapi.h */
4380     SHELL_USER_SID susID;
4381     DWORD          dwAccessType;
4382     BOOL           fInherit;
4383     DWORD          dwAccessMask;
4384     DWORD          dwInheritMask;
4385     DWORD          dwInheritAccessMask;
4386 } SHELL_USER_PERMISSION, *PSHELL_USER_PERMISSION;
4387
4388 /***********************************************************************
4389  *             GetShellSecurityDescriptor [SHLWAPI.475]
4390  *
4391  * prepares SECURITY_DESCRIPTOR from a set of ACEs
4392  *
4393  * PARAMS
4394  *  apUserPerm [I] array of pointers to SHELL_USER_PERMISSION structures,
4395  *                 each of which describes permissions to apply
4396  *  cUserPerm  [I] number of entries in apUserPerm array
4397  *
4398  * RETURNS
4399  *  success: pointer to SECURITY_DESCRIPTOR
4400  *  failure: NULL
4401  *
4402  * NOTES
4403  *  Call should free returned descriptor with LocalFree
4404  */
4405 PSECURITY_DESCRIPTOR WINAPI GetShellSecurityDescriptor(PSHELL_USER_PERMISSION *apUserPerm, int cUserPerm)
4406 {
4407     PSID *sidlist;
4408     PSID  cur_user = NULL;
4409     BYTE  tuUser[2000];
4410     DWORD acl_size;
4411     int   sid_count, i;
4412     PSECURITY_DESCRIPTOR psd = NULL;
4413
4414     TRACE("%p %d\n", apUserPerm, cUserPerm);
4415
4416     if (apUserPerm == NULL || cUserPerm <= 0)
4417         return NULL;
4418
4419     sidlist = HeapAlloc(GetProcessHeap(), 0, cUserPerm * sizeof(PSID));
4420     if (!sidlist)
4421         return NULL;
4422
4423     acl_size = sizeof(ACL);
4424
4425     for(sid_count = 0; sid_count < cUserPerm; sid_count++)
4426     {
4427         static SHELL_USER_SID null_sid = {{SECURITY_NULL_SID_AUTHORITY}, 0, 0};
4428         PSHELL_USER_PERMISSION perm = apUserPerm[sid_count];
4429         PSHELL_USER_SID sid = &perm->susID;
4430         PSID pSid;
4431         BOOL ret = TRUE;
4432
4433         if (!memcmp((void*)sid, (void*)&null_sid, sizeof(SHELL_USER_SID)))
4434         {  /* current user's SID */ 
4435             if (!cur_user)
4436             {
4437                 HANDLE Token;
4438                 DWORD bufsize = sizeof(tuUser);
4439
4440                 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &Token);
4441                 if (ret)
4442                 {
4443                     ret = GetTokenInformation(Token, TokenUser, (void*)tuUser, bufsize, &bufsize );
4444                     if (ret)
4445                         cur_user = ((PTOKEN_USER)tuUser)->User.Sid;
4446                     CloseHandle(Token);
4447                 }
4448             }
4449             pSid = cur_user;
4450         } else if (sid->dwUserID==0) /* one sub-authority */
4451             ret = AllocateAndInitializeSid(&sid->sidAuthority, 1, sid->dwUserGroupID, 0,
4452                     0, 0, 0, 0, 0, 0, &pSid);
4453         else
4454             ret = AllocateAndInitializeSid(&sid->sidAuthority, 2, sid->dwUserGroupID, sid->dwUserID,
4455                     0, 0, 0, 0, 0, 0, &pSid);
4456         if (!ret)
4457             goto free_sids;
4458
4459         sidlist[sid_count] = pSid;
4460         /* increment acl_size (1 ACE for non-inheritable and 2 ACEs for inheritable records */
4461         acl_size += (sizeof(ACCESS_ALLOWED_ACE)-sizeof(DWORD) + GetLengthSid(pSid)) * (perm->fInherit ? 2 : 1);
4462     }
4463
4464     psd = LocalAlloc(0, sizeof(SECURITY_DESCRIPTOR) + acl_size);
4465
4466     if (psd != NULL)
4467     {
4468         PACL pAcl = (PACL)(((BYTE*)psd)+sizeof(SECURITY_DESCRIPTOR));
4469
4470         if (!InitializeSecurityDescriptor(psd, SECURITY_DESCRIPTOR_REVISION))
4471             goto error;
4472
4473         if (!InitializeAcl(pAcl, acl_size, ACL_REVISION))
4474             goto error;
4475
4476         for(i = 0; i < sid_count; i++)
4477         {
4478             PSHELL_USER_PERMISSION sup = apUserPerm[i];
4479             PSID sid = sidlist[i];
4480
4481             switch(sup->dwAccessType)
4482             {
4483                 case ACCESS_ALLOWED_ACE_TYPE:
4484                     if (!AddAccessAllowedAce(pAcl, ACL_REVISION, sup->dwAccessMask, sid))
4485                         goto error;
4486                     if (sup->fInherit && !AddAccessAllowedAceEx(pAcl, ACL_REVISION, 
4487                                 (BYTE)sup->dwInheritMask, sup->dwInheritAccessMask, sid))
4488                         goto error;
4489                     break;
4490                 case ACCESS_DENIED_ACE_TYPE:
4491                     if (!AddAccessDeniedAce(pAcl, ACL_REVISION, sup->dwAccessMask, sid))
4492                         goto error;
4493                     if (sup->fInherit && !AddAccessDeniedAceEx(pAcl, ACL_REVISION, 
4494                                 (BYTE)sup->dwInheritMask, sup->dwInheritAccessMask, sid))
4495                         goto error;
4496                     break;
4497                 default:
4498                     goto error;
4499             }
4500         }
4501
4502         if (!SetSecurityDescriptorDacl(psd, TRUE, pAcl, FALSE))
4503             goto error;
4504     }
4505     goto free_sids;
4506
4507 error:
4508     LocalFree(psd);
4509     psd = NULL;
4510 free_sids:
4511     for(i = 0; i < sid_count; i++)
4512     {
4513         if (!cur_user || sidlist[i] != cur_user)
4514             FreeSid(sidlist[i]);
4515     }
4516     HeapFree(GetProcessHeap(), 0, sidlist);
4517
4518     return psd;
4519 }