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
23 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
25 /**************************************************************************
29 * This object provides a layer of abstraction over the system registry in
30 * order to simplify the process of parsing associations between files.
31 * Associations in this context means the registry entries that link (for
32 * example) the extension of a file with its description, list of
33 * applications to open the file with, and actions that can be performed on it
34 * (the shell displays such information in the context menu of explorer
35 * when you right-click on a file).
38 * You can use this object transparently by calling the helper functions
39 * AssocQueryKeyA(), AssocQueryStringA() and AssocQueryStringByKeyA(). These
40 * create an IQueryAssociations object, perform the requested actions
41 * and then dispose of the object. Alternatively, you can create an instance
42 * of the object using AssocCreate() and call the following methods on it:
47 CQueryAssociations::CQueryAssociations() : hkeySource(0), hkeyProgID(0)
51 CQueryAssociations::~CQueryAssociations()
55 /**************************************************************************
56 * IQueryAssociations_Init
58 * Initialise an IQueryAssociations object.
61 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
62 * pszAssoc [I] String for the root key name, or NULL if hkeyProgid is given
63 * hkeyProgid [I] Handle for the root key, or NULL if pszAssoc is given
64 * hWnd [I] Reserved, must be NULL.
67 * Success: S_OK. iface is initialised with the parameters given.
68 * Failure: An HRESULT error code indicating the error.
70 HRESULT STDMETHODCALLTYPE
CQueryAssociations::Init(
76 static const WCHAR szProgID
[] = L
"ProgID";
78 TRACE("(%p)->(%d,%s,%p,%p)\n", this,
86 FIXME("hwnd != NULL not supported\n");
91 FIXME("unsupported flags: %x\n", cfFlags
);
94 RegCloseKey(this->hkeySource
);
95 RegCloseKey(this->hkeyProgID
);
96 this->hkeySource
= this->hkeyProgID
= NULL
;
102 LONG ret
= RegOpenKeyExW(HKEY_CLASSES_ROOT
,
112 /* if this is a progid */
113 if (*pszAssoc
!= '.' && *pszAssoc
!= '{')
115 this->hkeyProgID
= this->hkeySource
;
119 /* if it's not a progid, it's a file extension or clsid */
120 if (*pszAssoc
== '.')
122 /* for a file extension, the progid is the default value */
123 hr
= this->GetValue(this->hkeySource
, NULL
, (void**)&progId
, NULL
);
127 else /* if (*pszAssoc == '{') */
130 /* for a clsid, the progid is the default value of the ProgID subkey */
131 ret
= RegOpenKeyExW(this->hkeySource
,
136 if (ret
!= ERROR_SUCCESS
)
138 hr
= this->GetValue(progIdKey
, NULL
, (void**)&progId
, NULL
);
141 RegCloseKey(progIdKey
);
144 /* open the actual progid key, the one with the shell subkey */
145 ret
= RegOpenKeyExW(HKEY_CLASSES_ROOT
,
150 HeapFree(GetProcessHeap(), 0, progId
);
154 else if (hkeyProgid
!= NULL
)
156 /* reopen the key so we don't end up closing a key owned by the caller */
157 RegOpenKeyExW(hkeyProgid
, NULL
, 0, KEY_READ
, &this->hkeyProgID
);
158 this->hkeySource
= this->hkeyProgID
;
165 /**************************************************************************
166 * IQueryAssociations_GetString
168 * Get a file association string from the registry.
171 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
172 * str [I] Type of string to get (ASSOCSTR enum from "shlwapi.h")
173 * pszExtra [I] Extra information about the string location
174 * pszOut [O] Destination for the association string
175 * pcchOut [I/O] Length of pszOut
178 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
179 * Failure: An HRESULT error code indicating the error.
181 HRESULT STDMETHODCALLTYPE
CQueryAssociations::GetString(
188 const ASSOCF unimplemented_flags
= ~ASSOCF_NOTRUNCATE
;
191 WCHAR path
[MAX_PATH
];
193 TRACE("(%p)->(0x%08x, %u, %s, %p, %p)\n", this, flags
, str
, debugstr_w(pszExtra
), pszOut
, pcchOut
);
194 if (flags
& unimplemented_flags
)
196 FIXME("%08x: unimplemented flags\n", flags
& unimplemented_flags
);
204 if (!this->hkeySource
&& !this->hkeyProgID
)
206 return HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION
);
211 case ASSOCSTR_COMMAND
:
214 hr
= this->GetCommand(pszExtra
, &command
);
217 hr
= this->ReturnString(flags
, pszOut
, pcchOut
, command
, strlenW(command
) + 1);
218 HeapFree(GetProcessHeap(), 0, command
);
222 case ASSOCSTR_EXECUTABLE
:
224 hr
= this->GetExecutable(pszExtra
, path
, MAX_PATH
, &len
);
230 return this->ReturnString(flags
, pszOut
, pcchOut
, path
, len
);
232 case ASSOCSTR_FRIENDLYDOCNAME
:
236 hr
= this->GetValue(this->hkeySource
, NULL
, (void**)&pszFileType
, NULL
);
242 DWORD ret
= RegGetValueW(HKEY_CLASSES_ROOT
, pszFileType
, NULL
, RRF_RT_REG_SZ
, NULL
, NULL
, &size
);
243 if (ret
== ERROR_SUCCESS
)
245 WCHAR
*docName
= static_cast<WCHAR
*>(HeapAlloc(GetProcessHeap(), 0, size
));
248 ret
= RegGetValueW(HKEY_CLASSES_ROOT
, pszFileType
, NULL
, RRF_RT_REG_SZ
, NULL
, docName
, &size
);
249 if (ret
== ERROR_SUCCESS
)
251 hr
= this->ReturnString(flags
, pszOut
, pcchOut
, docName
, strlenW(docName
) + 1);
255 hr
= HRESULT_FROM_WIN32(ret
);
257 HeapFree(GetProcessHeap(), 0, docName
);
266 hr
= HRESULT_FROM_WIN32(ret
);
268 HeapFree(GetProcessHeap(), 0, pszFileType
);
271 case ASSOCSTR_FRIENDLYAPPNAME
:
273 PVOID verinfoW
= NULL
;
274 DWORD size
, retval
= 0;
277 static const WCHAR translationW
[] = L
"\\VarFileInfo\\Translation";
278 static const WCHAR fileDescFmtW
[] = L
"\\StringFileInfo\\%04x%04x\\FileDescription";
281 hr
= this->GetExecutable(pszExtra
, path
, MAX_PATH
, &len
);
286 retval
= GetFileVersionInfoSizeW(path
, &size
);
289 goto get_friendly_name_fail
;
291 verinfoW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, retval
);
294 return E_OUTOFMEMORY
;
296 if (!GetFileVersionInfoW(path
, 0, retval
, verinfoW
))
298 goto get_friendly_name_fail
;
300 if (VerQueryValueW(verinfoW
, translationW
, (LPVOID
*)&bufW
, &flen
))
303 DWORD
*langCodeDesc
= (DWORD
*)bufW
;
304 for (i
= 0; i
< flen
/ sizeof(DWORD
); i
++)
306 sprintfW(fileDescW
, fileDescFmtW
, LOWORD(langCodeDesc
[i
]), HIWORD(langCodeDesc
[i
]));
307 if (VerQueryValueW(verinfoW
, fileDescW
, (LPVOID
*)&bufW
, &flen
))
309 /* Does strlenW(bufW) == 0 mean we use the filename? */
310 len
= strlenW(bufW
) + 1;
311 TRACE("found FileDescription: %s\n", debugstr_w(bufW
));
312 hr
= this->ReturnString(flags
, pszOut
, pcchOut
, bufW
, len
);
313 HeapFree(GetProcessHeap(), 0, verinfoW
);
318 get_friendly_name_fail
:
319 PathRemoveExtensionW(path
);
320 PathStripPathW(path
);
321 TRACE("using filename: %s\n", debugstr_w(path
));
322 hr
= this->ReturnString(flags
, pszOut
, pcchOut
, path
, strlenW(path
) + 1);
323 HeapFree(GetProcessHeap(), 0, verinfoW
);
326 case ASSOCSTR_CONTENTTYPE
:
328 static const WCHAR Content_TypeW
[] = L
"Content Type";
331 DWORD ret
= RegGetValueW(this->hkeySource
, NULL
, Content_TypeW
, RRF_RT_REG_SZ
, NULL
, NULL
, &size
);
332 if (ret
!= ERROR_SUCCESS
)
334 return HRESULT_FROM_WIN32(ret
);
336 WCHAR
*contentType
= static_cast<WCHAR
*>(HeapAlloc(GetProcessHeap(), 0, size
));
337 if (contentType
!= NULL
)
339 ret
= RegGetValueW(this->hkeySource
, NULL
, Content_TypeW
, RRF_RT_REG_SZ
, NULL
, contentType
, &size
);
340 if (ret
== ERROR_SUCCESS
)
342 hr
= this->ReturnString(flags
, pszOut
, pcchOut
, contentType
, strlenW(contentType
) + 1);
346 hr
= HRESULT_FROM_WIN32(ret
);
348 HeapFree(GetProcessHeap(), 0, contentType
);
356 case ASSOCSTR_DEFAULTICON
:
358 static const WCHAR DefaultIconW
[] = L
"DefaultIcon";
361 ret
= RegGetValueW(this->hkeyProgID
, DefaultIconW
, NULL
, RRF_RT_REG_SZ
, NULL
, NULL
, &size
);
362 if (ret
== ERROR_SUCCESS
)
364 WCHAR
*icon
= static_cast<WCHAR
*>(HeapAlloc(GetProcessHeap(), 0, size
));
367 ret
= RegGetValueW(this->hkeyProgID
, DefaultIconW
, NULL
, RRF_RT_REG_SZ
, NULL
, icon
, &size
);
368 if (ret
== ERROR_SUCCESS
)
370 hr
= this->ReturnString(flags
, pszOut
, pcchOut
, icon
, strlenW(icon
) + 1);
374 hr
= HRESULT_FROM_WIN32(ret
);
376 HeapFree(GetProcessHeap(), 0, icon
);
380 hr
= HRESULT_FROM_WIN32(ret
);
385 hr
= HRESULT_FROM_WIN32(ret
);
389 case ASSOCSTR_SHELLEXTENSION
:
391 static const WCHAR shellexW
[] = L
"ShellEx\\";
392 WCHAR keypath
[sizeof(shellexW
) / sizeof(shellexW
[0]) + 39], guid
[39];
396 hr
= CLSIDFromString(pszExtra
, &clsid
);
401 strcpyW(keypath
, shellexW
);
402 strcatW(keypath
, pszExtra
);
403 LONG ret
= RegOpenKeyExW(this->hkeySource
, keypath
, 0, KEY_READ
, &hkey
);
406 return HRESULT_FROM_WIN32(ret
);
408 DWORD size
= sizeof(guid
);
409 ret
= RegGetValueW(hkey
, NULL
, NULL
, RRF_RT_REG_SZ
, NULL
, guid
, &size
);
413 return HRESULT_FROM_WIN32(ret
);
415 return this->ReturnString(flags
, pszOut
, pcchOut
, guid
, size
/ sizeof(WCHAR
));
420 FIXME("assocstr %d unimplemented!\n", str
);
426 /**************************************************************************
427 * IQueryAssociations_GetKey
429 * Get a file association key from the registry.
432 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
433 * assockey [I] Type of key to get (ASSOCKEY enum from "shlwapi.h")
434 * pszExtra [I] Extra information about the key location
435 * phkeyOut [O] Destination for the association key
438 * Success: S_OK. phkeyOut contains a handle to the key.
439 * Failure: An HRESULT error code indicating the error.
441 HRESULT STDMETHODCALLTYPE
CQueryAssociations::GetKey(
447 FIXME("(%p,0x%8x,0x%8x,%s,%p)-stub!\n", this, cfFlags
, assockey
,
448 debugstr_w(pszExtra
), phkeyOut
);
452 /**************************************************************************
453 * IQueryAssociations_GetData
455 * Get the data for a file association key from the registry.
458 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
459 * assocdata [I] Type of data to get (ASSOCDATA enum from "shlwapi.h")
460 * pszExtra [I] Extra information about the data location
461 * pvOut [O] Destination for the association key
462 * pcbOut [I/O] Size of pvOut
465 * Success: S_OK. pszOut contains the data, pcbOut contains its length.
466 * Failure: An HRESULT error code indicating the error.
468 HRESULT STDMETHODCALLTYPE
CQueryAssociations::GetData(ASSOCF cfFlags
, ASSOCDATA assocdata
, LPCWSTR pszExtra
, LPVOID pvOut
, DWORD
*pcbOut
)
470 static const WCHAR edit_flags
[] = L
"EditFlags";
472 TRACE("(%p,0x%8x,0x%8x,%s,%p,%p)\n", this, cfFlags
, assocdata
,
473 debugstr_w(pszExtra
), pvOut
, pcbOut
);
477 FIXME("Unsupported flags: %x\n", cfFlags
);
482 case ASSOCDATA_EDITFLAGS
:
484 if(!this->hkeyProgID
)
486 return HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION
);
491 HRESULT hres
= this->GetValue(this->hkeyProgID
, edit_flags
, &data
, &size
);
499 HeapFree(GetProcessHeap(), 0, data
);
503 hres
= this->ReturnData(pvOut
, pcbOut
, data
, size
);
504 HeapFree(GetProcessHeap(), 0, data
);
509 FIXME("Unsupported ASSOCDATA value: %d\n", assocdata
);
515 /**************************************************************************
516 * IQueryAssociations_GetEnum
518 * Not yet implemented in native Win32.
521 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
522 * assocenum [I] Type of enum to get (ASSOCENUM enum from "shlwapi.h")
523 * pszExtra [I] Extra information about the enum location
524 * riid [I] REFIID to look for
525 * ppvOut [O] Destination for the interface.
529 * Failure: An HRESULT error code indicating the error.
532 * Presumably this function returns an enumerator object.
534 HRESULT STDMETHODCALLTYPE
CQueryAssociations::GetEnum(
544 HRESULT
CQueryAssociations::GetValue(HKEY hkey
, const WCHAR
*name
, void **data
, DWORD
*data_size
)
549 ret
= RegQueryValueExW(hkey
, name
, 0, NULL
, NULL
, &size
);
550 if (ret
!= ERROR_SUCCESS
)
552 return HRESULT_FROM_WIN32(ret
);
558 *data
= HeapAlloc(GetProcessHeap(), 0, size
);
561 return E_OUTOFMEMORY
;
563 ret
= RegQueryValueExW(hkey
, name
, 0, NULL
, (LPBYTE
)*data
, &size
);
564 if (ret
!= ERROR_SUCCESS
)
566 HeapFree(GetProcessHeap(), 0, *data
);
567 return HRESULT_FROM_WIN32(ret
);
576 HRESULT
CQueryAssociations::GetCommand(const WCHAR
*extra
, WCHAR
**command
)
583 WCHAR
*extra_from_reg
= NULL
;
585 static const WCHAR commandW
[] = L
"command";
586 static const WCHAR shellW
[] = L
"shell";
588 /* When looking for file extension it's possible to have a default value
589 that points to another key that contains 'shell/<verb>/command' subtree. */
590 hr
= this->GetValue(this->hkeySource
, NULL
, (void**)&filetype
, NULL
);
595 ret
= RegOpenKeyExW(HKEY_CLASSES_ROOT
, filetype
, 0, KEY_READ
, &hkeyFile
);
596 HeapFree(GetProcessHeap(), 0, filetype
);
598 if (ret
== ERROR_SUCCESS
)
600 ret
= RegOpenKeyExW(hkeyFile
, shellW
, 0, KEY_READ
, &hkeyShell
);
601 RegCloseKey(hkeyFile
);
605 ret
= RegOpenKeyExW(this->hkeySource
, shellW
, 0, KEY_READ
, &hkeyShell
);
610 ret
= RegOpenKeyExW(this->hkeySource
, shellW
, 0, KEY_READ
, &hkeyShell
);
615 return HRESULT_FROM_WIN32(ret
);
620 /* check for default verb */
621 hr
= this->GetValue(hkeyShell
, NULL
, (void**)&extra_from_reg
, NULL
);
624 /* no default verb, try first subkey */
625 DWORD max_subkey_len
;
627 ret
= RegQueryInfoKeyW(hkeyShell
, NULL
, NULL
, NULL
, NULL
, &max_subkey_len
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
630 RegCloseKey(hkeyShell
);
631 return HRESULT_FROM_WIN32(ret
);
635 extra_from_reg
= static_cast<WCHAR
*>(HeapAlloc(GetProcessHeap(), 0, max_subkey_len
* sizeof(WCHAR
)));
638 RegCloseKey(hkeyShell
);
639 return E_OUTOFMEMORY
;
642 ret
= RegEnumKeyExW(hkeyShell
, 0, extra_from_reg
, &max_subkey_len
, NULL
, NULL
, NULL
, NULL
);
645 HeapFree(GetProcessHeap(), 0, extra_from_reg
);
646 RegCloseKey(hkeyShell
);
647 return HRESULT_FROM_WIN32(ret
);
650 extra
= extra_from_reg
;
653 /* open verb subkey */
654 ret
= RegOpenKeyExW(hkeyShell
, extra
, 0, KEY_READ
, &hkeyVerb
);
655 HeapFree(GetProcessHeap(), 0, extra_from_reg
);
656 RegCloseKey(hkeyShell
);
659 return HRESULT_FROM_WIN32(ret
);
661 /* open command subkey */
662 ret
= RegOpenKeyExW(hkeyVerb
, commandW
, 0, KEY_READ
, &hkeyCommand
);
663 RegCloseKey(hkeyVerb
);
666 return HRESULT_FROM_WIN32(ret
);
668 hr
= this->GetValue(hkeyCommand
, NULL
, (void**)command
, NULL
);
669 RegCloseKey(hkeyCommand
);
673 HRESULT
CQueryAssociations::GetExecutable(LPCWSTR pszExtra
, LPWSTR path
, DWORD pathlen
, DWORD
*len
)
679 HRESULT hr
= this->GetCommand(pszExtra
, &pszCommand
);
685 DWORD expLen
= ExpandEnvironmentStringsW(pszCommand
, NULL
, 0);
689 WCHAR
*buf
= static_cast<WCHAR
*>(HeapAlloc(GetProcessHeap(), 0, expLen
* sizeof(WCHAR
)));
690 ExpandEnvironmentStringsW(pszCommand
, buf
, expLen
);
691 HeapFree(GetProcessHeap(), 0, pszCommand
);
695 /* cleanup pszCommand */
696 if (pszCommand
[0] == '"')
698 pszStart
= pszCommand
+ 1;
699 pszEnd
= strchrW(pszStart
, '"');
704 *len
= SearchPathW(NULL
, pszStart
, NULL
, pathlen
, path
, NULL
);
708 pszStart
= pszCommand
;
709 for (pszEnd
= pszStart
; (pszEnd
= strchrW(pszEnd
, ' ')); pszEnd
++)
713 if ((*len
= SearchPathW(NULL
, pszStart
, NULL
, pathlen
, path
, NULL
)))
721 *len
= SearchPathW(NULL
, pszStart
, NULL
, pathlen
, path
, NULL
);
725 HeapFree(GetProcessHeap(), 0, pszCommand
);
728 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
733 HRESULT
CQueryAssociations::ReturnData(void *out
, DWORD
*outlen
, const void *data
, DWORD datalen
)
737 if (*outlen
< datalen
)
743 memcpy(out
, data
, datalen
);
753 HRESULT
CQueryAssociations::ReturnString(ASSOCF flags
, LPWSTR out
, DWORD
*outlen
, LPCWSTR data
, DWORD datalen
)
758 TRACE("flags=0x%08x, data=%s\n", flags
, debugstr_w(data
));
766 if (*outlen
< datalen
)
768 if (flags
& ASSOCF_NOTRUNCATE
)
771 if (*outlen
> 0) out
[0] = 0;
776 len
= min(*outlen
, datalen
);
777 hr
= HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER
);
788 memcpy(out
, data
, len
*sizeof(WCHAR
));