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 const IQueryAssociationsVtbl
*lpVtbl
;
66 } IQueryAssociationsImpl
;
68 /**************************************************************************
69 * IQueryAssociations_QueryInterface {SHLWAPI}
71 * See IUnknown_QueryInterface.
73 static HRESULT WINAPI
IQueryAssociations_fnQueryInterface(
74 IQueryAssociations
* iface
,
78 IQueryAssociationsImpl
*This
= (IQueryAssociationsImpl
*)iface
;
80 TRACE("(%p,%s,%p)\n",This
, debugstr_guid(riid
), ppvObj
);
84 if (IsEqualIID(riid
, &IID_IUnknown
) ||
85 IsEqualIID(riid
, &IID_IQueryAssociations
))
89 IQueryAssociations_AddRef((IQueryAssociations
*)*ppvObj
);
90 TRACE("Returning IQueryAssociations (%p)\n", *ppvObj
);
93 TRACE("Returning E_NOINTERFACE\n");
97 /**************************************************************************
98 * IQueryAssociations_AddRef {SHLWAPI}
100 * See IUnknown_AddRef.
102 static ULONG WINAPI
IQueryAssociations_fnAddRef(IQueryAssociations
*iface
)
104 IQueryAssociationsImpl
*This
= (IQueryAssociationsImpl
*)iface
;
105 ULONG refCount
= InterlockedIncrement(&This
->ref
);
107 TRACE("(%p)->(ref before=%u)\n",This
, refCount
- 1);
112 /**************************************************************************
113 * IQueryAssociations_Release {SHLWAPI}
115 * See IUnknown_Release.
117 static ULONG WINAPI
IQueryAssociations_fnRelease(IQueryAssociations
*iface
)
119 IQueryAssociationsImpl
*This
= (IQueryAssociationsImpl
*)iface
;
120 ULONG refCount
= InterlockedDecrement(&This
->ref
);
122 TRACE("(%p)->(ref before=%u)\n",This
, refCount
+ 1);
126 TRACE("Destroying IQueryAssociations (%p)\n", This
);
127 RegCloseKey(This
->hkeySource
);
128 RegCloseKey(This
->hkeyProgID
);
129 HeapFree(GetProcessHeap(), 0, This
);
135 /**************************************************************************
136 * IQueryAssociations_Init {SHLWAPI}
138 * Initialise an IQueryAssociations object.
141 * iface [I] IQueryAssociations interface to initialise
142 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
143 * pszAssoc [I] String for the root key name, or NULL if hkeyProgid is given
144 * hkeyProgid [I] Handle for the root key, or NULL if pszAssoc is given
145 * hWnd [I] Reserved, must be NULL.
148 * Success: S_OK. iface is initialised with the parameters given.
149 * Failure: An HRESULT error code indicating the error.
151 static HRESULT WINAPI
IQueryAssociations_fnInit(
152 IQueryAssociations
*iface
,
158 static const WCHAR szProgID
[] = {'P','r','o','g','I','D',0};
159 IQueryAssociationsImpl
*This
= (IQueryAssociationsImpl
*)iface
;
162 TRACE("(%p)->(%d,%s,%p,%p)\n", iface
,
164 debugstr_w(pszAssoc
),
168 FIXME("hwnd != NULL not supported\n");
170 FIXME("unsupported flags: %x\n", cfFlags
);
171 if (pszAssoc
!= NULL
)
173 ret
= RegOpenKeyExW(HKEY_CLASSES_ROOT
,
178 if (ret
!= ERROR_SUCCESS
)
180 /* if this is not a prog id */
181 if ((*pszAssoc
== '.') || (*pszAssoc
== '{'))
183 RegOpenKeyExW(This
->hkeySource
,
190 This
->hkeyProgID
= This
->hkeySource
;
193 else if (hkeyProgid
!= NULL
)
195 This
->hkeyProgID
= hkeyProgid
;
202 static HRESULT
ASSOC_GetValue(HKEY hkey
, WCHAR
** pszText
)
208 ret
= RegQueryValueExW(hkey
, NULL
, 0, NULL
, NULL
, &len
);
209 if (ret
!= ERROR_SUCCESS
)
210 return HRESULT_FROM_WIN32(ret
);
213 *pszText
= HeapAlloc(GetProcessHeap(), 0, len
);
215 return E_OUTOFMEMORY
;
216 ret
= RegQueryValueExW(hkey
, NULL
, 0, NULL
, (LPBYTE
)*pszText
,
218 if (ret
!= ERROR_SUCCESS
)
220 HeapFree(GetProcessHeap(), 0, *pszText
);
221 return HRESULT_FROM_WIN32(ret
);
226 static HRESULT
ASSOC_GetCommand(IQueryAssociationsImpl
*This
,
227 LPCWSTR pszExtra
, WCHAR
**ppszCommand
)
235 WCHAR
* pszExtraFromReg
= NULL
;
237 static const WCHAR commandW
[] = { 'c','o','m','m','a','n','d',0 };
238 static const WCHAR shellW
[] = { 's','h','e','l','l',0 };
240 hr
= ASSOC_GetValue(This
->hkeySource
, &pszFileType
);
243 ret
= RegOpenKeyExW(HKEY_CLASSES_ROOT
, pszFileType
, 0, KEY_READ
, &hkeyFile
);
244 HeapFree(GetProcessHeap(), 0, pszFileType
);
245 if (ret
!= ERROR_SUCCESS
)
246 return HRESULT_FROM_WIN32(ret
);
248 ret
= RegOpenKeyExW(hkeyFile
, shellW
, 0, KEY_READ
, &hkeyShell
);
249 RegCloseKey(hkeyFile
);
250 if (ret
!= ERROR_SUCCESS
)
251 return HRESULT_FROM_WIN32(ret
);
255 hr
= ASSOC_GetValue(hkeyShell
, &pszExtraFromReg
);
256 /* if no default action */
257 if (hr
== E_FAIL
|| hr
== HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
))
260 ret
= RegQueryInfoKeyW(hkeyShell
, 0, 0, 0, 0, &rlen
, 0, 0, 0, 0, 0, 0);
261 if (ret
!= ERROR_SUCCESS
)
263 RegCloseKey(hkeyShell
);
264 return HRESULT_FROM_WIN32(ret
);
267 pszExtraFromReg
= HeapAlloc(GetProcessHeap(), 0, rlen
* sizeof(WCHAR
));
268 if (!pszExtraFromReg
)
270 RegCloseKey(hkeyShell
);
271 return E_OUTOFMEMORY
;
273 ret
= RegEnumKeyExW(hkeyShell
, 0, pszExtraFromReg
, &rlen
, 0, NULL
, NULL
, NULL
);
274 if (ret
!= ERROR_SUCCESS
)
276 RegCloseKey(hkeyShell
);
277 return HRESULT_FROM_WIN32(ret
);
282 RegCloseKey(hkeyShell
);
287 ret
= RegOpenKeyExW(hkeyShell
, pszExtra
? pszExtra
: pszExtraFromReg
, 0,
288 KEY_READ
, &hkeyVerb
);
289 HeapFree(GetProcessHeap(), 0, pszExtraFromReg
);
290 RegCloseKey(hkeyShell
);
291 if (ret
!= ERROR_SUCCESS
)
292 return HRESULT_FROM_WIN32(ret
);
294 ret
= RegOpenKeyExW(hkeyVerb
, commandW
, 0, KEY_READ
, &hkeyCommand
);
295 RegCloseKey(hkeyVerb
);
296 if (ret
!= ERROR_SUCCESS
)
297 return HRESULT_FROM_WIN32(ret
);
298 hr
= ASSOC_GetValue(hkeyCommand
, ppszCommand
);
299 RegCloseKey(hkeyCommand
);
303 static HRESULT
ASSOC_GetExecutable(IQueryAssociationsImpl
*This
,
304 LPCWSTR pszExtra
, LPWSTR path
,
305 DWORD pathlen
, DWORD
*len
)
314 hr
= ASSOC_GetCommand(This
, pszExtra
, &pszCommand
);
318 /* cleanup pszCommand */
319 if (pszCommand
[0] == '"')
321 pszStart
= pszCommand
+ 1;
322 pszEnd
= strchrW(pszStart
, '"');
325 *len
= SearchPathW(NULL
, pszStart
, NULL
, pathlen
, path
, NULL
);
329 pszStart
= pszCommand
;
330 for (pszEnd
= pszStart
; (pszEnd
= strchrW(pszEnd
, ' ')); pszEnd
++)
334 if ((*len
= SearchPathW(NULL
, pszStart
, NULL
, pathlen
, path
, NULL
)))
339 *len
= SearchPathW(NULL
, pszStart
, NULL
, pathlen
, path
, NULL
);
342 HeapFree(GetProcessHeap(), 0, pszCommand
);
344 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
348 static HRESULT
ASSOC_ReturnData(LPWSTR out
, DWORD
*outlen
, LPCWSTR data
,
355 if (*outlen
< datalen
)
361 lstrcpynW(out
, data
, datalen
);
371 /**************************************************************************
372 * IQueryAssociations_GetString {SHLWAPI}
374 * Get a file association string from the registry.
377 * iface [I] IQueryAssociations interface to query
378 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
379 * str [I] Type of string to get (ASSOCSTR enum from "shlwapi.h")
380 * pszExtra [I] Extra information about the string location
381 * pszOut [O] Destination for the association string
382 * pcchOut [I/O] Length of pszOut
385 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
386 * Failure: An HRESULT error code indicating the error.
388 static HRESULT WINAPI
IQueryAssociations_fnGetString(
389 IQueryAssociations
*iface
,
396 IQueryAssociationsImpl
*This
= (IQueryAssociationsImpl
*)iface
;
397 const ASSOCF cfUnimplemented
= ~(0);
400 WCHAR path
[MAX_PATH
];
402 TRACE("(%p,0x%8x,0x%8x,%s,%p,%p)\n", This
, cfFlags
, str
,
403 debugstr_w(pszExtra
), pszOut
, pcchOut
);
405 if (cfFlags
& cfUnimplemented
)
406 FIXME("%08x: unimplemented flags!\n", cfFlags
& cfUnimplemented
);
413 case ASSOCSTR_COMMAND
:
416 hr
= ASSOC_GetCommand(This
, pszExtra
, &command
);
419 hr
= ASSOC_ReturnData(pszOut
, pcchOut
, command
, strlenW(command
) + 1);
420 HeapFree(GetProcessHeap(), 0, command
);
425 case ASSOCSTR_EXECUTABLE
:
427 hr
= ASSOC_GetExecutable(This
, pszExtra
, path
, MAX_PATH
, &len
);
431 return ASSOC_ReturnData(pszOut
, pcchOut
, path
, len
);
434 case ASSOCSTR_FRIENDLYDOCNAME
:
440 hr
= ASSOC_GetValue(This
->hkeySource
, &pszFileType
);
444 ret
= RegGetValueW(HKEY_CLASSES_ROOT
, pszFileType
, NULL
, RRF_RT_REG_SZ
, NULL
, NULL
, &size
);
445 if (ret
== ERROR_SUCCESS
)
447 WCHAR
*docName
= HeapAlloc(GetProcessHeap(), 0, size
);
450 ret
= RegGetValueW(HKEY_CLASSES_ROOT
, pszFileType
, NULL
, RRF_RT_REG_SZ
, NULL
, docName
, &size
);
451 if (ret
== ERROR_SUCCESS
)
452 hr
= ASSOC_ReturnData(pszOut
, pcchOut
, docName
, strlenW(docName
) + 1);
454 hr
= HRESULT_FROM_WIN32(ret
);
455 HeapFree(GetProcessHeap(), 0, docName
);
461 hr
= HRESULT_FROM_WIN32(ret
);
462 HeapFree(GetProcessHeap(), 0, pszFileType
);
466 case ASSOCSTR_FRIENDLYAPPNAME
:
468 PVOID verinfoW
= NULL
;
469 DWORD size
, retval
= 0;
472 static const WCHAR translationW
[] = {
473 '\\','V','a','r','F','i','l','e','I','n','f','o',
474 '\\','T','r','a','n','s','l','a','t','i','o','n',0
476 static const WCHAR fileDescFmtW
[] = {
477 '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
478 '\\','%','0','4','x','%','0','4','x',
479 '\\','F','i','l','e','D','e','s','c','r','i','p','t','i','o','n',0
483 hr
= ASSOC_GetExecutable(This
, pszExtra
, path
, MAX_PATH
, &len
);
487 retval
= GetFileVersionInfoSizeW(path
, &size
);
489 goto get_friendly_name_fail
;
490 verinfoW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, retval
);
492 return E_OUTOFMEMORY
;
493 if (!GetFileVersionInfoW(path
, 0, retval
, verinfoW
))
494 goto get_friendly_name_fail
;
495 if (VerQueryValueW(verinfoW
, translationW
, (LPVOID
*)&bufW
, &flen
))
498 DWORD
*langCodeDesc
= (DWORD
*)bufW
;
499 for (i
= 0; i
< flen
/ sizeof(DWORD
); i
++)
501 sprintfW(fileDescW
, fileDescFmtW
, LOWORD(langCodeDesc
[i
]),
502 HIWORD(langCodeDesc
[i
]));
503 if (VerQueryValueW(verinfoW
, fileDescW
, (LPVOID
*)&bufW
, &flen
))
505 /* Does strlenW(bufW) == 0 mean we use the filename? */
506 len
= strlenW(bufW
) + 1;
507 TRACE("found FileDescription: %s\n", debugstr_w(bufW
));
508 hr
= ASSOC_ReturnData(pszOut
, pcchOut
, bufW
, len
);
509 HeapFree(GetProcessHeap(), 0, verinfoW
);
514 get_friendly_name_fail
:
515 PathRemoveExtensionW(path
);
516 PathStripPathW(path
);
517 TRACE("using filename: %s\n", debugstr_w(path
));
518 hr
= ASSOC_ReturnData(pszOut
, pcchOut
, path
, strlenW(path
) + 1);
519 HeapFree(GetProcessHeap(), 0, verinfoW
);
523 case ASSOCSTR_CONTENTTYPE
:
525 static const WCHAR Content_TypeW
[] = {'C','o','n','t','e','n','t',' ','T','y','p','e',0};
531 ret
= RegGetValueW(This
->hkeySource
, NULL
, Content_TypeW
, RRF_RT_REG_SZ
, NULL
, NULL
, &size
);
532 if (ret
!= ERROR_SUCCESS
)
533 return HRESULT_FROM_WIN32(ret
);
534 contentType
= HeapAlloc(GetProcessHeap(), 0, size
);
535 if (contentType
!= NULL
)
537 ret
= RegGetValueW(This
->hkeySource
, NULL
, Content_TypeW
, RRF_RT_REG_SZ
, NULL
, contentType
, &size
);
538 if (ret
== ERROR_SUCCESS
)
539 hr
= ASSOC_ReturnData(pszOut
, pcchOut
, contentType
, strlenW(contentType
) + 1);
541 hr
= HRESULT_FROM_WIN32(ret
);
542 HeapFree(GetProcessHeap(), 0, contentType
);
549 case ASSOCSTR_DEFAULTICON
:
551 static const WCHAR DefaultIconW
[] = {'D','e','f','a','u','l','t','I','c','o','n',0};
557 hr
= ASSOC_GetValue(This
->hkeySource
, &pszFileType
);
560 ret
= RegOpenKeyExW(HKEY_CLASSES_ROOT
, pszFileType
, 0, KEY_READ
, &hkeyFile
);
561 if (ret
== ERROR_SUCCESS
)
564 ret
= RegGetValueW(hkeyFile
, DefaultIconW
, NULL
, RRF_RT_REG_SZ
, NULL
, NULL
, &size
);
565 if (ret
== ERROR_SUCCESS
)
567 WCHAR
*icon
= HeapAlloc(GetProcessHeap(), 0, size
);
570 ret
= RegGetValueW(hkeyFile
, DefaultIconW
, NULL
, RRF_RT_REG_SZ
, NULL
, icon
, &size
);
571 if (ret
== ERROR_SUCCESS
)
572 hr
= ASSOC_ReturnData(pszOut
, pcchOut
, icon
, strlenW(icon
) + 1);
574 hr
= HRESULT_FROM_WIN32(ret
);
575 HeapFree(GetProcessHeap(), 0, icon
);
581 hr
= HRESULT_FROM_WIN32(ret
);
582 RegCloseKey(hkeyFile
);
585 hr
= HRESULT_FROM_WIN32(ret
);
586 HeapFree(GetProcessHeap(), 0, pszFileType
);
591 FIXME("assocstr %d unimplemented!\n", str
);
596 /**************************************************************************
597 * IQueryAssociations_GetKey {SHLWAPI}
599 * Get a file association key from the registry.
602 * iface [I] IQueryAssociations interface to query
603 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
604 * assockey [I] Type of key to get (ASSOCKEY enum from "shlwapi.h")
605 * pszExtra [I] Extra information about the key location
606 * phkeyOut [O] Destination for the association key
609 * Success: S_OK. phkeyOut contains a handle to the key.
610 * Failure: An HRESULT error code indicating the error.
612 static HRESULT WINAPI
IQueryAssociations_fnGetKey(
613 IQueryAssociations
*iface
,
619 IQueryAssociationsImpl
*This
= (IQueryAssociationsImpl
*)iface
;
621 FIXME("(%p,0x%8x,0x%8x,%s,%p)-stub!\n", This
, cfFlags
, assockey
,
622 debugstr_w(pszExtra
), phkeyOut
);
626 /**************************************************************************
627 * IQueryAssociations_GetData {SHLWAPI}
629 * Get the data for a file association key from the registry.
632 * iface [I] IQueryAssociations interface to query
633 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
634 * assocdata [I] Type of data to get (ASSOCDATA enum from "shlwapi.h")
635 * pszExtra [I] Extra information about the data location
636 * pvOut [O] Destination for the association key
637 * pcbOut [I/O] Size of pvOut
640 * Success: S_OK. pszOut contains the data, pcbOut contains its length.
641 * Failure: An HRESULT error code indicating the error.
643 static HRESULT WINAPI
IQueryAssociations_fnGetData(
644 IQueryAssociations
*iface
,
651 IQueryAssociationsImpl
*This
= (IQueryAssociationsImpl
*)iface
;
653 FIXME("(%p,0x%8x,0x%8x,%s,%p,%p)-stub!\n", This
, cfFlags
, assocdata
,
654 debugstr_w(pszExtra
), pvOut
, pcbOut
);
658 /**************************************************************************
659 * IQueryAssociations_GetEnum {SHLWAPI}
661 * Not yet implemented in native Win32.
664 * iface [I] IQueryAssociations interface to query
665 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
666 * assocenum [I] Type of enum to get (ASSOCENUM enum from "shlwapi.h")
667 * pszExtra [I] Extra information about the enum location
668 * riid [I] REFIID to look for
669 * ppvOut [O] Destination for the interface.
673 * Failure: An HRESULT error code indicating the error.
676 * Presumably this function returns an enumerator object.
678 static HRESULT WINAPI
IQueryAssociations_fnGetEnum(
679 IQueryAssociations
*iface
,
686 IQueryAssociationsImpl
*This
= (IQueryAssociationsImpl
*)iface
;
688 FIXME("(%p,0x%8x,0x%8x,%s,%s,%p)-stub!\n", This
, cfFlags
, assocenum
,
689 debugstr_w(pszExtra
), debugstr_guid(riid
), ppvOut
);
693 static const IQueryAssociationsVtbl IQueryAssociations_vtbl
=
695 IQueryAssociations_fnQueryInterface
,
696 IQueryAssociations_fnAddRef
,
697 IQueryAssociations_fnRelease
,
698 IQueryAssociations_fnInit
,
699 IQueryAssociations_fnGetString
,
700 IQueryAssociations_fnGetKey
,
701 IQueryAssociations_fnGetData
,
702 IQueryAssociations_fnGetEnum
705 /**************************************************************************
706 * IQueryAssociations_Constructor [internal]
708 * Construct a new IQueryAssociations object.
710 HRESULT WINAPI
QueryAssociations_Constructor(IUnknown
*pUnkOuter
, REFIID riid
, LPVOID
*ppOutput
)
712 IQueryAssociationsImpl
* this;
715 if (pUnkOuter
) return CLASS_E_NOAGGREGATION
;
717 if (!(this = SHAlloc(sizeof(*this)))) return E_OUTOFMEMORY
;
718 this->lpVtbl
= &IQueryAssociations_vtbl
;
720 this->hkeySource
= 0;
721 this->hkeyProgID
= 0;
722 if (FAILED(ret
= IUnknown_QueryInterface((IUnknown
*)this, riid
, ppOutput
))) SHFree( this );
723 TRACE("returning %p\n", *ppOutput
);