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
;
99 LONG ret
= RegOpenKeyExW(HKEY_CLASSES_ROOT
,
108 /* if this is not a prog id */
109 if ((*pszAssoc
== '.') || (*pszAssoc
== '{'))
111 RegOpenKeyExW(this->hkeySource
,
119 this->hkeyProgID
= this->hkeySource
;
124 else if (hkeyProgid
!= NULL
)
126 this->hkeyProgID
= hkeyProgid
;
133 /**************************************************************************
134 * IQueryAssociations_GetString
136 * Get a file association string from the registry.
139 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
140 * str [I] Type of string to get (ASSOCSTR enum from "shlwapi.h")
141 * pszExtra [I] Extra information about the string location
142 * pszOut [O] Destination for the association string
143 * pcchOut [I/O] Length of pszOut
146 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
147 * Failure: An HRESULT error code indicating the error.
149 HRESULT STDMETHODCALLTYPE
CQueryAssociations::GetString(
156 const ASSOCF unimplemented_flags
= ~ASSOCF_NOTRUNCATE
;
159 WCHAR path
[MAX_PATH
];
161 TRACE("(%p)->(0x%08x, %u, %s, %p, %p)\n", this, flags
, str
, debugstr_w(pszExtra
), pszOut
, pcchOut
);
162 if (flags
& unimplemented_flags
)
164 FIXME("%08x: unimplemented flags\n", flags
& unimplemented_flags
);
172 if (!this->hkeySource
&& !this->hkeyProgID
)
174 return HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION
);
179 case ASSOCSTR_COMMAND
:
182 hr
= this->GetCommand(pszExtra
, &command
);
185 hr
= this->ReturnString(flags
, pszOut
, pcchOut
, command
, strlenW(command
) + 1);
186 HeapFree(GetProcessHeap(), 0, command
);
190 case ASSOCSTR_EXECUTABLE
:
192 hr
= this->GetExecutable(pszExtra
, path
, MAX_PATH
, &len
);
198 return this->ReturnString(flags
, pszOut
, pcchOut
, path
, len
);
200 case ASSOCSTR_FRIENDLYDOCNAME
:
204 hr
= this->GetValue(this->hkeySource
, NULL
, (void**)&pszFileType
, NULL
);
210 DWORD ret
= RegGetValueW(HKEY_CLASSES_ROOT
, pszFileType
, NULL
, RRF_RT_REG_SZ
, NULL
, NULL
, &size
);
211 if (ret
== ERROR_SUCCESS
)
213 WCHAR
*docName
= static_cast<WCHAR
*>(HeapAlloc(GetProcessHeap(), 0, size
));
216 ret
= RegGetValueW(HKEY_CLASSES_ROOT
, pszFileType
, NULL
, RRF_RT_REG_SZ
, NULL
, docName
, &size
);
217 if (ret
== ERROR_SUCCESS
)
219 hr
= this->ReturnString(flags
, pszOut
, pcchOut
, docName
, strlenW(docName
) + 1);
223 hr
= HRESULT_FROM_WIN32(ret
);
225 HeapFree(GetProcessHeap(), 0, docName
);
234 hr
= HRESULT_FROM_WIN32(ret
);
236 HeapFree(GetProcessHeap(), 0, pszFileType
);
239 case ASSOCSTR_FRIENDLYAPPNAME
:
241 PVOID verinfoW
= NULL
;
242 DWORD size
, retval
= 0;
245 static const WCHAR translationW
[] = L
"\\VarFileInfo\\Translation";
246 static const WCHAR fileDescFmtW
[] = L
"\\StringFileInfo\\%04x%04x\\FileDescription";
249 hr
= this->GetExecutable(pszExtra
, path
, MAX_PATH
, &len
);
254 retval
= GetFileVersionInfoSizeW(path
, &size
);
257 goto get_friendly_name_fail
;
259 verinfoW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, retval
);
262 return E_OUTOFMEMORY
;
264 if (!GetFileVersionInfoW(path
, 0, retval
, verinfoW
))
266 goto get_friendly_name_fail
;
268 if (VerQueryValueW(verinfoW
, translationW
, (LPVOID
*)&bufW
, &flen
))
271 DWORD
*langCodeDesc
= (DWORD
*)bufW
;
272 for (i
= 0; i
< flen
/ sizeof(DWORD
); i
++)
274 sprintfW(fileDescW
, fileDescFmtW
, LOWORD(langCodeDesc
[i
]), HIWORD(langCodeDesc
[i
]));
275 if (VerQueryValueW(verinfoW
, fileDescW
, (LPVOID
*)&bufW
, &flen
))
277 /* Does strlenW(bufW) == 0 mean we use the filename? */
278 len
= strlenW(bufW
) + 1;
279 TRACE("found FileDescription: %s\n", debugstr_w(bufW
));
280 hr
= this->ReturnString(flags
, pszOut
, pcchOut
, bufW
, len
);
281 HeapFree(GetProcessHeap(), 0, verinfoW
);
286 get_friendly_name_fail
:
287 PathRemoveExtensionW(path
);
288 PathStripPathW(path
);
289 TRACE("using filename: %s\n", debugstr_w(path
));
290 hr
= this->ReturnString(flags
, pszOut
, pcchOut
, path
, strlenW(path
) + 1);
291 HeapFree(GetProcessHeap(), 0, verinfoW
);
294 case ASSOCSTR_CONTENTTYPE
:
296 static const WCHAR Content_TypeW
[] = L
"Content Type";
299 DWORD ret
= RegGetValueW(this->hkeySource
, NULL
, Content_TypeW
, RRF_RT_REG_SZ
, NULL
, NULL
, &size
);
300 if (ret
!= ERROR_SUCCESS
)
302 return HRESULT_FROM_WIN32(ret
);
304 WCHAR
*contentType
= static_cast<WCHAR
*>(HeapAlloc(GetProcessHeap(), 0, size
));
305 if (contentType
!= NULL
)
307 ret
= RegGetValueW(this->hkeySource
, NULL
, Content_TypeW
, RRF_RT_REG_SZ
, NULL
, contentType
, &size
);
308 if (ret
== ERROR_SUCCESS
)
310 hr
= this->ReturnString(flags
, pszOut
, pcchOut
, contentType
, strlenW(contentType
) + 1);
314 hr
= HRESULT_FROM_WIN32(ret
);
316 HeapFree(GetProcessHeap(), 0, contentType
);
324 case ASSOCSTR_DEFAULTICON
:
326 static const WCHAR DefaultIconW
[] = L
"DefaultIcon";
330 hr
= this->GetValue(this->hkeySource
, NULL
, (void**)&pszFileType
, NULL
);
335 DWORD ret
= RegOpenKeyExW(HKEY_CLASSES_ROOT
, pszFileType
, 0, KEY_READ
, &hkeyFile
);
336 if (ret
== ERROR_SUCCESS
)
339 ret
= RegGetValueW(hkeyFile
, DefaultIconW
, NULL
, RRF_RT_REG_SZ
, NULL
, NULL
, &size
);
340 if (ret
== ERROR_SUCCESS
)
342 WCHAR
*icon
= static_cast<WCHAR
*>(HeapAlloc(GetProcessHeap(), 0, size
));
345 ret
= RegGetValueW(hkeyFile
, DefaultIconW
, NULL
, RRF_RT_REG_SZ
, NULL
, icon
, &size
);
346 if (ret
== ERROR_SUCCESS
)
348 hr
= this->ReturnString(flags
, pszOut
, pcchOut
, icon
, strlenW(icon
) + 1);
352 hr
= HRESULT_FROM_WIN32(ret
);
354 HeapFree(GetProcessHeap(), 0, icon
);
363 hr
= HRESULT_FROM_WIN32(ret
);
365 RegCloseKey(hkeyFile
);
369 hr
= HRESULT_FROM_WIN32(ret
);
371 HeapFree(GetProcessHeap(), 0, pszFileType
);
374 case ASSOCSTR_SHELLEXTENSION
:
376 static const WCHAR shellexW
[] = L
"ShellEx\\";
377 WCHAR keypath
[sizeof(shellexW
) / sizeof(shellexW
[0]) + 39], guid
[39];
381 hr
= CLSIDFromString(pszExtra
, &clsid
);
386 strcpyW(keypath
, shellexW
);
387 strcatW(keypath
, pszExtra
);
388 LONG ret
= RegOpenKeyExW(this->hkeySource
, keypath
, 0, KEY_READ
, &hkey
);
391 return HRESULT_FROM_WIN32(ret
);
393 DWORD size
= sizeof(guid
);
394 ret
= RegGetValueW(hkey
, NULL
, NULL
, RRF_RT_REG_SZ
, NULL
, guid
, &size
);
398 return HRESULT_FROM_WIN32(ret
);
400 return this->ReturnString(flags
, pszOut
, pcchOut
, guid
, size
/ sizeof(WCHAR
));
405 FIXME("assocstr %d unimplemented!\n", str
);
411 /**************************************************************************
412 * IQueryAssociations_GetKey
414 * Get a file association key from the registry.
417 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
418 * assockey [I] Type of key to get (ASSOCKEY enum from "shlwapi.h")
419 * pszExtra [I] Extra information about the key location
420 * phkeyOut [O] Destination for the association key
423 * Success: S_OK. phkeyOut contains a handle to the key.
424 * Failure: An HRESULT error code indicating the error.
426 HRESULT STDMETHODCALLTYPE
CQueryAssociations::GetKey(
432 FIXME("(%p,0x%8x,0x%8x,%s,%p)-stub!\n", this, cfFlags
, assockey
,
433 debugstr_w(pszExtra
), phkeyOut
);
437 /**************************************************************************
438 * IQueryAssociations_GetData
440 * Get the data for a file association key from the registry.
443 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
444 * assocdata [I] Type of data to get (ASSOCDATA enum from "shlwapi.h")
445 * pszExtra [I] Extra information about the data location
446 * pvOut [O] Destination for the association key
447 * pcbOut [I/O] Size of pvOut
450 * Success: S_OK. pszOut contains the data, pcbOut contains its length.
451 * Failure: An HRESULT error code indicating the error.
453 HRESULT STDMETHODCALLTYPE
CQueryAssociations::GetData(ASSOCF cfFlags
, ASSOCDATA assocdata
, LPCWSTR pszExtra
, LPVOID pvOut
, DWORD
*pcbOut
)
455 static const WCHAR edit_flags
[] = L
"EditFlags";
457 TRACE("(%p,0x%8x,0x%8x,%s,%p,%p)\n", this, cfFlags
, assocdata
,
458 debugstr_w(pszExtra
), pvOut
, pcbOut
);
462 FIXME("Unsupported flags: %x\n", cfFlags
);
467 case ASSOCDATA_EDITFLAGS
:
469 if(!this->hkeyProgID
)
471 return HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION
);
476 HRESULT hres
= this->GetValue(this->hkeyProgID
, edit_flags
, &data
, &size
);
484 HeapFree(GetProcessHeap(), 0, data
);
488 hres
= this->ReturnData(pvOut
, pcbOut
, data
, size
);
489 HeapFree(GetProcessHeap(), 0, data
);
494 FIXME("Unsupported ASSOCDATA value: %d\n", assocdata
);
500 /**************************************************************************
501 * IQueryAssociations_GetEnum
503 * Not yet implemented in native Win32.
506 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
507 * assocenum [I] Type of enum to get (ASSOCENUM enum from "shlwapi.h")
508 * pszExtra [I] Extra information about the enum location
509 * riid [I] REFIID to look for
510 * ppvOut [O] Destination for the interface.
514 * Failure: An HRESULT error code indicating the error.
517 * Presumably this function returns an enumerator object.
519 HRESULT STDMETHODCALLTYPE
CQueryAssociations::GetEnum(
529 HRESULT
CQueryAssociations::GetValue(HKEY hkey
, const WCHAR
*name
, void **data
, DWORD
*data_size
)
534 ret
= RegQueryValueExW(hkey
, name
, 0, NULL
, NULL
, &size
);
535 if (ret
!= ERROR_SUCCESS
)
537 return HRESULT_FROM_WIN32(ret
);
543 *data
= HeapAlloc(GetProcessHeap(), 0, size
);
546 return E_OUTOFMEMORY
;
548 ret
= RegQueryValueExW(hkey
, name
, 0, NULL
, (LPBYTE
)*data
, &size
);
549 if (ret
!= ERROR_SUCCESS
)
551 HeapFree(GetProcessHeap(), 0, *data
);
552 return HRESULT_FROM_WIN32(ret
);
561 HRESULT
CQueryAssociations::GetCommand(const WCHAR
*extra
, WCHAR
**command
)
568 WCHAR
*extra_from_reg
= NULL
;
570 static const WCHAR commandW
[] = L
"command";
571 static const WCHAR shellW
[] = L
"shell";
573 /* When looking for file extension it's possible to have a default value
574 that points to another key that contains 'shell/<verb>/command' subtree. */
575 hr
= this->GetValue(this->hkeySource
, NULL
, (void**)&filetype
, NULL
);
580 ret
= RegOpenKeyExW(HKEY_CLASSES_ROOT
, filetype
, 0, KEY_READ
, &hkeyFile
);
581 HeapFree(GetProcessHeap(), 0, filetype
);
583 if (ret
== ERROR_SUCCESS
)
585 ret
= RegOpenKeyExW(hkeyFile
, shellW
, 0, KEY_READ
, &hkeyShell
);
586 RegCloseKey(hkeyFile
);
590 ret
= RegOpenKeyExW(this->hkeySource
, shellW
, 0, KEY_READ
, &hkeyShell
);
595 ret
= RegOpenKeyExW(this->hkeySource
, shellW
, 0, KEY_READ
, &hkeyShell
);
600 return HRESULT_FROM_WIN32(ret
);
605 /* check for default verb */
606 hr
= this->GetValue(hkeyShell
, NULL
, (void**)&extra_from_reg
, NULL
);
609 /* no default verb, try first subkey */
610 DWORD max_subkey_len
;
612 ret
= RegQueryInfoKeyW(hkeyShell
, NULL
, NULL
, NULL
, NULL
, &max_subkey_len
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
615 RegCloseKey(hkeyShell
);
616 return HRESULT_FROM_WIN32(ret
);
620 extra_from_reg
= static_cast<WCHAR
*>(HeapAlloc(GetProcessHeap(), 0, max_subkey_len
* sizeof(WCHAR
)));
623 RegCloseKey(hkeyShell
);
624 return E_OUTOFMEMORY
;
627 ret
= RegEnumKeyExW(hkeyShell
, 0, extra_from_reg
, &max_subkey_len
, NULL
, NULL
, NULL
, NULL
);
630 HeapFree(GetProcessHeap(), 0, extra_from_reg
);
631 RegCloseKey(hkeyShell
);
632 return HRESULT_FROM_WIN32(ret
);
635 extra
= extra_from_reg
;
638 /* open verb subkey */
639 ret
= RegOpenKeyExW(hkeyShell
, extra
, 0, KEY_READ
, &hkeyVerb
);
640 HeapFree(GetProcessHeap(), 0, extra_from_reg
);
641 RegCloseKey(hkeyShell
);
644 return HRESULT_FROM_WIN32(ret
);
646 /* open command subkey */
647 ret
= RegOpenKeyExW(hkeyVerb
, commandW
, 0, KEY_READ
, &hkeyCommand
);
648 RegCloseKey(hkeyVerb
);
651 return HRESULT_FROM_WIN32(ret
);
653 hr
= this->GetValue(hkeyCommand
, NULL
, (void**)command
, NULL
);
654 RegCloseKey(hkeyCommand
);
658 HRESULT
CQueryAssociations::GetExecutable(LPCWSTR pszExtra
, LPWSTR path
, DWORD pathlen
, DWORD
*len
)
664 HRESULT hr
= this->GetCommand(pszExtra
, &pszCommand
);
670 DWORD expLen
= ExpandEnvironmentStringsW(pszCommand
, NULL
, 0);
674 WCHAR
*buf
= static_cast<WCHAR
*>(HeapAlloc(GetProcessHeap(), 0, expLen
* sizeof(WCHAR
)));
675 ExpandEnvironmentStringsW(pszCommand
, buf
, expLen
);
676 HeapFree(GetProcessHeap(), 0, pszCommand
);
680 /* cleanup pszCommand */
681 if (pszCommand
[0] == '"')
683 pszStart
= pszCommand
+ 1;
684 pszEnd
= strchrW(pszStart
, '"');
689 *len
= SearchPathW(NULL
, pszStart
, NULL
, pathlen
, path
, NULL
);
693 pszStart
= pszCommand
;
694 for (pszEnd
= pszStart
; (pszEnd
= strchrW(pszEnd
, ' ')); pszEnd
++)
698 if ((*len
= SearchPathW(NULL
, pszStart
, NULL
, pathlen
, path
, NULL
)))
706 *len
= SearchPathW(NULL
, pszStart
, NULL
, pathlen
, path
, NULL
);
710 HeapFree(GetProcessHeap(), 0, pszCommand
);
713 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
718 HRESULT
CQueryAssociations::ReturnData(void *out
, DWORD
*outlen
, const void *data
, DWORD datalen
)
722 if (*outlen
< datalen
)
728 memcpy(out
, data
, datalen
);
738 HRESULT
CQueryAssociations::ReturnString(ASSOCF flags
, LPWSTR out
, DWORD
*outlen
, LPCWSTR data
, DWORD datalen
)
743 TRACE("flags=0x%08x, data=%s\n", flags
, debugstr_w(data
));
751 if (*outlen
< datalen
)
753 if (flags
& ASSOCF_NOTRUNCATE
)
756 if (*outlen
> 0) out
[0] = 0;
761 len
= min(*outlen
, datalen
);
762 hr
= HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER
);
773 memcpy(out
, data
, len
*sizeof(WCHAR
));