setupapi/tests: Trace error codes when SetupOpenLog fails.
[wine.git] / dlls / shell32 / assoc.c
blob21df4930720c0e5213378fcf8c8863fe5844dc86
1 /*
2 * IQueryAssociations object and helper functions
4 * Copyright 2002 Jon Griffiths
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.
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.
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
20 #include <stdarg.h>
22 #define COBJMACROS
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 "shobjidl.h"
31 #include "shell32_main.h"
32 #include "ver.h"
33 #include "wine/unicode.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(shell);
38 /**************************************************************************
39 * IQueryAssociations
41 * DESCRIPTION
42 * This object provides a layer of abstraction over the system registry in
43 * order to simplify the process of parsing associations between files.
44 * Associations in this context means the registry entries that link (for
45 * example) the extension of a file with its description, list of
46 * applications to open the file with, and actions that can be performed on it
47 * (the shell displays such information in the context menu of explorer
48 * when you right-click on a file).
50 * HELPERS
51 * You can use this object transparently by calling the helper functions
52 * AssocQueryKeyA(), AssocQueryStringA() and AssocQueryStringByKeyA(). These
53 * create an IQueryAssociations object, perform the requested actions
54 * and then dispose of the object. Alternatively, you can create an instance
55 * of the object using AssocCreate() and call the following methods on it:
57 * METHODS
60 typedef struct
62 IQueryAssociations IQueryAssociations_iface;
63 LONG ref;
64 HKEY hkeySource;
65 HKEY hkeyProgID;
66 } IQueryAssociationsImpl;
68 typedef struct
70 IApplicationAssociationRegistration IApplicationAssociationRegistration_iface;
71 LONG ref;
72 } IApplicationAssociationRegistrationImpl;
75 static inline IQueryAssociationsImpl *impl_from_IQueryAssociations(IQueryAssociations *iface)
77 return CONTAINING_RECORD(iface, IQueryAssociationsImpl, IQueryAssociations_iface);
80 struct enumassochandlers
82 IEnumAssocHandlers IEnumAssocHandlers_iface;
83 LONG ref;
86 static inline struct enumassochandlers *impl_from_IEnumAssocHandlers(IEnumAssocHandlers *iface)
88 return CONTAINING_RECORD(iface, struct enumassochandlers, IEnumAssocHandlers_iface);
91 /**************************************************************************
92 * IQueryAssociations_QueryInterface
94 * See IUnknown_QueryInterface.
96 static HRESULT WINAPI IQueryAssociations_fnQueryInterface(
97 IQueryAssociations* iface,
98 REFIID riid,
99 LPVOID *ppvObj)
101 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
103 TRACE("(%p,%s,%p)\n",This, debugstr_guid(riid), ppvObj);
105 if (ppvObj == NULL)
106 return E_POINTER;
108 *ppvObj = NULL;
110 if (IsEqualIID(riid, &IID_IUnknown) ||
111 IsEqualIID(riid, &IID_IQueryAssociations))
113 *ppvObj = &This->IQueryAssociations_iface;
115 IQueryAssociations_AddRef((IQueryAssociations*)*ppvObj);
116 TRACE("Returning IQueryAssociations (%p)\n", *ppvObj);
117 return S_OK;
119 TRACE("Returning E_NOINTERFACE\n");
120 return E_NOINTERFACE;
123 /**************************************************************************
124 * IQueryAssociations_AddRef
126 * See IUnknown_AddRef.
128 static ULONG WINAPI IQueryAssociations_fnAddRef(IQueryAssociations *iface)
130 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
131 ULONG refCount = InterlockedIncrement(&This->ref);
133 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
135 return refCount;
138 /**************************************************************************
139 * IQueryAssociations_Release
141 * See IUnknown_Release.
143 static ULONG WINAPI IQueryAssociations_fnRelease(IQueryAssociations *iface)
145 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
146 ULONG refCount = InterlockedDecrement(&This->ref);
148 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
150 if (!refCount)
152 TRACE("Destroying IQueryAssociations (%p)\n", This);
153 RegCloseKey(This->hkeySource);
154 RegCloseKey(This->hkeyProgID);
155 SHFree(This);
158 return refCount;
161 /**************************************************************************
162 * IQueryAssociations_Init
164 * Initialise an IQueryAssociations object.
166 * PARAMS
167 * iface [I] IQueryAssociations interface to initialise
168 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
169 * pszAssoc [I] String for the root key name, or NULL if hkeyProgid is given
170 * hkeyProgid [I] Handle for the root key, or NULL if pszAssoc is given
171 * hWnd [I] Reserved, must be NULL.
173 * RETURNS
174 * Success: S_OK. iface is initialised with the parameters given.
175 * Failure: An HRESULT error code indicating the error.
177 static HRESULT WINAPI IQueryAssociations_fnInit(
178 IQueryAssociations *iface,
179 ASSOCF cfFlags,
180 LPCWSTR pszAssoc,
181 HKEY hkeyProgid,
182 HWND hWnd)
184 static const WCHAR szProgID[] = {'P','r','o','g','I','D',0};
185 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
186 LONG ret;
188 TRACE("(%p)->(%d,%s,%p,%p)\n", iface,
189 cfFlags,
190 debugstr_w(pszAssoc),
191 hkeyProgid,
192 hWnd);
193 if (hWnd != NULL)
194 FIXME("hwnd != NULL not supported\n");
195 if (cfFlags != 0)
196 FIXME("unsupported flags: %x\n", cfFlags);
198 RegCloseKey(This->hkeySource);
199 RegCloseKey(This->hkeyProgID);
200 This->hkeySource = This->hkeyProgID = NULL;
201 if (pszAssoc != NULL)
203 ret = RegOpenKeyExW(HKEY_CLASSES_ROOT,
204 pszAssoc,
206 KEY_READ,
207 &This->hkeySource);
208 if (ret)
209 return S_OK;
210 /* if this is not a prog id */
211 if ((*pszAssoc == '.') || (*pszAssoc == '{'))
213 RegOpenKeyExW(This->hkeySource,
214 szProgID,
216 KEY_READ,
217 &This->hkeyProgID);
219 else
220 This->hkeyProgID = This->hkeySource;
221 return S_OK;
223 else if (hkeyProgid != NULL)
225 This->hkeyProgID = hkeyProgid;
226 return S_OK;
228 else
229 return E_INVALIDARG;
232 static HRESULT ASSOC_GetValue(HKEY hkey, const WCHAR *name, void **data, DWORD *data_size)
234 DWORD size;
235 LONG ret;
237 ret = RegQueryValueExW(hkey, name, 0, NULL, NULL, &size);
238 if (ret != ERROR_SUCCESS)
239 return HRESULT_FROM_WIN32(ret);
240 if (!size)
241 return E_FAIL;
242 *data = HeapAlloc(GetProcessHeap(), 0, size);
243 if (!*data)
244 return E_OUTOFMEMORY;
245 ret = RegQueryValueExW(hkey, name, 0, NULL, (LPBYTE)*data, &size);
246 if (ret != ERROR_SUCCESS)
248 HeapFree(GetProcessHeap(), 0, *data);
249 return HRESULT_FROM_WIN32(ret);
251 if(data_size)
252 *data_size = size;
253 return S_OK;
256 static HRESULT ASSOC_GetCommand(IQueryAssociationsImpl *This, const WCHAR *extra, WCHAR **command)
258 HKEY hkeyCommand;
259 HKEY hkeyShell;
260 HKEY hkeyVerb;
261 HRESULT hr;
262 LONG ret;
263 WCHAR *extra_from_reg = NULL;
264 WCHAR *filetype;
265 static const WCHAR commandW[] = { 'c','o','m','m','a','n','d',0 };
266 static const WCHAR shellW[] = { 's','h','e','l','l',0 };
268 /* When looking for file extension it's possible to have a default value
269 that points to another key that contains 'shell/<verb>/command' subtree. */
270 hr = ASSOC_GetValue(This->hkeySource, NULL, (void**)&filetype, NULL);
271 if (hr == S_OK)
273 HKEY hkeyFile;
275 ret = RegOpenKeyExW(HKEY_CLASSES_ROOT, filetype, 0, KEY_READ, &hkeyFile);
276 HeapFree(GetProcessHeap(), 0, filetype);
278 if (ret == ERROR_SUCCESS)
280 ret = RegOpenKeyExW(hkeyFile, shellW, 0, KEY_READ, &hkeyShell);
281 RegCloseKey(hkeyFile);
283 else
284 ret = RegOpenKeyExW(This->hkeySource, shellW, 0, KEY_READ, &hkeyShell);
286 else
287 ret = RegOpenKeyExW(This->hkeySource, shellW, 0, KEY_READ, &hkeyShell);
289 if (ret) return HRESULT_FROM_WIN32(ret);
291 if (!extra)
293 /* check for default verb */
294 hr = ASSOC_GetValue(hkeyShell, NULL, (void**)&extra_from_reg, NULL);
295 if (FAILED(hr))
297 /* no default verb, try first subkey */
298 DWORD max_subkey_len;
300 ret = RegQueryInfoKeyW(hkeyShell, NULL, NULL, NULL, NULL, &max_subkey_len, NULL, NULL, NULL, NULL, NULL, NULL);
301 if (ret)
303 RegCloseKey(hkeyShell);
304 return HRESULT_FROM_WIN32(ret);
307 max_subkey_len++;
308 extra_from_reg = HeapAlloc(GetProcessHeap(), 0, max_subkey_len * sizeof(WCHAR));
309 if (!extra_from_reg)
311 RegCloseKey(hkeyShell);
312 return E_OUTOFMEMORY;
315 ret = RegEnumKeyExW(hkeyShell, 0, extra_from_reg, &max_subkey_len, NULL, NULL, NULL, NULL);
316 if (ret)
318 HeapFree(GetProcessHeap(), 0, extra_from_reg);
319 RegCloseKey(hkeyShell);
320 return HRESULT_FROM_WIN32(ret);
323 extra = extra_from_reg;
326 /* open verb subkey */
327 ret = RegOpenKeyExW(hkeyShell, extra, 0, KEY_READ, &hkeyVerb);
328 HeapFree(GetProcessHeap(), 0, extra_from_reg);
329 RegCloseKey(hkeyShell);
330 if (ret) return HRESULT_FROM_WIN32(ret);
332 /* open command subkey */
333 ret = RegOpenKeyExW(hkeyVerb, commandW, 0, KEY_READ, &hkeyCommand);
334 RegCloseKey(hkeyVerb);
335 if (ret) return HRESULT_FROM_WIN32(ret);
337 hr = ASSOC_GetValue(hkeyCommand, NULL, (void**)command, NULL);
338 RegCloseKey(hkeyCommand);
339 return hr;
342 static HRESULT ASSOC_GetExecutable(IQueryAssociationsImpl *This,
343 LPCWSTR pszExtra, LPWSTR path,
344 DWORD pathlen, DWORD *len)
346 WCHAR *pszCommand;
347 WCHAR *pszStart;
348 WCHAR *pszEnd;
349 HRESULT hr;
351 hr = ASSOC_GetCommand(This, pszExtra, &pszCommand);
352 if (FAILED(hr))
353 return hr;
355 /* cleanup pszCommand */
356 if (pszCommand[0] == '"')
358 pszStart = pszCommand + 1;
359 pszEnd = strchrW(pszStart, '"');
360 if (pszEnd)
361 *pszEnd = 0;
362 *len = SearchPathW(NULL, pszStart, NULL, pathlen, path, NULL);
364 else
366 pszStart = pszCommand;
367 for (pszEnd = pszStart; (pszEnd = strchrW(pszEnd, ' ')); pszEnd++)
369 WCHAR c = *pszEnd;
370 *pszEnd = 0;
371 if ((*len = SearchPathW(NULL, pszStart, NULL, pathlen, path, NULL)))
372 break;
373 *pszEnd = c;
375 if (!pszEnd)
376 *len = SearchPathW(NULL, pszStart, NULL, pathlen, path, NULL);
379 HeapFree(GetProcessHeap(), 0, pszCommand);
380 if (!*len)
381 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
382 return S_OK;
385 static HRESULT ASSOC_ReturnData(void *out, DWORD *outlen, const void *data,
386 DWORD datalen)
388 if (out)
390 if (*outlen < datalen)
392 *outlen = datalen;
393 return E_POINTER;
395 *outlen = datalen;
396 memcpy(out, data, datalen);
397 return S_OK;
399 else
401 *outlen = datalen;
402 return S_FALSE;
406 static HRESULT ASSOC_ReturnString(ASSOCF flags, LPWSTR out, DWORD *outlen, LPCWSTR data, DWORD datalen)
408 HRESULT hr = S_OK;
409 DWORD len;
411 TRACE("flags=0x%08x, data=%s\n", flags, debugstr_w(data));
413 if (!out)
415 *outlen = datalen;
416 return S_FALSE;
419 if (*outlen < datalen)
421 if (flags & ASSOCF_NOTRUNCATE)
423 len = 0;
424 if (*outlen > 0) out[0] = 0;
425 hr = E_POINTER;
427 else
429 len = min(*outlen, datalen);
430 hr = E_NOT_SUFFICIENT_BUFFER;
432 *outlen = datalen;
434 else
435 len = datalen;
437 if (len)
438 memcpy(out, data, len*sizeof(WCHAR));
440 return hr;
443 /**************************************************************************
444 * IQueryAssociations_GetString
446 * Get a file association string from the registry.
448 * PARAMS
449 * iface [I] IQueryAssociations interface to query
450 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
451 * str [I] Type of string to get (ASSOCSTR enum from "shlwapi.h")
452 * pszExtra [I] Extra information about the string location
453 * pszOut [O] Destination for the association string
454 * pcchOut [I/O] Length of pszOut
456 * RETURNS
457 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
458 * Failure: An HRESULT error code indicating the error.
460 static HRESULT WINAPI IQueryAssociations_fnGetString(
461 IQueryAssociations *iface,
462 ASSOCF flags,
463 ASSOCSTR str,
464 LPCWSTR pszExtra,
465 LPWSTR pszOut,
466 DWORD *pcchOut)
468 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
469 const ASSOCF unimplemented_flags = ~ASSOCF_NOTRUNCATE;
470 DWORD len = 0;
471 HRESULT hr;
472 WCHAR path[MAX_PATH];
474 TRACE("(%p)->(0x%08x, %u, %s, %p, %p)\n", This, flags, str, debugstr_w(pszExtra), pszOut, pcchOut);
476 if (flags & unimplemented_flags)
477 FIXME("%08x: unimplemented flags\n", flags & unimplemented_flags);
479 if (!pcchOut)
480 return E_UNEXPECTED;
482 if (!This->hkeySource && !This->hkeyProgID)
483 return HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION);
485 switch (str)
487 case ASSOCSTR_COMMAND:
489 WCHAR *command;
490 hr = ASSOC_GetCommand(This, pszExtra, &command);
491 if (SUCCEEDED(hr))
493 hr = ASSOC_ReturnString(flags, pszOut, pcchOut, command, strlenW(command) + 1);
494 HeapFree(GetProcessHeap(), 0, command);
496 return hr;
499 case ASSOCSTR_EXECUTABLE:
501 hr = ASSOC_GetExecutable(This, pszExtra, path, MAX_PATH, &len);
502 if (FAILED(hr))
503 return hr;
504 len++;
505 return ASSOC_ReturnString(flags, pszOut, pcchOut, path, len);
508 case ASSOCSTR_FRIENDLYDOCNAME:
510 WCHAR *pszFileType;
511 DWORD ret;
512 DWORD size;
514 hr = ASSOC_GetValue(This->hkeySource, NULL, (void**)&pszFileType, NULL);
515 if (FAILED(hr))
516 return hr;
517 size = 0;
518 ret = RegGetValueW(HKEY_CLASSES_ROOT, pszFileType, NULL, RRF_RT_REG_SZ, NULL, NULL, &size);
519 if (ret == ERROR_SUCCESS)
521 WCHAR *docName = HeapAlloc(GetProcessHeap(), 0, size);
522 if (docName)
524 ret = RegGetValueW(HKEY_CLASSES_ROOT, pszFileType, NULL, RRF_RT_REG_SZ, NULL, docName, &size);
525 if (ret == ERROR_SUCCESS)
526 hr = ASSOC_ReturnString(flags, pszOut, pcchOut, docName, strlenW(docName) + 1);
527 else
528 hr = HRESULT_FROM_WIN32(ret);
529 HeapFree(GetProcessHeap(), 0, docName);
531 else
532 hr = E_OUTOFMEMORY;
534 else
535 hr = HRESULT_FROM_WIN32(ret);
536 HeapFree(GetProcessHeap(), 0, pszFileType);
537 return hr;
540 case ASSOCSTR_FRIENDLYAPPNAME:
542 PVOID verinfoW = NULL;
543 DWORD size, retval = 0;
544 UINT flen;
545 WCHAR *bufW;
546 static const WCHAR translationW[] = {
547 '\\','V','a','r','F','i','l','e','I','n','f','o',
548 '\\','T','r','a','n','s','l','a','t','i','o','n',0
550 static const WCHAR fileDescFmtW[] = {
551 '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
552 '\\','%','0','4','x','%','0','4','x',
553 '\\','F','i','l','e','D','e','s','c','r','i','p','t','i','o','n',0
555 WCHAR fileDescW[41];
557 hr = ASSOC_GetExecutable(This, pszExtra, path, MAX_PATH, &len);
558 if (FAILED(hr))
559 return hr;
561 retval = GetFileVersionInfoSizeW(path, &size);
562 if (!retval)
563 goto get_friendly_name_fail;
564 verinfoW = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, retval);
565 if (!verinfoW)
566 return E_OUTOFMEMORY;
567 if (!GetFileVersionInfoW(path, 0, retval, verinfoW))
568 goto get_friendly_name_fail;
569 if (VerQueryValueW(verinfoW, translationW, (LPVOID *)&bufW, &flen))
571 UINT i;
572 DWORD *langCodeDesc = (DWORD *)bufW;
573 for (i = 0; i < flen / sizeof(DWORD); i++)
575 sprintfW(fileDescW, fileDescFmtW, LOWORD(langCodeDesc[i]),
576 HIWORD(langCodeDesc[i]));
577 if (VerQueryValueW(verinfoW, fileDescW, (LPVOID *)&bufW, &flen))
579 /* Does strlenW(bufW) == 0 mean we use the filename? */
580 len = strlenW(bufW) + 1;
581 TRACE("found FileDescription: %s\n", debugstr_w(bufW));
582 hr = ASSOC_ReturnString(flags, pszOut, pcchOut, bufW, len);
583 HeapFree(GetProcessHeap(), 0, verinfoW);
584 return hr;
588 get_friendly_name_fail:
589 PathRemoveExtensionW(path);
590 PathStripPathW(path);
591 TRACE("using filename: %s\n", debugstr_w(path));
592 hr = ASSOC_ReturnString(flags, pszOut, pcchOut, path, strlenW(path) + 1);
593 HeapFree(GetProcessHeap(), 0, verinfoW);
594 return hr;
597 case ASSOCSTR_CONTENTTYPE:
599 static const WCHAR Content_TypeW[] = {'C','o','n','t','e','n','t',' ','T','y','p','e',0};
600 WCHAR *contentType;
601 DWORD ret;
602 DWORD size;
604 size = 0;
605 ret = RegGetValueW(This->hkeySource, NULL, Content_TypeW, RRF_RT_REG_SZ, NULL, NULL, &size);
606 if (ret != ERROR_SUCCESS)
607 return HRESULT_FROM_WIN32(ret);
608 contentType = HeapAlloc(GetProcessHeap(), 0, size);
609 if (contentType != NULL)
611 ret = RegGetValueW(This->hkeySource, NULL, Content_TypeW, RRF_RT_REG_SZ, NULL, contentType, &size);
612 if (ret == ERROR_SUCCESS)
613 hr = ASSOC_ReturnString(flags, pszOut, pcchOut, contentType, strlenW(contentType) + 1);
614 else
615 hr = HRESULT_FROM_WIN32(ret);
616 HeapFree(GetProcessHeap(), 0, contentType);
618 else
619 hr = E_OUTOFMEMORY;
620 return hr;
623 case ASSOCSTR_DEFAULTICON:
625 static const WCHAR DefaultIconW[] = {'D','e','f','a','u','l','t','I','c','o','n',0};
626 WCHAR *pszFileType;
627 DWORD ret;
628 DWORD size;
629 HKEY hkeyFile;
631 hr = ASSOC_GetValue(This->hkeySource, NULL, (void**)&pszFileType, NULL);
632 if (FAILED(hr))
633 return hr;
634 ret = RegOpenKeyExW(HKEY_CLASSES_ROOT, pszFileType, 0, KEY_READ, &hkeyFile);
635 if (ret == ERROR_SUCCESS)
637 size = 0;
638 ret = RegGetValueW(hkeyFile, DefaultIconW, NULL, RRF_RT_REG_SZ, NULL, NULL, &size);
639 if (ret == ERROR_SUCCESS)
641 WCHAR *icon = HeapAlloc(GetProcessHeap(), 0, size);
642 if (icon)
644 ret = RegGetValueW(hkeyFile, DefaultIconW, NULL, RRF_RT_REG_SZ, NULL, icon, &size);
645 if (ret == ERROR_SUCCESS)
646 hr = ASSOC_ReturnString(flags, pszOut, pcchOut, icon, strlenW(icon) + 1);
647 else
648 hr = HRESULT_FROM_WIN32(ret);
649 HeapFree(GetProcessHeap(), 0, icon);
651 else
652 hr = E_OUTOFMEMORY;
654 else
655 hr = HRESULT_FROM_WIN32(ret);
656 RegCloseKey(hkeyFile);
658 else
659 hr = HRESULT_FROM_WIN32(ret);
660 HeapFree(GetProcessHeap(), 0, pszFileType);
661 return hr;
663 case ASSOCSTR_SHELLEXTENSION:
665 static const WCHAR shellexW[] = {'S','h','e','l','l','E','x','\\',0};
666 WCHAR keypath[sizeof(shellexW) / sizeof(shellexW[0]) + 39], guid[39];
667 CLSID clsid;
668 HKEY hkey;
669 DWORD size;
670 LONG ret;
672 hr = CLSIDFromString(pszExtra, &clsid);
673 if (FAILED(hr)) return hr;
675 strcpyW(keypath, shellexW);
676 strcatW(keypath, pszExtra);
677 ret = RegOpenKeyExW(This->hkeySource, keypath, 0, KEY_READ, &hkey);
678 if (ret) return HRESULT_FROM_WIN32(ret);
680 size = sizeof(guid);
681 ret = RegGetValueW(hkey, NULL, NULL, RRF_RT_REG_SZ, NULL, guid, &size);
682 RegCloseKey(hkey);
683 if (ret) return HRESULT_FROM_WIN32(ret);
685 return ASSOC_ReturnString(flags, pszOut, pcchOut, guid, size / sizeof(WCHAR));
688 default:
689 FIXME("assocstr %d unimplemented!\n", str);
690 return E_NOTIMPL;
694 /**************************************************************************
695 * IQueryAssociations_GetKey
697 * Get a file association key from the registry.
699 * PARAMS
700 * iface [I] IQueryAssociations interface to query
701 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
702 * assockey [I] Type of key to get (ASSOCKEY enum from "shlwapi.h")
703 * pszExtra [I] Extra information about the key location
704 * phkeyOut [O] Destination for the association key
706 * RETURNS
707 * Success: S_OK. phkeyOut contains a handle to the key.
708 * Failure: An HRESULT error code indicating the error.
710 static HRESULT WINAPI IQueryAssociations_fnGetKey(
711 IQueryAssociations *iface,
712 ASSOCF cfFlags,
713 ASSOCKEY assockey,
714 LPCWSTR pszExtra,
715 HKEY *phkeyOut)
717 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
719 FIXME("(%p,0x%8x,0x%8x,%s,%p)-stub!\n", This, cfFlags, assockey,
720 debugstr_w(pszExtra), phkeyOut);
721 return E_NOTIMPL;
724 /**************************************************************************
725 * IQueryAssociations_GetData
727 * Get the data for a file association key from the registry.
729 * PARAMS
730 * iface [I] IQueryAssociations interface to query
731 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
732 * assocdata [I] Type of data to get (ASSOCDATA enum from "shlwapi.h")
733 * pszExtra [I] Extra information about the data location
734 * pvOut [O] Destination for the association key
735 * pcbOut [I/O] Size of pvOut
737 * RETURNS
738 * Success: S_OK. pszOut contains the data, pcbOut contains its length.
739 * Failure: An HRESULT error code indicating the error.
741 static HRESULT WINAPI IQueryAssociations_fnGetData(IQueryAssociations *iface,
742 ASSOCF cfFlags, ASSOCDATA assocdata, LPCWSTR pszExtra, LPVOID pvOut,
743 DWORD *pcbOut)
745 static const WCHAR edit_flags[] = {'E','d','i','t','F','l','a','g','s',0};
747 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
748 void *data;
749 DWORD size;
750 HRESULT hres;
752 TRACE("(%p,0x%8x,0x%8x,%s,%p,%p)\n", This, cfFlags, assocdata,
753 debugstr_w(pszExtra), pvOut, pcbOut);
755 if(cfFlags)
756 FIXME("Unsupported flags: %x\n", cfFlags);
758 switch(assocdata) {
759 case ASSOCDATA_EDITFLAGS:
760 if(!This->hkeyProgID)
761 return HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION);
763 hres = ASSOC_GetValue(This->hkeyProgID, edit_flags, &data, &size);
764 if(FAILED(hres) || !pcbOut)
765 return hres;
767 hres = ASSOC_ReturnData(pvOut, pcbOut, data, size);
768 HeapFree(GetProcessHeap(), 0, data);
769 return hres;
770 default:
771 FIXME("Unsupported ASSOCDATA value: %d\n", assocdata);
772 return E_NOTIMPL;
776 /**************************************************************************
777 * IQueryAssociations_GetEnum
779 * Not yet implemented in native Win32.
781 * PARAMS
782 * iface [I] IQueryAssociations interface to query
783 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
784 * assocenum [I] Type of enum to get (ASSOCENUM enum from "shlwapi.h")
785 * pszExtra [I] Extra information about the enum location
786 * riid [I] REFIID to look for
787 * ppvOut [O] Destination for the interface.
789 * RETURNS
790 * Success: S_OK.
791 * Failure: An HRESULT error code indicating the error.
793 * NOTES
794 * Presumably this function returns an enumerator object.
796 static HRESULT WINAPI IQueryAssociations_fnGetEnum(
797 IQueryAssociations *iface,
798 ASSOCF cfFlags,
799 ASSOCENUM assocenum,
800 LPCWSTR pszExtra,
801 REFIID riid,
802 LPVOID *ppvOut)
804 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
806 FIXME("(%p,0x%8x,0x%8x,%s,%s,%p)-stub!\n", This, cfFlags, assocenum,
807 debugstr_w(pszExtra), debugstr_guid(riid), ppvOut);
808 return E_NOTIMPL;
811 static const IQueryAssociationsVtbl IQueryAssociations_vtbl =
813 IQueryAssociations_fnQueryInterface,
814 IQueryAssociations_fnAddRef,
815 IQueryAssociations_fnRelease,
816 IQueryAssociations_fnInit,
817 IQueryAssociations_fnGetString,
818 IQueryAssociations_fnGetKey,
819 IQueryAssociations_fnGetData,
820 IQueryAssociations_fnGetEnum
823 /**************************************************************************
824 * IApplicationAssociationRegistration implementation
826 static inline IApplicationAssociationRegistrationImpl *impl_from_IApplicationAssociationRegistration(IApplicationAssociationRegistration *iface)
828 return CONTAINING_RECORD(iface, IApplicationAssociationRegistrationImpl, IApplicationAssociationRegistration_iface);
831 static HRESULT WINAPI ApplicationAssociationRegistration_QueryInterface(
832 IApplicationAssociationRegistration* iface, REFIID riid, LPVOID *ppv)
834 IApplicationAssociationRegistrationImpl *This = impl_from_IApplicationAssociationRegistration(iface);
836 TRACE("(%p, %s, %p)\n",This, debugstr_guid(riid), ppv);
838 if (ppv == NULL)
839 return E_POINTER;
841 if (IsEqualGUID(&IID_IUnknown, riid) ||
842 IsEqualGUID(&IID_IApplicationAssociationRegistration, riid)) {
843 *ppv = &This->IApplicationAssociationRegistration_iface;
844 IUnknown_AddRef((IUnknown*)*ppv);
845 TRACE("returning IApplicationAssociationRegistration: %p\n", *ppv);
846 return S_OK;
849 *ppv = NULL;
850 FIXME("(%p)->(%s %p) interface not supported\n", This, debugstr_guid(riid), ppv);
851 return E_NOINTERFACE;
854 static ULONG WINAPI ApplicationAssociationRegistration_AddRef(IApplicationAssociationRegistration *iface)
856 IApplicationAssociationRegistrationImpl *This = impl_from_IApplicationAssociationRegistration(iface);
857 ULONG ref = InterlockedIncrement(&This->ref);
859 TRACE("(%p) ref=%d\n", This, ref);
860 return ref;
863 static ULONG WINAPI ApplicationAssociationRegistration_Release(IApplicationAssociationRegistration *iface)
865 IApplicationAssociationRegistrationImpl *This = impl_from_IApplicationAssociationRegistration(iface);
866 ULONG ref = InterlockedDecrement(&This->ref);
868 TRACE("(%p) ref=%d\n", This, ref);
870 if (!ref) {
871 SHFree(This);
873 return ref;
876 static HRESULT WINAPI ApplicationAssociationRegistration_QueryCurrentDefault(IApplicationAssociationRegistration *iface, LPCWSTR query,
877 ASSOCIATIONTYPE type, ASSOCIATIONLEVEL level, LPWSTR *association)
879 IApplicationAssociationRegistrationImpl *This = impl_from_IApplicationAssociationRegistration(iface);
880 static WCHAR urlassoc[] = {'U','r','l','A','s','s','o','c','i','a','t','i','o','n','s',0};
881 static WCHAR mimeassoc[] = {'M','I','M','E','A','s','s','o','c','i','a','t','i','o','n','s',0};
882 static WCHAR assocations[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
883 'W','i','n','d','o','w','s','\\','S','h','e','l','l','\\',
884 'A','s','s','o','c','i','a','t','i','o','n','s',0};
885 static WCHAR slash[] = {'\\',0};
886 static WCHAR choice[] = {'U','s','e','r','C','h','o','i','c','e',0};
887 static WCHAR propid[] = {'P','r','o','g','i','d',0};
888 WCHAR path[MAX_PATH] = {0};
889 DWORD ret, keytype, size;
890 HKEY hkey = NULL;
891 HRESULT hr = HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION);
893 TRACE("(%p)->(%s, %d, %d, %p)\n", This, debugstr_w(query), type, level, association);
895 if(!association)
896 return E_INVALIDARG;
898 *association = NULL;
900 if((type == AT_URLPROTOCOL || type == AT_FILEEXTENSION) && !query[0])
901 return E_INVALIDARG;
902 else if(type == AT_FILEEXTENSION && query[0] != '.')
903 return E_INVALIDARG;
905 if(type == AT_FILEEXTENSION)
907 ret = RegOpenKeyExW(HKEY_CLASSES_ROOT, query, 0, KEY_READ, &hkey);
908 if(ret == ERROR_SUCCESS)
910 ret = RegGetValueW(hkey, NULL, NULL, RRF_RT_REG_SZ, &keytype, NULL, &size);
911 if(ret == ERROR_SUCCESS)
913 *association = CoTaskMemAlloc(size);
914 if(*association)
916 ret = RegGetValueW(hkey, NULL, NULL, RRF_RT_REG_SZ, &keytype, *association, &size);
917 if(ret == ERROR_SUCCESS)
918 hr = S_OK;
919 else
921 CoTaskMemFree(*association);
922 *association = NULL;
925 else
926 hr = E_OUTOFMEMORY;
930 else
932 ret = RegOpenKeyExW(HKEY_CURRENT_USER, assocations, 0, KEY_READ, &hkey);
933 if(ret == ERROR_SUCCESS)
935 if(type == AT_URLPROTOCOL)
936 lstrcpyW(path, urlassoc);
937 else if(type == AT_MIMETYPE)
938 lstrcpyW(path, mimeassoc);
939 else
941 WARN("Unsupported type (%d).\n", type);
942 RegCloseKey(hkey);
943 return hr;
946 lstrcatW(path, slash);
947 lstrcatW(path, query);
948 lstrcatW(path, slash);
949 lstrcatW(path, choice);
951 ret = RegGetValueW(hkey, path, propid, RRF_RT_REG_SZ, &keytype, NULL, &size);
952 if(ret == ERROR_SUCCESS)
954 *association = CoTaskMemAlloc(size);
955 if(*association)
957 ret = RegGetValueW(hkey, path, propid, RRF_RT_REG_SZ, &keytype, *association, &size);
958 if(ret == ERROR_SUCCESS)
959 hr = S_OK;
960 else
962 CoTaskMemFree(*association);
963 *association = NULL;
966 else
967 hr = E_OUTOFMEMORY;
972 RegCloseKey(hkey);
974 return hr;
977 static HRESULT WINAPI ApplicationAssociationRegistration_QueryAppIsDefault(IApplicationAssociationRegistration* This, LPCWSTR query,
978 ASSOCIATIONTYPE type, ASSOCIATIONLEVEL level, LPCWSTR appname, BOOL *is_default)
980 FIXME("(%p)->(%s, %d, %d, %s, %p)\n", This, debugstr_w(query), type, level, debugstr_w(appname), is_default);
981 return E_NOTIMPL;
984 static HRESULT WINAPI ApplicationAssociationRegistration_QueryAppIsDefaultAll(IApplicationAssociationRegistration* This, ASSOCIATIONLEVEL level,
985 LPCWSTR appname, BOOL *is_default)
987 FIXME("(%p)->(%d, %s, %p)\n", This, level, debugstr_w(appname), is_default);
988 return E_NOTIMPL;
991 static HRESULT WINAPI ApplicationAssociationRegistration_SetAppAsDefault(IApplicationAssociationRegistration* This, LPCWSTR appname,
992 LPCWSTR set, ASSOCIATIONTYPE set_type)
994 FIXME("(%p)->(%s, %s, %d)\n", This, debugstr_w(appname), debugstr_w(set), set_type);
995 return E_NOTIMPL;
998 static HRESULT WINAPI ApplicationAssociationRegistration_SetAppAsDefaultAll(IApplicationAssociationRegistration* This, LPCWSTR appname)
1000 FIXME("(%p)->(%s)\n", This, debugstr_w(appname));
1001 return E_NOTIMPL;
1005 static HRESULT WINAPI ApplicationAssociationRegistration_ClearUserAssociations(IApplicationAssociationRegistration* This)
1007 FIXME("(%p)\n", This);
1008 return E_NOTIMPL;
1012 static const IApplicationAssociationRegistrationVtbl IApplicationAssociationRegistration_vtbl =
1014 ApplicationAssociationRegistration_QueryInterface,
1015 ApplicationAssociationRegistration_AddRef,
1016 ApplicationAssociationRegistration_Release,
1017 ApplicationAssociationRegistration_QueryCurrentDefault,
1018 ApplicationAssociationRegistration_QueryAppIsDefault,
1019 ApplicationAssociationRegistration_QueryAppIsDefaultAll,
1020 ApplicationAssociationRegistration_SetAppAsDefault,
1021 ApplicationAssociationRegistration_SetAppAsDefaultAll,
1022 ApplicationAssociationRegistration_ClearUserAssociations
1025 /**************************************************************************
1026 * IQueryAssociations_Constructor [internal]
1028 * Construct a new IQueryAssociations object.
1030 HRESULT WINAPI QueryAssociations_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppOutput)
1032 IQueryAssociationsImpl* this;
1033 HRESULT ret;
1035 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1037 if (!(this = SHAlloc(sizeof(*this)))) return E_OUTOFMEMORY;
1038 this->IQueryAssociations_iface.lpVtbl = &IQueryAssociations_vtbl;
1039 this->ref = 0;
1040 this->hkeySource = 0;
1041 this->hkeyProgID = 0;
1042 if (FAILED(ret = IQueryAssociations_QueryInterface(&this->IQueryAssociations_iface, riid, ppOutput))) SHFree( this );
1043 TRACE("returning %p\n", *ppOutput);
1044 return ret;
1047 /**************************************************************************
1048 * ApplicationAssociationRegistration_Constructor [internal]
1050 * Construct a IApplicationAssociationRegistration object.
1052 HRESULT WINAPI ApplicationAssociationRegistration_Constructor(IUnknown *outer, REFIID riid, LPVOID *ppv)
1054 IApplicationAssociationRegistrationImpl *This;
1055 HRESULT hr;
1057 if (outer)
1058 return CLASS_E_NOAGGREGATION;
1060 if (!(This = SHAlloc(sizeof(*This))))
1061 return E_OUTOFMEMORY;
1063 This->IApplicationAssociationRegistration_iface.lpVtbl = &IApplicationAssociationRegistration_vtbl;
1064 This->ref = 0;
1066 hr = IApplicationAssociationRegistration_QueryInterface(&This->IApplicationAssociationRegistration_iface, riid, ppv);
1067 if (FAILED(hr))
1068 SHFree(This);
1070 TRACE("returning 0x%x with %p\n", hr, *ppv);
1071 return hr;
1074 static HRESULT WINAPI enumassochandlers_QueryInterface(IEnumAssocHandlers *iface, REFIID riid, void **obj)
1076 struct enumassochandlers *This = impl_from_IEnumAssocHandlers(iface);
1078 TRACE("(%p %s %p)\n", This, debugstr_guid(riid), obj);
1080 if (IsEqualIID(riid, &IID_IEnumAssocHandlers) ||
1081 IsEqualIID(riid, &IID_IUnknown))
1083 *obj = iface;
1084 IEnumAssocHandlers_AddRef(iface);
1085 return S_OK;
1088 *obj = NULL;
1089 return E_NOINTERFACE;
1092 static ULONG WINAPI enumassochandlers_AddRef(IEnumAssocHandlers *iface)
1094 struct enumassochandlers *This = impl_from_IEnumAssocHandlers(iface);
1095 ULONG ref = InterlockedIncrement(&This->ref);
1097 TRACE("(%p)->(%u)\n", This, ref);
1098 return ref;
1101 static ULONG WINAPI enumassochandlers_Release(IEnumAssocHandlers *iface)
1103 struct enumassochandlers *This = impl_from_IEnumAssocHandlers(iface);
1104 ULONG ref = InterlockedDecrement(&This->ref);
1106 TRACE("(%p)->(%u)\n", This, ref);
1108 if (!ref)
1109 SHFree(This);
1111 return ref;
1114 static HRESULT WINAPI enumassochandlers_Next(IEnumAssocHandlers *iface, ULONG count, IAssocHandler **handlers,
1115 ULONG *fetched)
1117 struct enumassochandlers *This = impl_from_IEnumAssocHandlers(iface);
1119 FIXME("(%p)->(%u %p %p): stub\n", This, count, handlers, fetched);
1121 return E_NOTIMPL;
1124 static const IEnumAssocHandlersVtbl enumassochandlersvtbl = {
1125 enumassochandlers_QueryInterface,
1126 enumassochandlers_AddRef,
1127 enumassochandlers_Release,
1128 enumassochandlers_Next
1131 /**************************************************************************
1132 * SHAssocEnumHandlers [SHELL32.@]
1134 HRESULT WINAPI SHAssocEnumHandlers(const WCHAR *extra, ASSOC_FILTER filter, IEnumAssocHandlers **enumhandlers)
1136 struct enumassochandlers *enumassoc;
1138 FIXME("(%s %d %p): stub\n", debugstr_w(extra), filter, enumhandlers);
1140 *enumhandlers = NULL;
1142 enumassoc = SHAlloc(sizeof(*enumassoc));
1143 if (!enumassoc)
1144 return E_OUTOFMEMORY;
1146 enumassoc->IEnumAssocHandlers_iface.lpVtbl = &enumassochandlersvtbl;
1147 enumassoc->ref = 1;
1149 *enumhandlers = &enumassoc->IEnumAssocHandlers_iface;
1150 return S_OK;