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