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
31 #include "shell32_main.h"
33 #include "wine/unicode.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(shell);
38 /**************************************************************************
39 * IQueryAssociations {SHELL32}
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).
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:
62 IQueryAssociations IQueryAssociations_iface;
66 } IQueryAssociationsImpl;
68 static inline IQueryAssociationsImpl *impl_from_IQueryAssociations(IQueryAssociations *iface)
70 return CONTAINING_RECORD(iface, IQueryAssociationsImpl, IQueryAssociations_iface);
73 /**************************************************************************
74 * IQueryAssociations_QueryInterface {SHLWAPI}
76 * See IUnknown_QueryInterface.
78 static HRESULT WINAPI IQueryAssociations_fnQueryInterface(
79 IQueryAssociations* iface,
83 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
85 TRACE("(%p,%s,%p)\n",This, debugstr_guid(riid), ppvObj);
89 if (IsEqualIID(riid, &IID_IUnknown) ||
90 IsEqualIID(riid, &IID_IQueryAssociations))
94 IQueryAssociations_AddRef((IQueryAssociations*)*ppvObj);
95 TRACE("Returning IQueryAssociations (%p)\n", *ppvObj);
98 TRACE("Returning E_NOINTERFACE\n");
102 /**************************************************************************
103 * IQueryAssociations_AddRef {SHLWAPI}
105 * See IUnknown_AddRef.
107 static ULONG WINAPI IQueryAssociations_fnAddRef(IQueryAssociations *iface)
109 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
110 ULONG refCount = InterlockedIncrement(&This->ref);
112 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
117 /**************************************************************************
118 * IQueryAssociations_Release {SHLWAPI}
120 * See IUnknown_Release.
122 static ULONG WINAPI IQueryAssociations_fnRelease(IQueryAssociations *iface)
124 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
125 ULONG refCount = InterlockedDecrement(&This->ref);
127 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
131 TRACE("Destroying IQueryAssociations (%p)\n", This);
132 RegCloseKey(This->hkeySource);
133 RegCloseKey(This->hkeyProgID);
134 HeapFree(GetProcessHeap(), 0, This);
140 /**************************************************************************
141 * IQueryAssociations_Init {SHLWAPI}
143 * Initialise an IQueryAssociations object.
146 * iface [I] IQueryAssociations interface to initialise
147 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
148 * pszAssoc [I] String for the root key name, or NULL if hkeyProgid is given
149 * hkeyProgid [I] Handle for the root key, or NULL if pszAssoc is given
150 * hWnd [I] Reserved, must be NULL.
153 * Success: S_OK. iface is initialised with the parameters given.
154 * Failure: An HRESULT error code indicating the error.
156 static HRESULT WINAPI IQueryAssociations_fnInit(
157 IQueryAssociations *iface,
163 static const WCHAR szProgID[] = {'P','r','o','g','I','D',0};
164 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
167 TRACE("(%p)->(%d,%s,%p,%p)\n", iface,
169 debugstr_w(pszAssoc),
173 FIXME("hwnd != NULL not supported\n");
175 FIXME("unsupported flags: %x\n", cfFlags);
176 if (pszAssoc != NULL)
178 ret = RegOpenKeyExW(HKEY_CLASSES_ROOT,
183 if (ret != ERROR_SUCCESS)
185 /* if this is not a prog id */
186 if ((*pszAssoc == '.') || (*pszAssoc == '{'))
188 RegOpenKeyExW(This->hkeySource,
195 This->hkeyProgID = This->hkeySource;
198 else if (hkeyProgid != NULL)
200 This->hkeyProgID = hkeyProgid;
207 static HRESULT ASSOC_GetValue(HKEY hkey, WCHAR ** pszText)
213 ret = RegQueryValueExW(hkey, NULL, 0, NULL, NULL, &len);
214 if (ret != ERROR_SUCCESS)
215 return HRESULT_FROM_WIN32(ret);
218 *pszText = HeapAlloc(GetProcessHeap(), 0, len);
220 return E_OUTOFMEMORY;
221 ret = RegQueryValueExW(hkey, NULL, 0, NULL, (LPBYTE)*pszText,
223 if (ret != ERROR_SUCCESS)
225 HeapFree(GetProcessHeap(), 0, *pszText);
226 return HRESULT_FROM_WIN32(ret);
231 static HRESULT ASSOC_GetCommand(IQueryAssociationsImpl *This,
232 LPCWSTR pszExtra, WCHAR **ppszCommand)
240 WCHAR * pszExtraFromReg = NULL;
242 static const WCHAR commandW[] = { 'c','o','m','m','a','n','d',0 };
243 static const WCHAR shellW[] = { 's','h','e','l','l',0 };
245 hr = ASSOC_GetValue(This->hkeySource, &pszFileType);
248 ret = RegOpenKeyExW(HKEY_CLASSES_ROOT, pszFileType, 0, KEY_READ, &hkeyFile);
249 HeapFree(GetProcessHeap(), 0, pszFileType);
250 if (ret != ERROR_SUCCESS)
251 return HRESULT_FROM_WIN32(ret);
253 ret = RegOpenKeyExW(hkeyFile, shellW, 0, KEY_READ, &hkeyShell);
254 RegCloseKey(hkeyFile);
255 if (ret != ERROR_SUCCESS)
256 return HRESULT_FROM_WIN32(ret);
260 hr = ASSOC_GetValue(hkeyShell, &pszExtraFromReg);
261 /* if no default action */
262 if (hr == E_FAIL || hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
265 ret = RegQueryInfoKeyW(hkeyShell, 0, 0, 0, 0, &rlen, 0, 0, 0, 0, 0, 0);
266 if (ret != ERROR_SUCCESS)
268 RegCloseKey(hkeyShell);
269 return HRESULT_FROM_WIN32(ret);
272 pszExtraFromReg = HeapAlloc(GetProcessHeap(), 0, rlen * sizeof(WCHAR));
273 if (!pszExtraFromReg)
275 RegCloseKey(hkeyShell);
276 return E_OUTOFMEMORY;
278 ret = RegEnumKeyExW(hkeyShell, 0, pszExtraFromReg, &rlen, 0, NULL, NULL, NULL);
279 if (ret != ERROR_SUCCESS)
281 RegCloseKey(hkeyShell);
282 return HRESULT_FROM_WIN32(ret);
287 RegCloseKey(hkeyShell);
292 ret = RegOpenKeyExW(hkeyShell, pszExtra ? pszExtra : pszExtraFromReg, 0,
293 KEY_READ, &hkeyVerb);
294 HeapFree(GetProcessHeap(), 0, pszExtraFromReg);
295 RegCloseKey(hkeyShell);
296 if (ret != ERROR_SUCCESS)
297 return HRESULT_FROM_WIN32(ret);
299 ret = RegOpenKeyExW(hkeyVerb, commandW, 0, KEY_READ, &hkeyCommand);
300 RegCloseKey(hkeyVerb);
301 if (ret != ERROR_SUCCESS)
302 return HRESULT_FROM_WIN32(ret);
303 hr = ASSOC_GetValue(hkeyCommand, ppszCommand);
304 RegCloseKey(hkeyCommand);
308 static HRESULT ASSOC_GetExecutable(IQueryAssociationsImpl *This,
309 LPCWSTR pszExtra, LPWSTR path,
310 DWORD pathlen, DWORD *len)
319 hr = ASSOC_GetCommand(This, pszExtra, &pszCommand);
323 /* cleanup pszCommand */
324 if (pszCommand[0] == '"')
326 pszStart = pszCommand + 1;
327 pszEnd = strchrW(pszStart, '"');
330 *len = SearchPathW(NULL, pszStart, NULL, pathlen, path, NULL);
334 pszStart = pszCommand;
335 for (pszEnd = pszStart; (pszEnd = strchrW(pszEnd, ' ')); pszEnd++)
339 if ((*len = SearchPathW(NULL, pszStart, NULL, pathlen, path, NULL)))
344 *len = SearchPathW(NULL, pszStart, NULL, pathlen, path, NULL);
347 HeapFree(GetProcessHeap(), 0, pszCommand);
349 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
353 static HRESULT ASSOC_ReturnData(LPWSTR out, DWORD *outlen, LPCWSTR data,
360 if (*outlen < datalen)
366 lstrcpynW(out, data, datalen);
376 /**************************************************************************
377 * IQueryAssociations_GetString {SHLWAPI}
379 * Get a file association string from the registry.
382 * iface [I] IQueryAssociations interface to query
383 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
384 * str [I] Type of string to get (ASSOCSTR enum from "shlwapi.h")
385 * pszExtra [I] Extra information about the string location
386 * pszOut [O] Destination for the association string
387 * pcchOut [I/O] Length of pszOut
390 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
391 * Failure: An HRESULT error code indicating the error.
393 static HRESULT WINAPI IQueryAssociations_fnGetString(
394 IQueryAssociations *iface,
401 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
402 const ASSOCF cfUnimplemented = ~(0);
405 WCHAR path[MAX_PATH];
407 TRACE("(%p,0x%8x,0x%8x,%s,%p,%p)\n", This, cfFlags, str,
408 debugstr_w(pszExtra), pszOut, pcchOut);
410 if (cfFlags & cfUnimplemented)
411 FIXME("%08x: unimplemented flags!\n", cfFlags & cfUnimplemented);
418 case ASSOCSTR_COMMAND:
421 hr = ASSOC_GetCommand(This, pszExtra, &command);
424 hr = ASSOC_ReturnData(pszOut, pcchOut, command, strlenW(command) + 1);
425 HeapFree(GetProcessHeap(), 0, command);
430 case ASSOCSTR_EXECUTABLE:
432 hr = ASSOC_GetExecutable(This, pszExtra, path, MAX_PATH, &len);
436 return ASSOC_ReturnData(pszOut, pcchOut, path, len);
439 case ASSOCSTR_FRIENDLYDOCNAME:
445 hr = ASSOC_GetValue(This->hkeySource, &pszFileType);
449 ret = RegGetValueW(HKEY_CLASSES_ROOT, pszFileType, NULL, RRF_RT_REG_SZ, NULL, NULL, &size);
450 if (ret == ERROR_SUCCESS)
452 WCHAR *docName = HeapAlloc(GetProcessHeap(), 0, size);
455 ret = RegGetValueW(HKEY_CLASSES_ROOT, pszFileType, NULL, RRF_RT_REG_SZ, NULL, docName, &size);
456 if (ret == ERROR_SUCCESS)
457 hr = ASSOC_ReturnData(pszOut, pcchOut, docName, strlenW(docName) + 1);
459 hr = HRESULT_FROM_WIN32(ret);
460 HeapFree(GetProcessHeap(), 0, docName);
466 hr = HRESULT_FROM_WIN32(ret);
467 HeapFree(GetProcessHeap(), 0, pszFileType);
471 case ASSOCSTR_FRIENDLYAPPNAME:
473 PVOID verinfoW = NULL;
474 DWORD size, retval = 0;
477 static const WCHAR translationW[] = {
478 '\\','V','a','r','F','i','l','e','I','n','f','o',
479 '\\','T','r','a','n','s','l','a','t','i','o','n',0
481 static const WCHAR fileDescFmtW[] = {
482 '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
483 '\\','%','0','4','x','%','0','4','x',
484 '\\','F','i','l','e','D','e','s','c','r','i','p','t','i','o','n',0
488 hr = ASSOC_GetExecutable(This, pszExtra, path, MAX_PATH, &len);
492 retval = GetFileVersionInfoSizeW(path, &size);
494 goto get_friendly_name_fail;
495 verinfoW = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, retval);
497 return E_OUTOFMEMORY;
498 if (!GetFileVersionInfoW(path, 0, retval, verinfoW))
499 goto get_friendly_name_fail;
500 if (VerQueryValueW(verinfoW, translationW, (LPVOID *)&bufW, &flen))
503 DWORD *langCodeDesc = (DWORD *)bufW;
504 for (i = 0; i < flen / sizeof(DWORD); i++)
506 sprintfW(fileDescW, fileDescFmtW, LOWORD(langCodeDesc[i]),
507 HIWORD(langCodeDesc[i]));
508 if (VerQueryValueW(verinfoW, fileDescW, (LPVOID *)&bufW, &flen))
510 /* Does strlenW(bufW) == 0 mean we use the filename? */
511 len = strlenW(bufW) + 1;
512 TRACE("found FileDescription: %s\n", debugstr_w(bufW));
513 hr = ASSOC_ReturnData(pszOut, pcchOut, bufW, len);
514 HeapFree(GetProcessHeap(), 0, verinfoW);
519 get_friendly_name_fail:
520 PathRemoveExtensionW(path);
521 PathStripPathW(path);
522 TRACE("using filename: %s\n", debugstr_w(path));
523 hr = ASSOC_ReturnData(pszOut, pcchOut, path, strlenW(path) + 1);
524 HeapFree(GetProcessHeap(), 0, verinfoW);
528 case ASSOCSTR_CONTENTTYPE:
530 static const WCHAR Content_TypeW[] = {'C','o','n','t','e','n','t',' ','T','y','p','e',0};
536 ret = RegGetValueW(This->hkeySource, NULL, Content_TypeW, RRF_RT_REG_SZ, NULL, NULL, &size);
537 if (ret != ERROR_SUCCESS)
538 return HRESULT_FROM_WIN32(ret);
539 contentType = HeapAlloc(GetProcessHeap(), 0, size);
540 if (contentType != NULL)
542 ret = RegGetValueW(This->hkeySource, NULL, Content_TypeW, RRF_RT_REG_SZ, NULL, contentType, &size);
543 if (ret == ERROR_SUCCESS)
544 hr = ASSOC_ReturnData(pszOut, pcchOut, contentType, strlenW(contentType) + 1);
546 hr = HRESULT_FROM_WIN32(ret);
547 HeapFree(GetProcessHeap(), 0, contentType);
554 case ASSOCSTR_DEFAULTICON:
556 static const WCHAR DefaultIconW[] = {'D','e','f','a','u','l','t','I','c','o','n',0};
562 hr = ASSOC_GetValue(This->hkeySource, &pszFileType);
565 ret = RegOpenKeyExW(HKEY_CLASSES_ROOT, pszFileType, 0, KEY_READ, &hkeyFile);
566 if (ret == ERROR_SUCCESS)
569 ret = RegGetValueW(hkeyFile, DefaultIconW, NULL, RRF_RT_REG_SZ, NULL, NULL, &size);
570 if (ret == ERROR_SUCCESS)
572 WCHAR *icon = HeapAlloc(GetProcessHeap(), 0, size);
575 ret = RegGetValueW(hkeyFile, DefaultIconW, NULL, RRF_RT_REG_SZ, NULL, icon, &size);
576 if (ret == ERROR_SUCCESS)
577 hr = ASSOC_ReturnData(pszOut, pcchOut, icon, strlenW(icon) + 1);
579 hr = HRESULT_FROM_WIN32(ret);
580 HeapFree(GetProcessHeap(), 0, icon);
586 hr = HRESULT_FROM_WIN32(ret);
587 RegCloseKey(hkeyFile);
590 hr = HRESULT_FROM_WIN32(ret);
591 HeapFree(GetProcessHeap(), 0, pszFileType);
596 FIXME("assocstr %d unimplemented!\n", str);
601 /**************************************************************************
602 * IQueryAssociations_GetKey {SHLWAPI}
604 * Get a file association key from the registry.
607 * iface [I] IQueryAssociations interface to query
608 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
609 * assockey [I] Type of key to get (ASSOCKEY enum from "shlwapi.h")
610 * pszExtra [I] Extra information about the key location
611 * phkeyOut [O] Destination for the association key
614 * Success: S_OK. phkeyOut contains a handle to the key.
615 * Failure: An HRESULT error code indicating the error.
617 static HRESULT WINAPI IQueryAssociations_fnGetKey(
618 IQueryAssociations *iface,
624 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
626 FIXME("(%p,0x%8x,0x%8x,%s,%p)-stub!\n", This, cfFlags, assockey,
627 debugstr_w(pszExtra), phkeyOut);
631 /**************************************************************************
632 * IQueryAssociations_GetData {SHLWAPI}
634 * Get the data for a file association key from the registry.
637 * iface [I] IQueryAssociations interface to query
638 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
639 * assocdata [I] Type of data to get (ASSOCDATA enum from "shlwapi.h")
640 * pszExtra [I] Extra information about the data location
641 * pvOut [O] Destination for the association key
642 * pcbOut [I/O] Size of pvOut
645 * Success: S_OK. pszOut contains the data, pcbOut contains its length.
646 * Failure: An HRESULT error code indicating the error.
648 static HRESULT WINAPI IQueryAssociations_fnGetData(
649 IQueryAssociations *iface,
656 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
658 FIXME("(%p,0x%8x,0x%8x,%s,%p,%p)-stub!\n", This, cfFlags, assocdata,
659 debugstr_w(pszExtra), pvOut, pcbOut);
663 /**************************************************************************
664 * IQueryAssociations_GetEnum {SHLWAPI}
666 * Not yet implemented in native Win32.
669 * iface [I] IQueryAssociations interface to query
670 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
671 * assocenum [I] Type of enum to get (ASSOCENUM enum from "shlwapi.h")
672 * pszExtra [I] Extra information about the enum location
673 * riid [I] REFIID to look for
674 * ppvOut [O] Destination for the interface.
678 * Failure: An HRESULT error code indicating the error.
681 * Presumably this function returns an enumerator object.
683 static HRESULT WINAPI IQueryAssociations_fnGetEnum(
684 IQueryAssociations *iface,
691 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
693 FIXME("(%p,0x%8x,0x%8x,%s,%s,%p)-stub!\n", This, cfFlags, assocenum,
694 debugstr_w(pszExtra), debugstr_guid(riid), ppvOut);
698 static const IQueryAssociationsVtbl IQueryAssociations_vtbl =
700 IQueryAssociations_fnQueryInterface,
701 IQueryAssociations_fnAddRef,
702 IQueryAssociations_fnRelease,
703 IQueryAssociations_fnInit,
704 IQueryAssociations_fnGetString,
705 IQueryAssociations_fnGetKey,
706 IQueryAssociations_fnGetData,
707 IQueryAssociations_fnGetEnum
710 /**************************************************************************
711 * IQueryAssociations_Constructor [internal]
713 * Construct a new IQueryAssociations object.
715 HRESULT WINAPI QueryAssociations_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppOutput)
717 IQueryAssociationsImpl* this;
720 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
722 if (!(this = SHAlloc(sizeof(*this)))) return E_OUTOFMEMORY;
723 this->IQueryAssociations_iface.lpVtbl = &IQueryAssociations_vtbl;
725 this->hkeySource = 0;
726 this->hkeyProgID = 0;
727 if (FAILED(ret = IUnknown_QueryInterface((IUnknown *)this, riid, ppOutput))) SHFree( this );
728 TRACE("returning %p\n", *ppOutput);