setupapi: Add stub for CM_Get_Device_Interface_Alias{A,W}.
[wine.git] / dlls / shell32 / assoc.c
blob49751048f341d1d0316935084f31f4535284131e
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 static HRESULT ASSOC_GetValue(HKEY hkey, const WCHAR *name, void **data, DWORD *data_size);
93 /**************************************************************************
94 * IQueryAssociations_QueryInterface
96 * See IUnknown_QueryInterface.
98 static HRESULT WINAPI IQueryAssociations_fnQueryInterface(
99 IQueryAssociations* iface,
100 REFIID riid,
101 LPVOID *ppvObj)
103 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
105 TRACE("(%p,%s,%p)\n",This, debugstr_guid(riid), ppvObj);
107 if (ppvObj == NULL)
108 return E_POINTER;
110 *ppvObj = NULL;
112 if (IsEqualIID(riid, &IID_IUnknown) ||
113 IsEqualIID(riid, &IID_IQueryAssociations))
115 *ppvObj = &This->IQueryAssociations_iface;
117 IQueryAssociations_AddRef((IQueryAssociations*)*ppvObj);
118 TRACE("Returning IQueryAssociations (%p)\n", *ppvObj);
119 return S_OK;
121 TRACE("Returning E_NOINTERFACE\n");
122 return E_NOINTERFACE;
125 /**************************************************************************
126 * IQueryAssociations_AddRef
128 * See IUnknown_AddRef.
130 static ULONG WINAPI IQueryAssociations_fnAddRef(IQueryAssociations *iface)
132 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
133 ULONG refCount = InterlockedIncrement(&This->ref);
135 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
137 return refCount;
140 /**************************************************************************
141 * IQueryAssociations_Release
143 * See IUnknown_Release.
145 static ULONG WINAPI IQueryAssociations_fnRelease(IQueryAssociations *iface)
147 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
148 ULONG refCount = InterlockedDecrement(&This->ref);
150 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
152 if (!refCount)
154 TRACE("Destroying IQueryAssociations (%p)\n", This);
155 RegCloseKey(This->hkeySource);
156 RegCloseKey(This->hkeyProgID);
157 SHFree(This);
160 return refCount;
163 /**************************************************************************
164 * IQueryAssociations_Init
166 * Initialise an IQueryAssociations object.
168 * PARAMS
169 * iface [I] IQueryAssociations interface to initialise
170 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
171 * pszAssoc [I] String for the root key name, or NULL if hkeyProgid is given
172 * hkeyProgid [I] Handle for the root key, or NULL if pszAssoc is given
173 * hWnd [I] Reserved, must be NULL.
175 * RETURNS
176 * Success: S_OK. iface is initialised with the parameters given.
177 * Failure: An HRESULT error code indicating the error.
179 static HRESULT WINAPI IQueryAssociations_fnInit(
180 IQueryAssociations *iface,
181 ASSOCF cfFlags,
182 LPCWSTR pszAssoc,
183 HKEY hkeyProgid,
184 HWND hWnd)
186 static const WCHAR szProgID[] = {'P','r','o','g','I','D',0};
187 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
188 LONG ret;
190 TRACE("(%p)->(%d,%s,%p,%p)\n", iface,
191 cfFlags,
192 debugstr_w(pszAssoc),
193 hkeyProgid,
194 hWnd);
195 if (hWnd != NULL)
196 FIXME("hwnd != NULL not supported\n");
197 if (cfFlags != 0)
198 FIXME("unsupported flags: %x\n", cfFlags);
200 RegCloseKey(This->hkeySource);
201 if (This->hkeySource != This->hkeyProgID)
202 RegCloseKey(This->hkeyProgID);
203 This->hkeySource = This->hkeyProgID = NULL;
205 /* If the process of initializing hkeyProgID fails, just return S_OK. That's what Windows does. */
206 if (pszAssoc != NULL)
208 WCHAR *progId;
209 HRESULT hr;
211 ret = RegOpenKeyExW(HKEY_CLASSES_ROOT,
212 pszAssoc,
214 KEY_READ,
215 &This->hkeySource);
216 if (ret)
217 return S_OK;
218 /* if this is a progid */
219 if (*pszAssoc != '.' && *pszAssoc != '{')
221 This->hkeyProgID = This->hkeySource;
222 return S_OK;
225 /* if it's not a progid, it's a file extension or clsid */
226 if (*pszAssoc == '.')
228 /* for a file extension, the progid is the default value */
229 hr = ASSOC_GetValue(This->hkeySource, NULL, (void**)&progId, NULL);
230 if (FAILED(hr))
231 return S_OK;
233 else /* if (*pszAssoc == '{') */
235 HKEY progIdKey;
236 /* for a clsid, the progid is the default value of the ProgID subkey */
237 ret = RegOpenKeyExW(This->hkeySource,
238 szProgID,
240 KEY_READ,
241 &progIdKey);
242 if (ret != ERROR_SUCCESS)
243 return S_OK;
244 hr = ASSOC_GetValue(progIdKey, NULL, (void**)&progId, NULL);
245 if (FAILED(hr))
246 return S_OK;
247 RegCloseKey(progIdKey);
250 /* open the actual progid key, the one with the shell subkey */
251 ret = RegOpenKeyExW(HKEY_CLASSES_ROOT,
252 progId,
254 KEY_READ,
255 &This->hkeyProgID);
256 heap_free(progId);
258 return S_OK;
260 else if (hkeyProgid != NULL)
262 /* reopen the key so we don't end up closing a key owned by the caller */
263 RegOpenKeyExW(hkeyProgid, NULL, 0, KEY_READ, &This->hkeyProgID);
264 This->hkeySource = This->hkeyProgID;
265 return S_OK;
267 else
268 return E_INVALIDARG;
271 static HRESULT ASSOC_GetValue(HKEY hkey, const WCHAR *name, void **data, DWORD *data_size)
273 DWORD size;
274 LONG ret;
276 ret = RegQueryValueExW(hkey, name, 0, NULL, NULL, &size);
277 if (ret != ERROR_SUCCESS)
278 return HRESULT_FROM_WIN32(ret);
279 if (!size)
280 return E_FAIL;
281 *data = heap_alloc(size);
282 if (!*data)
283 return E_OUTOFMEMORY;
284 ret = RegQueryValueExW(hkey, name, 0, NULL, (LPBYTE)*data, &size);
285 if (ret != ERROR_SUCCESS)
287 heap_free(*data);
288 return HRESULT_FROM_WIN32(ret);
290 if(data_size)
291 *data_size = size;
292 return S_OK;
295 static HRESULT ASSOC_GetCommand(IQueryAssociationsImpl *This, const WCHAR *extra, WCHAR **command)
297 HKEY hkeyCommand;
298 HKEY hkeyShell;
299 HKEY hkeyVerb;
300 HRESULT hr;
301 LONG ret;
302 WCHAR *extra_from_reg = NULL;
303 WCHAR *filetype;
304 static const WCHAR commandW[] = { 'c','o','m','m','a','n','d',0 };
305 static const WCHAR shellW[] = { 's','h','e','l','l',0 };
307 /* When looking for file extension it's possible to have a default value
308 that points to another key that contains 'shell/<verb>/command' subtree. */
309 hr = ASSOC_GetValue(This->hkeySource, NULL, (void**)&filetype, NULL);
310 if (hr == S_OK)
312 HKEY hkeyFile;
314 ret = RegOpenKeyExW(HKEY_CLASSES_ROOT, filetype, 0, KEY_READ, &hkeyFile);
315 heap_free(filetype);
317 if (ret == ERROR_SUCCESS)
319 ret = RegOpenKeyExW(hkeyFile, shellW, 0, KEY_READ, &hkeyShell);
320 RegCloseKey(hkeyFile);
322 else
323 ret = RegOpenKeyExW(This->hkeySource, shellW, 0, KEY_READ, &hkeyShell);
325 else
326 ret = RegOpenKeyExW(This->hkeySource, shellW, 0, KEY_READ, &hkeyShell);
328 if (ret) return HRESULT_FROM_WIN32(ret);
330 if (!extra)
332 /* check for default verb */
333 hr = ASSOC_GetValue(hkeyShell, NULL, (void**)&extra_from_reg, NULL);
334 if (FAILED(hr))
336 /* no default verb, try first subkey */
337 DWORD max_subkey_len;
339 ret = RegQueryInfoKeyW(hkeyShell, NULL, NULL, NULL, NULL, &max_subkey_len, NULL, NULL, NULL, NULL, NULL, NULL);
340 if (ret)
342 RegCloseKey(hkeyShell);
343 return HRESULT_FROM_WIN32(ret);
346 max_subkey_len++;
347 extra_from_reg = heap_alloc(max_subkey_len * sizeof(WCHAR));
348 if (!extra_from_reg)
350 RegCloseKey(hkeyShell);
351 return E_OUTOFMEMORY;
354 ret = RegEnumKeyExW(hkeyShell, 0, extra_from_reg, &max_subkey_len, NULL, NULL, NULL, NULL);
355 if (ret)
357 heap_free(extra_from_reg);
358 RegCloseKey(hkeyShell);
359 return HRESULT_FROM_WIN32(ret);
362 extra = extra_from_reg;
365 /* open verb subkey */
366 ret = RegOpenKeyExW(hkeyShell, extra, 0, KEY_READ, &hkeyVerb);
367 heap_free(extra_from_reg);
368 RegCloseKey(hkeyShell);
369 if (ret) return HRESULT_FROM_WIN32(ret);
371 /* open command subkey */
372 ret = RegOpenKeyExW(hkeyVerb, commandW, 0, KEY_READ, &hkeyCommand);
373 RegCloseKey(hkeyVerb);
374 if (ret) return HRESULT_FROM_WIN32(ret);
376 hr = ASSOC_GetValue(hkeyCommand, NULL, (void**)command, NULL);
377 RegCloseKey(hkeyCommand);
378 return hr;
381 static HRESULT ASSOC_GetExecutable(IQueryAssociationsImpl *This,
382 LPCWSTR pszExtra, LPWSTR path,
383 DWORD pathlen, DWORD *len)
385 WCHAR *pszCommand;
386 WCHAR *pszStart;
387 WCHAR *pszEnd;
388 HRESULT hr;
390 hr = ASSOC_GetCommand(This, pszExtra, &pszCommand);
391 if (FAILED(hr))
392 return hr;
394 /* cleanup pszCommand */
395 if (pszCommand[0] == '"')
397 pszStart = pszCommand + 1;
398 pszEnd = strchrW(pszStart, '"');
399 if (pszEnd)
400 *pszEnd = 0;
401 *len = SearchPathW(NULL, pszStart, NULL, pathlen, path, NULL);
403 else
405 pszStart = pszCommand;
406 for (pszEnd = pszStart; (pszEnd = strchrW(pszEnd, ' ')); pszEnd++)
408 WCHAR c = *pszEnd;
409 *pszEnd = 0;
410 if ((*len = SearchPathW(NULL, pszStart, NULL, pathlen, path, NULL)))
411 break;
412 *pszEnd = c;
414 if (!pszEnd)
415 *len = SearchPathW(NULL, pszStart, NULL, pathlen, path, NULL);
418 heap_free(pszCommand);
419 if (!*len)
420 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
421 return S_OK;
424 static HRESULT ASSOC_ReturnData(void *out, DWORD *outlen, const void *data,
425 DWORD datalen)
427 if (out)
429 if (*outlen < datalen)
431 *outlen = datalen;
432 return E_POINTER;
434 *outlen = datalen;
435 memcpy(out, data, datalen);
436 return S_OK;
438 else
440 *outlen = datalen;
441 return S_FALSE;
445 static HRESULT ASSOC_ReturnString(ASSOCF flags, LPWSTR out, DWORD *outlen, LPCWSTR data, DWORD datalen)
447 HRESULT hr = S_OK;
448 DWORD len;
450 TRACE("flags=0x%08x, data=%s\n", flags, debugstr_w(data));
452 if (!out)
454 *outlen = datalen;
455 return S_FALSE;
458 if (*outlen < datalen)
460 if (flags & ASSOCF_NOTRUNCATE)
462 len = 0;
463 if (*outlen > 0) out[0] = 0;
464 hr = E_POINTER;
466 else
468 len = min(*outlen, datalen);
469 hr = E_NOT_SUFFICIENT_BUFFER;
471 *outlen = datalen;
473 else
474 len = datalen;
476 if (len)
477 memcpy(out, data, len*sizeof(WCHAR));
479 return hr;
482 /**************************************************************************
483 * IQueryAssociations_GetString
485 * Get a file association string from the registry.
487 * PARAMS
488 * iface [I] IQueryAssociations interface to query
489 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
490 * str [I] Type of string to get (ASSOCSTR enum from "shlwapi.h")
491 * pszExtra [I] Extra information about the string location
492 * pszOut [O] Destination for the association string
493 * pcchOut [I/O] Length of pszOut
495 * RETURNS
496 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
497 * Failure: An HRESULT error code indicating the error.
499 static HRESULT WINAPI IQueryAssociations_fnGetString(
500 IQueryAssociations *iface,
501 ASSOCF flags,
502 ASSOCSTR str,
503 LPCWSTR pszExtra,
504 LPWSTR pszOut,
505 DWORD *pcchOut)
507 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
508 const ASSOCF unimplemented_flags = ~ASSOCF_NOTRUNCATE;
509 DWORD len = 0;
510 HRESULT hr;
511 WCHAR path[MAX_PATH];
513 TRACE("(%p)->(0x%08x, %u, %s, %p, %p)\n", This, flags, str, debugstr_w(pszExtra), pszOut, pcchOut);
515 if (flags & unimplemented_flags)
516 FIXME("%08x: unimplemented flags\n", flags & unimplemented_flags);
518 if (!pcchOut)
519 return E_UNEXPECTED;
521 if (!This->hkeySource && !This->hkeyProgID)
522 return HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION);
524 switch (str)
526 case ASSOCSTR_COMMAND:
528 WCHAR *command;
529 hr = ASSOC_GetCommand(This, pszExtra, &command);
530 if (SUCCEEDED(hr))
532 hr = ASSOC_ReturnString(flags, pszOut, pcchOut, command, strlenW(command) + 1);
533 heap_free(command);
535 return hr;
538 case ASSOCSTR_EXECUTABLE:
540 hr = ASSOC_GetExecutable(This, pszExtra, path, MAX_PATH, &len);
541 if (FAILED(hr))
542 return hr;
543 len++;
544 return ASSOC_ReturnString(flags, pszOut, pcchOut, path, len);
547 case ASSOCSTR_FRIENDLYDOCNAME:
549 WCHAR *docName;
551 hr = ASSOC_GetValue(This->hkeyProgID, NULL, (void**)&docName, NULL);
552 if (FAILED(hr)) {
553 /* hKeyProgID is NULL or there is no default value, so fail */
554 return HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION);
556 hr = ASSOC_ReturnString(flags, pszOut, pcchOut, docName, strlenW(docName) + 1);
557 heap_free(docName);
558 return hr;
561 case ASSOCSTR_FRIENDLYAPPNAME:
563 PVOID verinfoW = NULL;
564 DWORD size, retval = 0;
565 UINT flen;
566 WCHAR *bufW;
567 static const WCHAR translationW[] = {
568 '\\','V','a','r','F','i','l','e','I','n','f','o',
569 '\\','T','r','a','n','s','l','a','t','i','o','n',0
571 static const WCHAR fileDescFmtW[] = {
572 '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
573 '\\','%','0','4','x','%','0','4','x',
574 '\\','F','i','l','e','D','e','s','c','r','i','p','t','i','o','n',0
576 WCHAR fileDescW[41];
578 hr = ASSOC_GetExecutable(This, pszExtra, path, MAX_PATH, &len);
579 if (FAILED(hr))
580 return hr;
582 retval = GetFileVersionInfoSizeW(path, &size);
583 if (!retval)
584 goto get_friendly_name_fail;
585 verinfoW = heap_alloc_zero(retval);
586 if (!verinfoW)
587 return E_OUTOFMEMORY;
588 if (!GetFileVersionInfoW(path, 0, retval, verinfoW))
589 goto get_friendly_name_fail;
590 if (VerQueryValueW(verinfoW, translationW, (LPVOID *)&bufW, &flen))
592 UINT i;
593 DWORD *langCodeDesc = (DWORD *)bufW;
594 for (i = 0; i < flen / sizeof(DWORD); i++)
596 sprintfW(fileDescW, fileDescFmtW, LOWORD(langCodeDesc[i]),
597 HIWORD(langCodeDesc[i]));
598 if (VerQueryValueW(verinfoW, fileDescW, (LPVOID *)&bufW, &flen))
600 /* Does strlenW(bufW) == 0 mean we use the filename? */
601 len = strlenW(bufW) + 1;
602 TRACE("found FileDescription: %s\n", debugstr_w(bufW));
603 hr = ASSOC_ReturnString(flags, pszOut, pcchOut, bufW, len);
604 heap_free(verinfoW);
605 return hr;
609 get_friendly_name_fail:
610 PathRemoveExtensionW(path);
611 PathStripPathW(path);
612 TRACE("using filename: %s\n", debugstr_w(path));
613 hr = ASSOC_ReturnString(flags, pszOut, pcchOut, path, strlenW(path) + 1);
614 heap_free(verinfoW);
615 return hr;
618 case ASSOCSTR_CONTENTTYPE:
620 static const WCHAR Content_TypeW[] = {'C','o','n','t','e','n','t',' ','T','y','p','e',0};
621 WCHAR *contentType;
622 DWORD ret;
623 DWORD size;
625 size = 0;
626 ret = RegGetValueW(This->hkeySource, NULL, Content_TypeW, RRF_RT_REG_SZ, NULL, NULL, &size);
627 if (ret != ERROR_SUCCESS)
628 return HRESULT_FROM_WIN32(ret);
629 contentType = heap_alloc(size);
630 if (contentType != NULL)
632 ret = RegGetValueW(This->hkeySource, NULL, Content_TypeW, RRF_RT_REG_SZ, NULL, contentType, &size);
633 if (ret == ERROR_SUCCESS)
634 hr = ASSOC_ReturnString(flags, pszOut, pcchOut, contentType, strlenW(contentType) + 1);
635 else
636 hr = HRESULT_FROM_WIN32(ret);
637 heap_free(contentType);
639 else
640 hr = E_OUTOFMEMORY;
641 return hr;
644 case ASSOCSTR_DEFAULTICON:
646 static const WCHAR DefaultIconW[] = {'D','e','f','a','u','l','t','I','c','o','n',0};
647 static const WCHAR documentIcon[] = {'s','h','e','l','l','3','2','.','d','l','l',',','0',0};
648 DWORD ret;
649 DWORD size;
651 size = 0;
652 ret = RegGetValueW(This->hkeyProgID, DefaultIconW, NULL, RRF_RT_REG_SZ, NULL, NULL, &size);
653 if (ret == ERROR_SUCCESS)
655 WCHAR *icon = heap_alloc(size);
656 if (icon)
658 ret = RegGetValueW(This->hkeyProgID, DefaultIconW, NULL, RRF_RT_REG_SZ, NULL, icon, &size);
659 if (ret == ERROR_SUCCESS)
660 hr = ASSOC_ReturnString(flags, pszOut, pcchOut, icon, strlenW(icon) + 1);
661 else
662 hr = HRESULT_FROM_WIN32(ret);
663 heap_free(icon);
665 else
666 hr = E_OUTOFMEMORY;
667 } else {
668 /* there is no DefaultIcon subkey or hkeyProgID is NULL, so return the default document icon */
669 if (This->hkeyProgID == NULL)
670 hr = ASSOC_ReturnString(flags, pszOut, pcchOut, documentIcon, strlenW(documentIcon) + 1);
671 else
672 return HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION);
674 return hr;
676 case ASSOCSTR_SHELLEXTENSION:
678 static const WCHAR shellexW[] = {'S','h','e','l','l','E','x','\\',0};
679 WCHAR keypath[ARRAY_SIZE(shellexW) + 39], guid[39];
680 CLSID clsid;
681 HKEY hkey;
682 DWORD size;
683 LONG ret;
685 hr = CLSIDFromString(pszExtra, &clsid);
686 if (FAILED(hr)) return hr;
688 strcpyW(keypath, shellexW);
689 strcatW(keypath, pszExtra);
690 ret = RegOpenKeyExW(This->hkeySource, keypath, 0, KEY_READ, &hkey);
691 if (ret) return HRESULT_FROM_WIN32(ret);
693 size = sizeof(guid);
694 ret = RegGetValueW(hkey, NULL, NULL, RRF_RT_REG_SZ, NULL, guid, &size);
695 RegCloseKey(hkey);
696 if (ret) return HRESULT_FROM_WIN32(ret);
698 return ASSOC_ReturnString(flags, pszOut, pcchOut, guid, size / sizeof(WCHAR));
701 default:
702 FIXME("assocstr %d unimplemented!\n", str);
703 return E_NOTIMPL;
707 /**************************************************************************
708 * IQueryAssociations_GetKey
710 * Get a file association key from the registry.
712 * PARAMS
713 * iface [I] IQueryAssociations interface to query
714 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
715 * assockey [I] Type of key to get (ASSOCKEY enum from "shlwapi.h")
716 * pszExtra [I] Extra information about the key location
717 * phkeyOut [O] Destination for the association key
719 * RETURNS
720 * Success: S_OK. phkeyOut contains a handle to the key.
721 * Failure: An HRESULT error code indicating the error.
723 static HRESULT WINAPI IQueryAssociations_fnGetKey(
724 IQueryAssociations *iface,
725 ASSOCF cfFlags,
726 ASSOCKEY assockey,
727 LPCWSTR pszExtra,
728 HKEY *phkeyOut)
730 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
732 FIXME("(%p,0x%8x,0x%8x,%s,%p)-stub!\n", This, cfFlags, assockey,
733 debugstr_w(pszExtra), phkeyOut);
734 return E_NOTIMPL;
737 /**************************************************************************
738 * IQueryAssociations_GetData
740 * Get the data for a file association key from the registry.
742 * PARAMS
743 * iface [I] IQueryAssociations interface to query
744 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
745 * assocdata [I] Type of data to get (ASSOCDATA enum from "shlwapi.h")
746 * pszExtra [I] Extra information about the data location
747 * pvOut [O] Destination for the association key
748 * pcbOut [I/O] Size of pvOut
750 * RETURNS
751 * Success: S_OK. pszOut contains the data, pcbOut contains its length.
752 * Failure: An HRESULT error code indicating the error.
754 static HRESULT WINAPI IQueryAssociations_fnGetData(IQueryAssociations *iface,
755 ASSOCF cfFlags, ASSOCDATA assocdata, LPCWSTR pszExtra, LPVOID pvOut,
756 DWORD *pcbOut)
758 static const WCHAR edit_flags[] = {'E','d','i','t','F','l','a','g','s',0};
760 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
761 void *data = NULL;
762 DWORD size;
763 HRESULT hres;
765 TRACE("(%p,0x%8x,0x%8x,%s,%p,%p)\n", This, cfFlags, assocdata,
766 debugstr_w(pszExtra), pvOut, pcbOut);
768 if(cfFlags)
769 FIXME("Unsupported flags: %x\n", cfFlags);
771 switch(assocdata) {
772 case ASSOCDATA_EDITFLAGS:
773 if(!This->hkeyProgID)
774 return HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION);
776 hres = ASSOC_GetValue(This->hkeyProgID, edit_flags, &data, &size);
777 if(SUCCEEDED(hres) && pcbOut)
778 hres = ASSOC_ReturnData(pvOut, pcbOut, data, size);
779 heap_free(data);
780 return hres;
781 default:
782 FIXME("Unsupported ASSOCDATA value: %d\n", assocdata);
783 return E_NOTIMPL;
787 /**************************************************************************
788 * IQueryAssociations_GetEnum
790 * Not yet implemented in native Win32.
792 * PARAMS
793 * iface [I] IQueryAssociations interface to query
794 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
795 * assocenum [I] Type of enum to get (ASSOCENUM enum from "shlwapi.h")
796 * pszExtra [I] Extra information about the enum location
797 * riid [I] REFIID to look for
798 * ppvOut [O] Destination for the interface.
800 * RETURNS
801 * Success: S_OK.
802 * Failure: An HRESULT error code indicating the error.
804 * NOTES
805 * Presumably this function returns an enumerator object.
807 static HRESULT WINAPI IQueryAssociations_fnGetEnum(
808 IQueryAssociations *iface,
809 ASSOCF cfFlags,
810 ASSOCENUM assocenum,
811 LPCWSTR pszExtra,
812 REFIID riid,
813 LPVOID *ppvOut)
815 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
817 FIXME("(%p,0x%8x,0x%8x,%s,%s,%p)-stub!\n", This, cfFlags, assocenum,
818 debugstr_w(pszExtra), debugstr_guid(riid), ppvOut);
819 return E_NOTIMPL;
822 static const IQueryAssociationsVtbl IQueryAssociations_vtbl =
824 IQueryAssociations_fnQueryInterface,
825 IQueryAssociations_fnAddRef,
826 IQueryAssociations_fnRelease,
827 IQueryAssociations_fnInit,
828 IQueryAssociations_fnGetString,
829 IQueryAssociations_fnGetKey,
830 IQueryAssociations_fnGetData,
831 IQueryAssociations_fnGetEnum
834 /**************************************************************************
835 * IApplicationAssociationRegistration implementation
837 static inline IApplicationAssociationRegistrationImpl *impl_from_IApplicationAssociationRegistration(IApplicationAssociationRegistration *iface)
839 return CONTAINING_RECORD(iface, IApplicationAssociationRegistrationImpl, IApplicationAssociationRegistration_iface);
842 static HRESULT WINAPI ApplicationAssociationRegistration_QueryInterface(
843 IApplicationAssociationRegistration* iface, REFIID riid, LPVOID *ppv)
845 IApplicationAssociationRegistrationImpl *This = impl_from_IApplicationAssociationRegistration(iface);
847 TRACE("(%p, %s, %p)\n",This, debugstr_guid(riid), ppv);
849 if (ppv == NULL)
850 return E_POINTER;
852 if (IsEqualGUID(&IID_IUnknown, riid) ||
853 IsEqualGUID(&IID_IApplicationAssociationRegistration, riid)) {
854 *ppv = &This->IApplicationAssociationRegistration_iface;
855 IUnknown_AddRef((IUnknown*)*ppv);
856 TRACE("returning IApplicationAssociationRegistration: %p\n", *ppv);
857 return S_OK;
860 *ppv = NULL;
861 FIXME("(%p)->(%s %p) interface not supported\n", This, debugstr_guid(riid), ppv);
862 return E_NOINTERFACE;
865 static ULONG WINAPI ApplicationAssociationRegistration_AddRef(IApplicationAssociationRegistration *iface)
867 IApplicationAssociationRegistrationImpl *This = impl_from_IApplicationAssociationRegistration(iface);
868 ULONG ref = InterlockedIncrement(&This->ref);
870 TRACE("(%p) ref=%d\n", This, ref);
871 return ref;
874 static ULONG WINAPI ApplicationAssociationRegistration_Release(IApplicationAssociationRegistration *iface)
876 IApplicationAssociationRegistrationImpl *This = impl_from_IApplicationAssociationRegistration(iface);
877 ULONG ref = InterlockedDecrement(&This->ref);
879 TRACE("(%p) ref=%d\n", This, ref);
881 if (!ref) {
882 SHFree(This);
884 return ref;
887 static HRESULT WINAPI ApplicationAssociationRegistration_QueryCurrentDefault(IApplicationAssociationRegistration *iface, LPCWSTR query,
888 ASSOCIATIONTYPE type, ASSOCIATIONLEVEL level, LPWSTR *association)
890 IApplicationAssociationRegistrationImpl *This = impl_from_IApplicationAssociationRegistration(iface);
891 static WCHAR urlassoc[] = {'U','r','l','A','s','s','o','c','i','a','t','i','o','n','s',0};
892 static WCHAR mimeassoc[] = {'M','I','M','E','A','s','s','o','c','i','a','t','i','o','n','s',0};
893 static WCHAR associations[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
894 'W','i','n','d','o','w','s','\\','S','h','e','l','l','\\',
895 'A','s','s','o','c','i','a','t','i','o','n','s',0};
896 static WCHAR slash[] = {'\\',0};
897 static WCHAR choice[] = {'U','s','e','r','C','h','o','i','c','e',0};
898 static WCHAR propid[] = {'P','r','o','g','i','d',0};
899 WCHAR path[MAX_PATH] = {0};
900 DWORD ret, keytype, size;
901 HKEY hkey = NULL;
902 HRESULT hr = HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION);
904 TRACE("(%p)->(%s, %d, %d, %p)\n", This, debugstr_w(query), type, level, association);
906 if(!association)
907 return E_INVALIDARG;
909 *association = NULL;
911 if((type == AT_URLPROTOCOL || type == AT_FILEEXTENSION) && !query[0])
912 return E_INVALIDARG;
913 else if(type == AT_FILEEXTENSION && query[0] != '.')
914 return E_INVALIDARG;
916 if(type == AT_FILEEXTENSION)
918 ret = RegOpenKeyExW(HKEY_CLASSES_ROOT, query, 0, KEY_READ, &hkey);
919 if(ret == ERROR_SUCCESS)
921 ret = RegGetValueW(hkey, NULL, NULL, RRF_RT_REG_SZ, &keytype, NULL, &size);
922 if(ret == ERROR_SUCCESS)
924 *association = CoTaskMemAlloc(size);
925 if(*association)
927 ret = RegGetValueW(hkey, NULL, NULL, RRF_RT_REG_SZ, &keytype, *association, &size);
928 if(ret == ERROR_SUCCESS)
929 hr = S_OK;
930 else
932 CoTaskMemFree(*association);
933 *association = NULL;
936 else
937 hr = E_OUTOFMEMORY;
941 else
943 ret = RegOpenKeyExW(HKEY_CURRENT_USER, associations, 0, KEY_READ, &hkey);
944 if(ret == ERROR_SUCCESS)
946 if(type == AT_URLPROTOCOL)
947 lstrcpyW(path, urlassoc);
948 else if(type == AT_MIMETYPE)
949 lstrcpyW(path, mimeassoc);
950 else
952 WARN("Unsupported type (%d).\n", type);
953 RegCloseKey(hkey);
954 return hr;
957 lstrcatW(path, slash);
958 lstrcatW(path, query);
959 lstrcatW(path, slash);
960 lstrcatW(path, choice);
962 ret = RegGetValueW(hkey, path, propid, RRF_RT_REG_SZ, &keytype, NULL, &size);
963 if(ret == ERROR_SUCCESS)
965 *association = CoTaskMemAlloc(size);
966 if(*association)
968 ret = RegGetValueW(hkey, path, propid, RRF_RT_REG_SZ, &keytype, *association, &size);
969 if(ret == ERROR_SUCCESS)
970 hr = S_OK;
971 else
973 CoTaskMemFree(*association);
974 *association = NULL;
977 else
978 hr = E_OUTOFMEMORY;
983 RegCloseKey(hkey);
985 return hr;
988 static HRESULT WINAPI ApplicationAssociationRegistration_QueryAppIsDefault(IApplicationAssociationRegistration* This, LPCWSTR query,
989 ASSOCIATIONTYPE type, ASSOCIATIONLEVEL level, LPCWSTR appname, BOOL *is_default)
991 FIXME("(%p)->(%s, %d, %d, %s, %p)\n", This, debugstr_w(query), type, level, debugstr_w(appname), is_default);
992 return E_NOTIMPL;
995 static HRESULT WINAPI ApplicationAssociationRegistration_QueryAppIsDefaultAll(IApplicationAssociationRegistration* This, ASSOCIATIONLEVEL level,
996 LPCWSTR appname, BOOL *is_default)
998 FIXME("(%p)->(%d, %s, %p)\n", This, level, debugstr_w(appname), is_default);
999 return E_NOTIMPL;
1002 static HRESULT WINAPI ApplicationAssociationRegistration_SetAppAsDefault(IApplicationAssociationRegistration* This, LPCWSTR appname,
1003 LPCWSTR set, ASSOCIATIONTYPE set_type)
1005 FIXME("(%p)->(%s, %s, %d)\n", This, debugstr_w(appname), debugstr_w(set), set_type);
1006 return E_NOTIMPL;
1009 static HRESULT WINAPI ApplicationAssociationRegistration_SetAppAsDefaultAll(IApplicationAssociationRegistration* This, LPCWSTR appname)
1011 FIXME("(%p)->(%s)\n", This, debugstr_w(appname));
1012 return E_NOTIMPL;
1016 static HRESULT WINAPI ApplicationAssociationRegistration_ClearUserAssociations(IApplicationAssociationRegistration* This)
1018 FIXME("(%p)\n", This);
1019 return E_NOTIMPL;
1023 static const IApplicationAssociationRegistrationVtbl IApplicationAssociationRegistration_vtbl =
1025 ApplicationAssociationRegistration_QueryInterface,
1026 ApplicationAssociationRegistration_AddRef,
1027 ApplicationAssociationRegistration_Release,
1028 ApplicationAssociationRegistration_QueryCurrentDefault,
1029 ApplicationAssociationRegistration_QueryAppIsDefault,
1030 ApplicationAssociationRegistration_QueryAppIsDefaultAll,
1031 ApplicationAssociationRegistration_SetAppAsDefault,
1032 ApplicationAssociationRegistration_SetAppAsDefaultAll,
1033 ApplicationAssociationRegistration_ClearUserAssociations
1036 /**************************************************************************
1037 * IQueryAssociations_Constructor [internal]
1039 * Construct a new IQueryAssociations object.
1041 HRESULT WINAPI QueryAssociations_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppOutput)
1043 IQueryAssociationsImpl* this;
1044 HRESULT ret;
1046 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1048 if (!(this = SHAlloc(sizeof(*this)))) return E_OUTOFMEMORY;
1049 this->IQueryAssociations_iface.lpVtbl = &IQueryAssociations_vtbl;
1050 this->ref = 0;
1051 this->hkeySource = 0;
1052 this->hkeyProgID = 0;
1053 if (FAILED(ret = IQueryAssociations_QueryInterface(&this->IQueryAssociations_iface, riid, ppOutput))) SHFree( this );
1054 TRACE("returning %p\n", *ppOutput);
1055 return ret;
1058 /**************************************************************************
1059 * ApplicationAssociationRegistration_Constructor [internal]
1061 * Construct a IApplicationAssociationRegistration object.
1063 HRESULT WINAPI ApplicationAssociationRegistration_Constructor(IUnknown *outer, REFIID riid, LPVOID *ppv)
1065 IApplicationAssociationRegistrationImpl *This;
1066 HRESULT hr;
1068 if (outer)
1069 return CLASS_E_NOAGGREGATION;
1071 if (!(This = SHAlloc(sizeof(*This))))
1072 return E_OUTOFMEMORY;
1074 This->IApplicationAssociationRegistration_iface.lpVtbl = &IApplicationAssociationRegistration_vtbl;
1075 This->ref = 0;
1077 hr = IApplicationAssociationRegistration_QueryInterface(&This->IApplicationAssociationRegistration_iface, riid, ppv);
1078 if (FAILED(hr))
1079 SHFree(This);
1081 TRACE("returning 0x%x with %p\n", hr, *ppv);
1082 return hr;
1085 static HRESULT WINAPI enumassochandlers_QueryInterface(IEnumAssocHandlers *iface, REFIID riid, void **obj)
1087 struct enumassochandlers *This = impl_from_IEnumAssocHandlers(iface);
1089 TRACE("(%p %s %p)\n", This, debugstr_guid(riid), obj);
1091 if (IsEqualIID(riid, &IID_IEnumAssocHandlers) ||
1092 IsEqualIID(riid, &IID_IUnknown))
1094 *obj = iface;
1095 IEnumAssocHandlers_AddRef(iface);
1096 return S_OK;
1099 *obj = NULL;
1100 return E_NOINTERFACE;
1103 static ULONG WINAPI enumassochandlers_AddRef(IEnumAssocHandlers *iface)
1105 struct enumassochandlers *This = impl_from_IEnumAssocHandlers(iface);
1106 ULONG ref = InterlockedIncrement(&This->ref);
1108 TRACE("(%p)->(%u)\n", This, ref);
1109 return ref;
1112 static ULONG WINAPI enumassochandlers_Release(IEnumAssocHandlers *iface)
1114 struct enumassochandlers *This = impl_from_IEnumAssocHandlers(iface);
1115 ULONG ref = InterlockedDecrement(&This->ref);
1117 TRACE("(%p)->(%u)\n", This, ref);
1119 if (!ref)
1120 SHFree(This);
1122 return ref;
1125 static HRESULT WINAPI enumassochandlers_Next(IEnumAssocHandlers *iface, ULONG count, IAssocHandler **handlers,
1126 ULONG *fetched)
1128 struct enumassochandlers *This = impl_from_IEnumAssocHandlers(iface);
1130 FIXME("(%p)->(%u %p %p): stub\n", This, count, handlers, fetched);
1132 return E_NOTIMPL;
1135 static const IEnumAssocHandlersVtbl enumassochandlersvtbl = {
1136 enumassochandlers_QueryInterface,
1137 enumassochandlers_AddRef,
1138 enumassochandlers_Release,
1139 enumassochandlers_Next
1142 /**************************************************************************
1143 * SHCreateAssociationRegistration [SHELL32.@]
1145 HRESULT WINAPI SHCreateAssociationRegistration(REFIID riid, LPVOID *ppv)
1147 return ApplicationAssociationRegistration_Constructor(NULL, riid, ppv);
1150 /**************************************************************************
1151 * SHAssocEnumHandlers [SHELL32.@]
1153 HRESULT WINAPI SHAssocEnumHandlers(const WCHAR *extra, ASSOC_FILTER filter, IEnumAssocHandlers **enumhandlers)
1155 struct enumassochandlers *enumassoc;
1157 FIXME("(%s %d %p): stub\n", debugstr_w(extra), filter, enumhandlers);
1159 *enumhandlers = NULL;
1161 enumassoc = SHAlloc(sizeof(*enumassoc));
1162 if (!enumassoc)
1163 return E_OUTOFMEMORY;
1165 enumassoc->IEnumAssocHandlers_iface.lpVtbl = &enumassochandlersvtbl;
1166 enumassoc->ref = 1;
1168 *enumhandlers = &enumassoc->IEnumAssocHandlers_iface;
1169 return S_OK;