shlwapi: Sign-compare warnings fix.
[wine] / dlls / shlwapi / assoc.c
1 /*
2  * IQueryAssociations object and helper functions
3  *
4  * Copyright 2002 Jon Griffiths
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 #include <stdarg.h>
21 #include <assert.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winnls.h"
26 #include "winreg.h"
27 #include "objbase.h"
28 #include "shlguid.h"
29 #include "shlwapi.h"
30 #include "ver.h"
31 #include "wine/unicode.h"
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(shell);
35
36 /**************************************************************************
37  *  IQueryAssociations {SHLWAPI}
38  *
39  * DESCRIPTION
40  *  This object provides a layer of abstraction over the system registry in
41  *  order to simplify the process of parsing associations between files.
42  *  Associations in this context means the registry entries that link (for
43  *  example) the extension of a file with its description, list of
44  *  applications to open the file with, and actions that can be performed on it
45  *  (the shell displays such information in the context menu of explorer
46  *  when you right-click on a file).
47  *
48  * HELPERS
49  * You can use this object transparently by calling the helper functions
50  * AssocQueryKeyA(), AssocQueryStringA() and AssocQueryStringByKeyA(). These
51  * create an IQueryAssociations object, perform the requested actions
52  * and then dispose of the object. Alternatively, you can create an instance
53  * of the object using AssocCreate() and call the following methods on it:
54  *
55  * METHODS
56  */
57
58 /* Default IQueryAssociations::Init() flags */
59 #define SHLWAPI_DEF_ASSOCF (ASSOCF_INIT_BYEXENAME|ASSOCF_INIT_DEFAULTTOSTAR| \
60                             ASSOCF_INIT_DEFAULTTOFOLDER)
61
62 typedef struct
63 {
64   const IQueryAssociationsVtbl *lpVtbl;
65   LONG ref;
66   HKEY hkeySource;
67   HKEY hkeyProgID;
68 } IQueryAssociationsImpl;
69
70 static const IQueryAssociationsVtbl IQueryAssociations_vtbl;
71
72 /**************************************************************************
73  *  IQueryAssociations_Constructor [internal]
74  *
75  * Construct a new IQueryAssociations object.
76  */
77 static IQueryAssociations* IQueryAssociations_Constructor(void)
78 {
79   IQueryAssociationsImpl* iface;
80
81   iface = HeapAlloc(GetProcessHeap(),0,sizeof(IQueryAssociationsImpl));
82   iface->lpVtbl = &IQueryAssociations_vtbl;
83   iface->ref = 1;
84   iface->hkeySource = NULL;
85   iface->hkeyProgID = NULL;
86
87   TRACE("Returning IQueryAssociations* %p\n", iface);
88   return (IQueryAssociations*)iface;
89 }
90
91 /*************************************************************************
92  * SHLWAPI_ParamAToW
93  *
94  * Internal helper function: Convert ASCII parameter to Unicode.
95  */
96 static BOOL SHLWAPI_ParamAToW(LPCSTR lpszParam, LPWSTR lpszBuff, DWORD dwLen,
97                               LPWSTR* lpszOut)
98 {
99   if (lpszParam)
100   {
101     DWORD dwStrLen = MultiByteToWideChar(CP_ACP, 0, lpszParam, -1, NULL, 0);
102
103     if (dwStrLen < dwLen)
104     {
105       *lpszOut = lpszBuff; /* Use Buffer, it is big enough */
106     }
107     else
108     {
109       /* Create a new buffer big enough for the string */
110       *lpszOut = HeapAlloc(GetProcessHeap(), 0,
111                                    dwStrLen * sizeof(WCHAR));
112       if (!*lpszOut)
113         return FALSE;
114     }
115     MultiByteToWideChar(CP_ACP, 0, lpszParam, -1, *lpszOut, dwStrLen);
116   }
117   else
118     *lpszOut = NULL;
119   return TRUE;
120 }
121
122 /*************************************************************************
123  * AssocCreate  [SHLWAPI.@]
124  *
125  * Create a new IQueryAssociations object.
126  *
127  * PARAMS
128  *  clsid       [I] CLSID of object
129  *  refiid      [I] REFIID of interface
130  *  lpInterface [O] Destination for the created IQueryAssociations object
131  *
132  * RETURNS
133  *  Success: S_OK. lpInterface contains the new object.
134  *  Failure: An HRESULT error code indicating the error.
135  *
136  * NOTES
137  *  refiid must be equal to IID_IQueryAssociations, or this function will fail.
138  */
139 HRESULT WINAPI AssocCreate(CLSID clsid, REFIID refiid, void **lpInterface)
140 {
141   HRESULT hRet;
142   IQueryAssociations* lpAssoc;
143
144   TRACE("(%s,%s,%p)\n", debugstr_guid(&clsid), debugstr_guid(refiid),
145         lpInterface);
146
147   if (!lpInterface)
148     return E_INVALIDARG;
149
150   *(DWORD*)lpInterface = 0;
151
152   if (!IsEqualGUID(&clsid, &IID_IQueryAssociations))
153     return E_NOTIMPL;
154
155   lpAssoc = IQueryAssociations_Constructor();
156
157   if (!lpAssoc)
158     return E_OUTOFMEMORY;
159
160   hRet = IQueryAssociations_QueryInterface(lpAssoc, refiid, lpInterface);
161   IQueryAssociations_Release(lpAssoc);
162   return hRet;
163 }
164
165 /*************************************************************************
166  * AssocQueryKeyW  [SHLWAPI.@]
167  *
168  * See AssocQueryKeyA.
169  */
170 HRESULT WINAPI AssocQueryKeyW(ASSOCF cfFlags, ASSOCKEY assockey, LPCWSTR pszAssoc,
171                               LPCWSTR pszExtra, HKEY *phkeyOut)
172 {
173   HRESULT hRet;
174   IQueryAssociations* lpAssoc;
175
176   TRACE("(0x%8x,0x%8x,%s,%s,%p)\n", cfFlags, assockey, debugstr_w(pszAssoc),
177         debugstr_w(pszExtra), phkeyOut);
178
179   lpAssoc = IQueryAssociations_Constructor();
180
181   if (!lpAssoc)
182     return E_OUTOFMEMORY;
183
184   cfFlags &= SHLWAPI_DEF_ASSOCF;
185   hRet = IQueryAssociations_Init(lpAssoc, cfFlags, pszAssoc, NULL, NULL);
186
187   if (SUCCEEDED(hRet))
188     hRet = IQueryAssociations_GetKey(lpAssoc, cfFlags, assockey, pszExtra, phkeyOut);
189
190   IQueryAssociations_Release(lpAssoc);
191   return hRet;
192 }
193
194 /*************************************************************************
195  * AssocQueryKeyA  [SHLWAPI.@]
196  *
197  * Get a file association key from the registry.
198  *
199  * PARAMS
200  *  cfFlags  [I] ASSOCF_ flags from "shlwapi.h"
201  *  assockey [I] Type of key to get
202  *  pszAssoc [I] Key name to search below
203  *  pszExtra [I] Extra information about the key location
204  *  phkeyOut [O] Destination for the association key
205  *
206  * RETURNS
207  *  Success: S_OK. phkeyOut contains the key.
208  *  Failure: An HRESULT error code indicating the error.
209  */
210 HRESULT WINAPI AssocQueryKeyA(ASSOCF cfFlags, ASSOCKEY assockey, LPCSTR pszAssoc,
211                               LPCSTR pszExtra, HKEY *phkeyOut)
212 {
213   WCHAR szAssocW[MAX_PATH], *lpszAssocW = NULL;
214   WCHAR szExtraW[MAX_PATH], *lpszExtraW = NULL;
215   HRESULT hRet = E_OUTOFMEMORY;
216
217   TRACE("(0x%8x,0x%8x,%s,%s,%p)\n", cfFlags, assockey, debugstr_a(pszAssoc),
218         debugstr_a(pszExtra), phkeyOut);
219
220   if (SHLWAPI_ParamAToW(pszAssoc, szAssocW, MAX_PATH, &lpszAssocW) &&
221       SHLWAPI_ParamAToW(pszExtra, szExtraW, MAX_PATH, &lpszExtraW))
222   {
223     hRet = AssocQueryKeyW(cfFlags, assockey, lpszAssocW, lpszExtraW, phkeyOut);
224   }
225
226   if (lpszAssocW != szAssocW)
227     HeapFree(GetProcessHeap(), 0, lpszAssocW);
228
229   if (lpszExtraW != szExtraW)
230     HeapFree(GetProcessHeap(), 0, lpszExtraW);
231
232   return hRet;
233 }
234
235 /*************************************************************************
236  * AssocQueryStringW  [SHLWAPI.@]
237  *
238  * See AssocQueryStringA.
239  */
240 HRESULT WINAPI AssocQueryStringW(ASSOCF cfFlags, ASSOCSTR str, LPCWSTR pszAssoc,
241                                  LPCWSTR pszExtra, LPWSTR pszOut, DWORD *pcchOut)
242 {
243   HRESULT hRet;
244   IQueryAssociations* lpAssoc;
245
246   TRACE("(0x%8x,0x%8x,%s,%s,%p,%p)\n", cfFlags, str, debugstr_w(pszAssoc),
247         debugstr_w(pszExtra), pszOut, pcchOut);
248
249   if (!pcchOut)
250     return E_UNEXPECTED;
251
252   lpAssoc = IQueryAssociations_Constructor();
253
254   if (!lpAssoc)
255     return E_OUTOFMEMORY;
256
257   hRet = IQueryAssociations_Init(lpAssoc, cfFlags & SHLWAPI_DEF_ASSOCF,
258                                  pszAssoc, NULL, NULL);
259
260   if (SUCCEEDED(hRet))
261     hRet = IQueryAssociations_GetString(lpAssoc, cfFlags, str, pszExtra,
262                                         pszOut, pcchOut);
263
264   IQueryAssociations_Release(lpAssoc);
265   return hRet;
266 }
267
268 /*************************************************************************
269  * AssocQueryStringA  [SHLWAPI.@]
270  *
271  * Get a file association string from the registry.
272  *
273  * PARAMS
274  *  cfFlags  [I] ASSOCF_ flags from "shlwapi.h"
275  *  str      [I] Type of string to get (ASSOCSTR enum from "shlwapi.h")
276  *  pszAssoc [I] Key name to search below
277  *  pszExtra [I] Extra information about the string location
278  *  pszOut   [O] Destination for the association string
279  *  pcchOut  [O] Length of pszOut
280  *
281  * RETURNS
282  *  Success: S_OK. pszOut contains the string, pcchOut contains its length.
283  *  Failure: An HRESULT error code indicating the error.
284  */
285 HRESULT WINAPI AssocQueryStringA(ASSOCF cfFlags, ASSOCSTR str, LPCSTR pszAssoc,
286                                  LPCSTR pszExtra, LPSTR pszOut, DWORD *pcchOut)
287 {
288   WCHAR szAssocW[MAX_PATH], *lpszAssocW = NULL;
289   WCHAR szExtraW[MAX_PATH], *lpszExtraW = NULL;
290   HRESULT hRet = E_OUTOFMEMORY;
291
292   TRACE("(0x%8x,0x%8x,%s,%s,%p,%p)\n", cfFlags, str, debugstr_a(pszAssoc),
293         debugstr_a(pszExtra), pszOut, pcchOut);
294
295   if (!pcchOut)
296     hRet = E_UNEXPECTED;
297   else if (SHLWAPI_ParamAToW(pszAssoc, szAssocW, MAX_PATH, &lpszAssocW) &&
298            SHLWAPI_ParamAToW(pszExtra, szExtraW, MAX_PATH, &lpszExtraW))
299   {
300     WCHAR szReturnW[MAX_PATH], *lpszReturnW = szReturnW;
301     DWORD dwLenOut = *pcchOut;
302
303     if (dwLenOut >= MAX_PATH)
304       lpszReturnW = HeapAlloc(GetProcessHeap(), 0,
305                                       (dwLenOut + 1) * sizeof(WCHAR));
306     else
307       dwLenOut = sizeof(szReturnW) / sizeof(szReturnW[0]);
308
309     if (!lpszReturnW)
310       hRet = E_OUTOFMEMORY;
311     else
312     {
313       hRet = AssocQueryStringW(cfFlags, str, lpszAssocW, lpszExtraW,
314                                lpszReturnW, &dwLenOut);
315
316       if (SUCCEEDED(hRet))
317         dwLenOut = WideCharToMultiByte(CP_ACP, 0, lpszReturnW, -1,
318                                        pszOut, *pcchOut, NULL, NULL);
319
320       *pcchOut = dwLenOut;
321       if (lpszReturnW != szReturnW)
322         HeapFree(GetProcessHeap(), 0, lpszReturnW);
323     }
324   }
325
326   if (lpszAssocW != szAssocW)
327     HeapFree(GetProcessHeap(), 0, lpszAssocW);
328   if (lpszExtraW != szExtraW)
329     HeapFree(GetProcessHeap(), 0, lpszExtraW);
330   return hRet;
331 }
332
333 /*************************************************************************
334  * AssocQueryStringByKeyW  [SHLWAPI.@]
335  *
336  * See AssocQueryStringByKeyA.
337  */
338 HRESULT WINAPI AssocQueryStringByKeyW(ASSOCF cfFlags, ASSOCSTR str, HKEY hkAssoc,
339                                       LPCWSTR pszExtra, LPWSTR pszOut,
340                                       DWORD *pcchOut)
341 {
342   HRESULT hRet;
343   IQueryAssociations* lpAssoc;
344
345   TRACE("(0x%8x,0x%8x,%p,%s,%p,%p)\n", cfFlags, str, hkAssoc,
346         debugstr_w(pszExtra), pszOut, pcchOut);
347
348   lpAssoc = IQueryAssociations_Constructor();
349
350   if (!lpAssoc)
351     return E_OUTOFMEMORY;
352
353   cfFlags &= SHLWAPI_DEF_ASSOCF;
354   hRet = IQueryAssociations_Init(lpAssoc, cfFlags, 0, hkAssoc, NULL);
355
356   if (SUCCEEDED(hRet))
357     hRet = IQueryAssociations_GetString(lpAssoc, cfFlags, str, pszExtra,
358                                         pszOut, pcchOut);
359
360   IQueryAssociations_Release(lpAssoc);
361   return hRet;
362 }
363
364 /*************************************************************************
365  * AssocQueryStringByKeyA  [SHLWAPI.@]
366  *
367  * Get a file association string from the registry, given a starting key.
368  *
369  * PARAMS
370  *  cfFlags  [I] ASSOCF_ flags from "shlwapi.h"
371  *  str      [I] Type of string to get
372  *  hkAssoc  [I] Key to search below
373  *  pszExtra [I] Extra information about the string location
374  *  pszOut   [O] Destination for the association string
375  *  pcchOut  [O] Length of pszOut
376  *
377  * RETURNS
378  *  Success: S_OK. pszOut contains the string, pcchOut contains its length.
379  *  Failure: An HRESULT error code indicating the error.
380  */
381 HRESULT WINAPI AssocQueryStringByKeyA(ASSOCF cfFlags, ASSOCSTR str, HKEY hkAssoc,
382                                       LPCSTR pszExtra, LPSTR pszOut,
383                                       DWORD *pcchOut)
384 {
385   WCHAR szExtraW[MAX_PATH], *lpszExtraW = szExtraW;
386   WCHAR szReturnW[MAX_PATH], *lpszReturnW = szReturnW;
387   HRESULT hRet = E_OUTOFMEMORY;
388
389   TRACE("(0x%8x,0x%8x,%p,%s,%p,%p)\n", cfFlags, str, hkAssoc,
390         debugstr_a(pszExtra), pszOut, pcchOut);
391
392   if (!pcchOut)
393     hRet = E_INVALIDARG;
394   else if (SHLWAPI_ParamAToW(pszExtra, szExtraW, MAX_PATH, &lpszExtraW))
395   {
396     DWORD dwLenOut = *pcchOut;
397     if (dwLenOut >= MAX_PATH)
398       lpszReturnW = HeapAlloc(GetProcessHeap(), 0,
399                                       (dwLenOut + 1) * sizeof(WCHAR));
400
401     if (lpszReturnW)
402     {
403       hRet = AssocQueryStringByKeyW(cfFlags, str, hkAssoc, lpszExtraW,
404                                     lpszReturnW, &dwLenOut);
405
406       if (SUCCEEDED(hRet))
407         WideCharToMultiByte(CP_ACP,0,szReturnW,-1,pszOut,dwLenOut,0,0);
408       *pcchOut = dwLenOut;
409
410       if (lpszReturnW != szReturnW)
411         HeapFree(GetProcessHeap(), 0, lpszReturnW);
412     }
413   }
414
415   if (lpszExtraW != szExtraW)
416     HeapFree(GetProcessHeap(), 0, lpszExtraW);
417   return hRet;
418 }
419
420
421 /**************************************************************************
422  *  AssocIsDangerous  (SHLWAPI.@)
423  *  
424  * Determine if a file association is dangerous (potentially malware).
425  *
426  * PARAMS
427  *  lpszAssoc [I] Name of file or file extension to check.
428  *
429  * RETURNS
430  *  TRUE, if lpszAssoc may potentially be malware (executable),
431  *  FALSE, Otherwise.
432  */
433 BOOL WINAPI AssocIsDangerous(LPCWSTR lpszAssoc)
434 {
435     FIXME("%s\n", debugstr_w(lpszAssoc));
436     return FALSE;
437 }
438
439 /**************************************************************************
440  *  IQueryAssociations_QueryInterface {SHLWAPI}
441  *
442  * See IUnknown_QueryInterface.
443  */
444 static HRESULT WINAPI IQueryAssociations_fnQueryInterface(
445   IQueryAssociations* iface,
446   REFIID riid,
447   LPVOID *ppvObj)
448 {
449   IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
450
451   TRACE("(%p,%s,%p)\n",This, debugstr_guid(riid), ppvObj);
452
453   *ppvObj = NULL;
454
455   if (IsEqualIID(riid, &IID_IUnknown) ||
456       IsEqualIID(riid, &IID_IQueryAssociations))
457   {
458     *ppvObj = (IQueryAssociations*)This;
459
460     IQueryAssociations_AddRef((IQueryAssociations*)*ppvObj);
461     TRACE("Returning IQueryAssociations (%p)\n", *ppvObj);
462     return S_OK;
463   }
464   TRACE("Returning E_NOINTERFACE\n");
465   return E_NOINTERFACE;
466 }
467
468 /**************************************************************************
469  *  IQueryAssociations_AddRef {SHLWAPI}
470  *
471  * See IUnknown_AddRef.
472  */
473 static ULONG WINAPI IQueryAssociations_fnAddRef(IQueryAssociations *iface)
474 {
475   IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
476   ULONG refCount = InterlockedIncrement(&This->ref);
477   
478   TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
479
480   return refCount;
481 }
482
483 /**************************************************************************
484  *  IQueryAssociations_Release {SHLWAPI}
485  *
486  * See IUnknown_Release.
487  */
488 static ULONG WINAPI IQueryAssociations_fnRelease(IQueryAssociations *iface)
489 {
490   IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
491   ULONG refCount = InterlockedDecrement(&This->ref);
492
493   TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
494
495   if (!refCount)
496   {
497     TRACE("Destroying IQueryAssociations (%p)\n", This);
498     RegCloseKey(This->hkeySource);
499     RegCloseKey(This->hkeyProgID);
500     HeapFree(GetProcessHeap(), 0, This);
501   }
502   
503   return refCount;
504 }
505
506 /**************************************************************************
507  *  IQueryAssociations_Init {SHLWAPI}
508  *
509  * Initialise an IQueryAssociations object.
510  *
511  * PARAMS
512  *  iface      [I] IQueryAssociations interface to initialise
513  *  cfFlags    [I] ASSOCF_ flags from "shlwapi.h"
514  *  pszAssoc   [I] String for the root key name, or NULL if hkeyProgid is given
515  *  hkeyProgid [I] Handle for the root key, or NULL if pszAssoc is given
516  *  hWnd       [I] Reserved, must be NULL.
517  *
518  * RETURNS
519  *  Success: S_OK. iface is initialised with the parameters given.
520  *  Failure: An HRESULT error code indicating the error.
521  */
522 static HRESULT WINAPI IQueryAssociations_fnInit(
523   IQueryAssociations *iface,
524   ASSOCF cfFlags,
525   LPCWSTR pszAssoc,
526   HKEY hkeyProgid,
527   HWND hWnd)
528 {
529     static const WCHAR szProgID[] = {'P','r','o','g','I','D',0};
530     IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
531     LONG ret;
532
533     TRACE("(%p)->(%d,%s,%p,%p)\n", iface,
534                                     cfFlags,
535                                     debugstr_w(pszAssoc),
536                                     hkeyProgid,
537                                     hWnd);
538     if (hWnd != NULL)
539         FIXME("hwnd != NULL not supported\n");
540     if (cfFlags != 0)
541         FIXME("unsupported flags: %x\n", cfFlags);
542     if (pszAssoc != NULL)
543     {
544         ret = RegOpenKeyExW(HKEY_CLASSES_ROOT,
545                             pszAssoc,
546                             0,
547                             KEY_READ,
548                             &This->hkeySource);
549         if (ret != ERROR_SUCCESS)
550             return E_FAIL;
551         /* if this is not a prog id */
552         if ((*pszAssoc == '.') || (*pszAssoc == '{'))
553         {
554             RegOpenKeyExW(This->hkeySource,
555                           szProgID,
556                           0,
557                           KEY_READ,
558                           &This->hkeyProgID);
559         }
560         else
561             This->hkeyProgID = This->hkeySource;
562         return S_OK;
563     }
564     else if (hkeyProgid != NULL)
565     {
566         This->hkeyProgID = hkeyProgid;
567         return S_OK;
568     }
569     else
570         return E_INVALIDARG;
571 }
572
573 static HRESULT ASSOC_GetValue(HKEY hkey, WCHAR ** pszText)
574 {
575   DWORD len;
576   LONG ret;
577
578   assert(pszText);
579   ret = RegQueryValueExW(hkey, NULL, 0, NULL, NULL, &len);
580   if (ret != ERROR_SUCCESS)
581     return HRESULT_FROM_WIN32(ret);
582   if (!len)
583     return E_FAIL;
584   *pszText = HeapAlloc(GetProcessHeap(), 0, len);
585   if (!*pszText)
586     return E_OUTOFMEMORY;
587   ret = RegQueryValueExW(hkey, NULL, 0, NULL, (LPBYTE)*pszText,
588                          &len);
589   if (ret != ERROR_SUCCESS)
590   {
591     HeapFree(GetProcessHeap(), 0, *pszText);
592     return HRESULT_FROM_WIN32(ret);
593   }
594   return S_OK;
595 }
596
597 static HRESULT ASSOC_GetExecutable(IQueryAssociationsImpl *This,
598                                    LPCWSTR pszExtra, LPWSTR path,
599                                    DWORD pathlen, DWORD *len)
600 {
601   HKEY hkeyCommand;
602   HKEY hkeyFile;
603   HKEY hkeyShell;
604   HKEY hkeyVerb;
605   HRESULT hr;
606   LONG ret;
607   WCHAR * pszCommand;
608   WCHAR * pszEnd;
609   WCHAR * pszExtraFromReg = NULL;
610   WCHAR * pszFileType;
611   WCHAR * pszStart;
612   static const WCHAR commandW[] = { 'c','o','m','m','a','n','d',0 };
613   static const WCHAR shellW[] = { 's','h','e','l','l',0 };
614
615   assert(len);
616
617   hr = ASSOC_GetValue(This->hkeySource, &pszFileType);
618   if (FAILED(hr))
619     return hr;
620   ret = RegOpenKeyExW(HKEY_CLASSES_ROOT, pszFileType, 0, KEY_READ, &hkeyFile);
621   HeapFree(GetProcessHeap(), 0, pszFileType);
622   if (ret != ERROR_SUCCESS)
623     return HRESULT_FROM_WIN32(ret);
624
625   ret = RegOpenKeyExW(hkeyFile, shellW, 0, KEY_READ, &hkeyShell);
626   RegCloseKey(hkeyFile);
627   if (ret != ERROR_SUCCESS)
628     return HRESULT_FROM_WIN32(ret);
629
630   if (!pszExtra)
631   {
632     hr = ASSOC_GetValue(hkeyShell, &pszExtraFromReg);
633     /* if no default action */
634     if (hr == E_FAIL || hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
635     {
636       DWORD rlen;
637       ret = RegQueryInfoKeyW(hkeyShell, 0, 0, 0, 0, &rlen, 0, 0, 0, 0, 0, 0);
638       if (ret != ERROR_SUCCESS)
639       {
640         RegCloseKey(hkeyShell);
641         return HRESULT_FROM_WIN32(ret);
642       }
643       rlen++;
644       pszExtraFromReg = HeapAlloc(GetProcessHeap(), 0, rlen * sizeof(WCHAR));
645       if (!pszExtraFromReg)
646       {
647         RegCloseKey(hkeyShell);
648         return E_OUTOFMEMORY;
649       }
650       ret = RegEnumKeyExW(hkeyShell, 0, pszExtraFromReg, &rlen, 0, NULL, NULL, NULL);
651       if (ret != ERROR_SUCCESS)
652       {
653         RegCloseKey(hkeyShell);
654         return HRESULT_FROM_WIN32(ret);
655       }
656     }
657     else if (FAILED(hr))
658     {
659       RegCloseKey(hkeyShell);
660       return hr;
661     }
662   }
663
664   ret = RegOpenKeyExW(hkeyShell, pszExtra ? pszExtra : pszExtraFromReg, 0,
665                       KEY_READ, &hkeyVerb);
666   HeapFree(GetProcessHeap(), 0, pszExtraFromReg);
667   RegCloseKey(hkeyShell);
668   if (ret != ERROR_SUCCESS)
669     return HRESULT_FROM_WIN32(ret);
670
671   ret = RegOpenKeyExW(hkeyVerb, commandW, 0, KEY_READ, &hkeyCommand);
672   RegCloseKey(hkeyVerb);
673   if (ret != ERROR_SUCCESS)
674     return HRESULT_FROM_WIN32(ret);
675   hr = ASSOC_GetValue(hkeyCommand, &pszCommand);
676   RegCloseKey(hkeyCommand);
677   if (FAILED(hr))
678     return hr;
679
680   /* cleanup pszCommand */
681   if (pszCommand[0] == '"')
682   {
683     pszStart = pszCommand + 1;
684     pszEnd = strchrW(pszStart, '"');
685   }
686   else
687   {
688     pszStart = pszCommand;
689     pszEnd = strchrW(pszStart, ' ');
690   }
691   if (pszEnd)
692     *pszEnd = 0;
693
694   *len = SearchPathW(NULL, pszStart, NULL, pathlen, path, NULL);
695   HeapFree(GetProcessHeap(), 0, pszCommand);
696   if (!*len)
697     return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
698   return S_OK;
699 }
700
701 static HRESULT ASSOC_ReturnData(LPWSTR out, DWORD *outlen, LPCWSTR data,
702                                 DWORD datalen)
703 {
704   assert(outlen);
705
706   if (out)
707   {
708     if (*outlen < datalen)
709     {
710       *outlen = datalen;
711       return E_POINTER;
712     }
713     *outlen = datalen;
714     lstrcpynW(out, data, datalen);
715     return S_OK;
716   }
717   else
718   {
719     *outlen = datalen;
720     return S_FALSE;
721   }
722 }
723
724 /**************************************************************************
725  *  IQueryAssociations_GetString {SHLWAPI}
726  *
727  * Get a file association string from the registry.
728  *
729  * PARAMS
730  *  iface    [I]   IQueryAssociations interface to query
731  *  cfFlags  [I]   ASSOCF_ flags from "shlwapi.h"
732  *  str      [I]   Type of string to get (ASSOCSTR enum from "shlwapi.h")
733  *  pszExtra [I]   Extra information about the string location
734  *  pszOut   [O]   Destination for the association string
735  *  pcchOut  [I/O] Length of pszOut
736  *
737  * RETURNS
738  *  Success: S_OK. pszOut contains the string, pcchOut contains its length.
739  *  Failure: An HRESULT error code indicating the error.
740  */
741 static HRESULT WINAPI IQueryAssociations_fnGetString(
742   IQueryAssociations *iface,
743   ASSOCF cfFlags,
744   ASSOCSTR str,
745   LPCWSTR pszExtra,
746   LPWSTR pszOut,
747   DWORD *pcchOut)
748 {
749   IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
750   const ASSOCF cfUnimplemented = ~(0);
751   DWORD len = 0;
752   HRESULT hr;
753   WCHAR path[MAX_PATH];
754
755   TRACE("(%p,0x%8x,0x%8x,%s,%p,%p)\n", This, cfFlags, str,
756         debugstr_w(pszExtra), pszOut, pcchOut);
757
758   if (cfFlags & cfUnimplemented)
759     FIXME("%08x: unimplemented flags!\n", cfFlags & cfUnimplemented);
760
761   if (!pcchOut)
762     return E_UNEXPECTED;
763
764   switch (str)
765   {
766     case ASSOCSTR_EXECUTABLE:
767     {
768       hr = ASSOC_GetExecutable(This, pszExtra, path, MAX_PATH, &len);
769       if (FAILED(hr))
770         return hr;
771       len++;
772       return ASSOC_ReturnData(pszOut, pcchOut, path, len);
773     }
774
775     case ASSOCSTR_FRIENDLYAPPNAME:
776     {
777       PVOID verinfoW = NULL;
778       DWORD size, retval = 0;
779       UINT flen;
780       WCHAR *bufW;
781       static const WCHAR translationW[] = {
782         '\\','V','a','r','F','i','l','e','I','n','f','o',
783         '\\','T','r','a','n','s','l','a','t','i','o','n',0
784       };
785       static const WCHAR fileDescFmtW[] = {
786         '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
787         '\\','%','0','4','x','%','0','4','x',
788         '\\','F','i','l','e','D','e','s','c','r','i','p','t','i','o','n',0
789       };
790       WCHAR fileDescW[41];
791
792       hr = ASSOC_GetExecutable(This, pszExtra, path, MAX_PATH, &len);
793       if (FAILED(hr))
794         return hr;
795
796       retval = GetFileVersionInfoSizeW(path, &size);
797       if (!retval)
798         goto get_friendly_name_fail;
799       verinfoW = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, retval);
800       if (!verinfoW)
801         return E_OUTOFMEMORY;
802       if (!GetFileVersionInfoW(path, 0, retval, verinfoW))
803         goto get_friendly_name_fail;
804       if (VerQueryValueW(verinfoW, translationW, (LPVOID *)&bufW, &flen))
805       {
806         UINT i;
807         DWORD *langCodeDesc = (DWORD *)bufW;
808         for (i = 0; i < flen / sizeof(DWORD); i++)
809         {
810           sprintfW(fileDescW, fileDescFmtW, LOWORD(langCodeDesc[i]),
811                    HIWORD(langCodeDesc[i]));
812           if (VerQueryValueW(verinfoW, fileDescW, (LPVOID *)&bufW, &flen))
813           {
814             /* Does strlenW(bufW) == 0 mean we use the filename? */
815             len = strlenW(bufW) + 1;
816             TRACE("found FileDescription: %s\n", debugstr_w(bufW));
817             return ASSOC_ReturnData(pszOut, pcchOut, bufW, len);
818           }
819         }
820       }
821 get_friendly_name_fail:
822       PathRemoveExtensionW(path);
823       PathStripPathW(path);
824       TRACE("using filename: %s\n", debugstr_w(path));
825       return ASSOC_ReturnData(pszOut, pcchOut, path, strlenW(path) + 1);
826     }
827
828     default:
829       FIXME("assocstr %d unimplemented!\n", str);
830       return E_NOTIMPL;
831   }
832 }
833
834 /**************************************************************************
835  *  IQueryAssociations_GetKey {SHLWAPI}
836  *
837  * Get a file association key from the registry.
838  *
839  * PARAMS
840  *  iface    [I] IQueryAssociations interface to query
841  *  cfFlags  [I] ASSOCF_ flags from "shlwapi.h"
842  *  assockey [I] Type of key to get (ASSOCKEY enum from "shlwapi.h")
843  *  pszExtra [I] Extra information about the key location
844  *  phkeyOut [O] Destination for the association key
845  *
846  * RETURNS
847  *  Success: S_OK. phkeyOut contains a handle to the key.
848  *  Failure: An HRESULT error code indicating the error.
849  */
850 static HRESULT WINAPI IQueryAssociations_fnGetKey(
851   IQueryAssociations *iface,
852   ASSOCF cfFlags,
853   ASSOCKEY assockey,
854   LPCWSTR pszExtra,
855   HKEY *phkeyOut)
856 {
857   IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
858
859   FIXME("(%p,0x%8x,0x%8x,%s,%p)-stub!\n", This, cfFlags, assockey,
860         debugstr_w(pszExtra), phkeyOut);
861   return E_NOTIMPL;
862 }
863
864 /**************************************************************************
865  *  IQueryAssociations_GetData {SHLWAPI}
866  *
867  * Get the data for a file association key from the registry.
868  *
869  * PARAMS
870  *  iface     [I]   IQueryAssociations interface to query
871  *  cfFlags   [I]   ASSOCF_ flags from "shlwapi.h"
872  *  assocdata [I]   Type of data to get (ASSOCDATA enum from "shlwapi.h")
873  *  pszExtra  [I]   Extra information about the data location
874  *  pvOut     [O]   Destination for the association key
875  *  pcbOut    [I/O] Size of pvOut
876  *
877  * RETURNS
878  *  Success: S_OK. pszOut contains the data, pcbOut contains its length.
879  *  Failure: An HRESULT error code indicating the error.
880  */
881 static HRESULT WINAPI IQueryAssociations_fnGetData(
882   IQueryAssociations *iface,
883   ASSOCF cfFlags,
884   ASSOCDATA assocdata,
885   LPCWSTR pszExtra,
886   LPVOID pvOut,
887   DWORD *pcbOut)
888 {
889   IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
890
891   FIXME("(%p,0x%8x,0x%8x,%s,%p,%p)-stub!\n", This, cfFlags, assocdata,
892         debugstr_w(pszExtra), pvOut, pcbOut);
893   return E_NOTIMPL;
894 }
895
896 /**************************************************************************
897  *  IQueryAssociations_GetEnum {SHLWAPI}
898  *
899  * Not yet implemented in native Win32.
900  *
901  * PARAMS
902  *  iface     [I] IQueryAssociations interface to query
903  *  cfFlags   [I] ASSOCF_ flags from "shlwapi.h"
904  *  assocenum [I] Type of enum to get (ASSOCENUM enum from "shlwapi.h")
905  *  pszExtra  [I] Extra information about the enum location
906  *  riid      [I] REFIID to look for
907  *  ppvOut    [O] Destination for the interface.
908  *
909  * RETURNS
910  *  Success: S_OK.
911  *  Failure: An HRESULT error code indicating the error.
912  *
913  * NOTES
914  *  Presumably this function returns an enumerator object.
915  */
916 static HRESULT WINAPI IQueryAssociations_fnGetEnum(
917   IQueryAssociations *iface,
918   ASSOCF cfFlags,
919   ASSOCENUM assocenum,
920   LPCWSTR pszExtra,
921   REFIID riid,
922   LPVOID *ppvOut)
923 {
924   IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
925
926   FIXME("(%p,0x%8x,0x%8x,%s,%s,%p)-stub!\n", This, cfFlags, assocenum,
927         debugstr_w(pszExtra), debugstr_guid(riid), ppvOut);
928   return E_NOTIMPL;
929 }
930
931 static const IQueryAssociationsVtbl IQueryAssociations_vtbl =
932 {
933   IQueryAssociations_fnQueryInterface,
934   IQueryAssociations_fnAddRef,
935   IQueryAssociations_fnRelease,
936   IQueryAssociations_fnInit,
937   IQueryAssociations_fnGetString,
938   IQueryAssociations_fnGetKey,
939   IQueryAssociations_fnGetData,
940   IQueryAssociations_fnGetEnum
941 };