dinput: Clear DIA_APPNOMAP BuildActionMap flag with specific device semantic.
[wine.git] / dlls / shell32 / assoc.c
blob339be6ac44554087740d7c7a6eacbd6ad7125e8c
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/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(shell);
37 /**************************************************************************
38 * IQueryAssociations
40 * DESCRIPTION
41 * This object provides a layer of abstraction over the system registry in
42 * order to simplify the process of parsing associations between files.
43 * Associations in this context means the registry entries that link (for
44 * example) the extension of a file with its description, list of
45 * applications to open the file with, and actions that can be performed on it
46 * (the shell displays such information in the context menu of explorer
47 * when you right-click on a file).
49 * HELPERS
50 * You can use this object transparently by calling the helper functions
51 * AssocQueryKeyA(), AssocQueryStringA() and AssocQueryStringByKeyA(). These
52 * create an IQueryAssociations object, perform the requested actions
53 * and then dispose of the object. Alternatively, you can create an instance
54 * of the object using AssocCreate() and call the following methods on it:
56 * METHODS
59 typedef struct
61 IQueryAssociations IQueryAssociations_iface;
62 LONG ref;
63 HKEY hkeySource;
64 HKEY hkeyProgID;
65 } IQueryAssociationsImpl;
67 typedef struct
69 IApplicationAssociationRegistration IApplicationAssociationRegistration_iface;
70 LONG ref;
71 } IApplicationAssociationRegistrationImpl;
74 static inline IQueryAssociationsImpl *impl_from_IQueryAssociations(IQueryAssociations *iface)
76 return CONTAINING_RECORD(iface, IQueryAssociationsImpl, IQueryAssociations_iface);
79 struct enumassochandlers
81 IEnumAssocHandlers IEnumAssocHandlers_iface;
82 LONG ref;
85 static inline struct enumassochandlers *impl_from_IEnumAssocHandlers(IEnumAssocHandlers *iface)
87 return CONTAINING_RECORD(iface, struct enumassochandlers, IEnumAssocHandlers_iface);
90 static HRESULT ASSOC_GetValue(HKEY hkey, const WCHAR *name, void **data, DWORD *data_size);
92 /**************************************************************************
93 * IQueryAssociations_QueryInterface
95 * See IUnknown_QueryInterface.
97 static HRESULT WINAPI IQueryAssociations_fnQueryInterface(
98 IQueryAssociations* iface,
99 REFIID riid,
100 LPVOID *ppvObj)
102 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
104 TRACE("(%p,%s,%p)\n",This, debugstr_guid(riid), ppvObj);
106 if (ppvObj == NULL)
107 return E_POINTER;
109 *ppvObj = NULL;
111 if (IsEqualIID(riid, &IID_IUnknown) ||
112 IsEqualIID(riid, &IID_IQueryAssociations))
114 *ppvObj = &This->IQueryAssociations_iface;
116 IQueryAssociations_AddRef((IQueryAssociations*)*ppvObj);
117 TRACE("Returning IQueryAssociations (%p)\n", *ppvObj);
118 return S_OK;
120 TRACE("Returning E_NOINTERFACE\n");
121 return E_NOINTERFACE;
124 /**************************************************************************
125 * IQueryAssociations_AddRef
127 * See IUnknown_AddRef.
129 static ULONG WINAPI IQueryAssociations_fnAddRef(IQueryAssociations *iface)
131 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
132 ULONG refCount = InterlockedIncrement(&This->ref);
134 TRACE("(%p)->(ref before=%lu)\n",This, refCount - 1);
136 return refCount;
139 /**************************************************************************
140 * IQueryAssociations_Release
142 * See IUnknown_Release.
144 static ULONG WINAPI IQueryAssociations_fnRelease(IQueryAssociations *iface)
146 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
147 ULONG refCount = InterlockedDecrement(&This->ref);
149 TRACE("(%p)->(ref before=%lu)\n",This, refCount + 1);
151 if (!refCount)
153 TRACE("Destroying IQueryAssociations (%p)\n", This);
154 RegCloseKey(This->hkeySource);
155 RegCloseKey(This->hkeyProgID);
156 SHFree(This);
159 return refCount;
162 /**************************************************************************
163 * IQueryAssociations_Init
165 * Initialise an IQueryAssociations object.
167 * PARAMS
168 * iface [I] IQueryAssociations interface to initialise
169 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
170 * pszAssoc [I] String for the root key name, or NULL if hkeyProgid is given
171 * hkeyProgid [I] Handle for the root key, or NULL if pszAssoc is given
172 * hWnd [I] Reserved, must be NULL.
174 * RETURNS
175 * Success: S_OK. iface is initialised with the parameters given.
176 * Failure: An HRESULT error code indicating the error.
178 static HRESULT WINAPI IQueryAssociations_fnInit(
179 IQueryAssociations *iface,
180 ASSOCF cfFlags,
181 LPCWSTR pszAssoc,
182 HKEY hkeyProgid,
183 HWND hWnd)
185 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
186 LONG ret;
188 TRACE("(%p)->(%ld,%s,%p,%p)\n", iface,
189 cfFlags,
190 debugstr_w(pszAssoc),
191 hkeyProgid,
192 hWnd);
193 if (hWnd != NULL)
194 FIXME("hwnd != NULL not supported\n");
195 if (cfFlags != 0)
196 FIXME("unsupported flags: %lx\n", cfFlags);
198 RegCloseKey(This->hkeySource);
199 if (This->hkeySource != This->hkeyProgID)
200 RegCloseKey(This->hkeyProgID);
201 This->hkeySource = This->hkeyProgID = NULL;
203 /* If the process of initializing hkeyProgID fails, just return S_OK. That's what Windows does. */
204 if (pszAssoc != NULL)
206 WCHAR *progId;
207 HRESULT hr;
209 ret = RegOpenKeyExW(HKEY_CLASSES_ROOT,
210 pszAssoc,
212 KEY_READ,
213 &This->hkeySource);
214 if (ret)
215 return S_OK;
216 /* if this is a progid */
217 if (*pszAssoc != '.' && *pszAssoc != '{')
219 This->hkeyProgID = This->hkeySource;
220 return S_OK;
223 /* if it's not a progid, it's a file extension or clsid */
224 if (*pszAssoc == '.')
226 /* for a file extension, the progid is the default value */
227 hr = ASSOC_GetValue(This->hkeySource, NULL, (void**)&progId, NULL);
228 if (FAILED(hr))
229 return S_OK;
231 else /* if (*pszAssoc == '{') */
233 HKEY progIdKey;
234 /* for a clsid, the progid is the default value of the ProgID subkey */
235 ret = RegOpenKeyExW(This->hkeySource, L"ProgID", 0, KEY_READ, &progIdKey);
236 if (ret != ERROR_SUCCESS)
237 return S_OK;
238 hr = ASSOC_GetValue(progIdKey, NULL, (void**)&progId, NULL);
239 if (FAILED(hr))
240 return S_OK;
241 RegCloseKey(progIdKey);
244 /* open the actual progid key, the one with the shell subkey */
245 ret = RegOpenKeyExW(HKEY_CLASSES_ROOT,
246 progId,
248 KEY_READ,
249 &This->hkeyProgID);
250 heap_free(progId);
252 return S_OK;
254 else if (hkeyProgid != NULL)
256 /* reopen the key so we don't end up closing a key owned by the caller */
257 RegOpenKeyExW(hkeyProgid, NULL, 0, KEY_READ, &This->hkeyProgID);
258 This->hkeySource = This->hkeyProgID;
259 return S_OK;
261 else
262 return E_INVALIDARG;
265 static HRESULT ASSOC_GetValue(HKEY hkey, const WCHAR *name, void **data, DWORD *data_size)
267 DWORD size;
268 LONG ret;
270 ret = RegQueryValueExW(hkey, name, 0, NULL, NULL, &size);
271 if (ret != ERROR_SUCCESS)
272 return HRESULT_FROM_WIN32(ret);
273 if (!size)
274 return E_FAIL;
275 *data = heap_alloc(size);
276 if (!*data)
277 return E_OUTOFMEMORY;
278 ret = RegQueryValueExW(hkey, name, 0, NULL, (LPBYTE)*data, &size);
279 if (ret != ERROR_SUCCESS)
281 heap_free(*data);
282 return HRESULT_FROM_WIN32(ret);
284 if(data_size)
285 *data_size = size;
286 return S_OK;
289 static HRESULT ASSOC_GetCommand(IQueryAssociationsImpl *This, const WCHAR *extra, WCHAR **command)
291 HKEY hkeyCommand;
292 HKEY hkeyShell;
293 HKEY hkeyVerb;
294 HRESULT hr;
295 LONG ret;
296 WCHAR *extra_from_reg = NULL;
297 WCHAR *filetype;
299 /* When looking for file extension it's possible to have a default value
300 that points to another key that contains 'shell/<verb>/command' subtree. */
301 hr = ASSOC_GetValue(This->hkeySource, NULL, (void**)&filetype, NULL);
302 if (hr == S_OK)
304 HKEY hkeyFile;
306 ret = RegOpenKeyExW(HKEY_CLASSES_ROOT, filetype, 0, KEY_READ, &hkeyFile);
307 heap_free(filetype);
309 if (ret == ERROR_SUCCESS)
311 ret = RegOpenKeyExW(hkeyFile, L"shell", 0, KEY_READ, &hkeyShell);
312 RegCloseKey(hkeyFile);
314 else
315 ret = RegOpenKeyExW(This->hkeySource, L"shell", 0, KEY_READ, &hkeyShell);
317 else
318 ret = RegOpenKeyExW(This->hkeySource, L"shell", 0, KEY_READ, &hkeyShell);
320 if (ret) return HRESULT_FROM_WIN32(ret);
322 if (!extra)
324 /* check for default verb */
325 hr = ASSOC_GetValue(hkeyShell, NULL, (void**)&extra_from_reg, NULL);
326 if (FAILED(hr))
328 /* no default verb, try first subkey */
329 DWORD max_subkey_len;
331 ret = RegQueryInfoKeyW(hkeyShell, NULL, NULL, NULL, NULL, &max_subkey_len, NULL, NULL, NULL, NULL, NULL, NULL);
332 if (ret)
334 RegCloseKey(hkeyShell);
335 return HRESULT_FROM_WIN32(ret);
338 max_subkey_len++;
339 extra_from_reg = heap_alloc(max_subkey_len * sizeof(WCHAR));
340 if (!extra_from_reg)
342 RegCloseKey(hkeyShell);
343 return E_OUTOFMEMORY;
346 ret = RegEnumKeyExW(hkeyShell, 0, extra_from_reg, &max_subkey_len, NULL, NULL, NULL, NULL);
347 if (ret)
349 heap_free(extra_from_reg);
350 RegCloseKey(hkeyShell);
351 return HRESULT_FROM_WIN32(ret);
354 extra = extra_from_reg;
357 /* open verb subkey */
358 ret = RegOpenKeyExW(hkeyShell, extra, 0, KEY_READ, &hkeyVerb);
359 heap_free(extra_from_reg);
360 RegCloseKey(hkeyShell);
361 if (ret) return HRESULT_FROM_WIN32(ret);
363 /* open command subkey */
364 ret = RegOpenKeyExW(hkeyVerb, L"command", 0, KEY_READ, &hkeyCommand);
365 RegCloseKey(hkeyVerb);
366 if (ret) return HRESULT_FROM_WIN32(ret);
368 hr = ASSOC_GetValue(hkeyCommand, NULL, (void**)command, NULL);
369 RegCloseKey(hkeyCommand);
370 return hr;
373 static HRESULT ASSOC_GetExecutable(IQueryAssociationsImpl *This,
374 LPCWSTR pszExtra, LPWSTR path,
375 DWORD pathlen, DWORD *len)
377 WCHAR *pszCommand;
378 WCHAR *pszStart;
379 WCHAR *pszEnd;
380 HRESULT hr;
382 hr = ASSOC_GetCommand(This, pszExtra, &pszCommand);
383 if (FAILED(hr))
384 return hr;
386 /* cleanup pszCommand */
387 if (pszCommand[0] == '"')
389 pszStart = pszCommand + 1;
390 pszEnd = wcschr(pszStart, '"');
391 if (pszEnd)
392 *pszEnd = 0;
393 *len = SearchPathW(NULL, pszStart, NULL, pathlen, path, NULL);
395 else
397 pszStart = pszCommand;
398 for (pszEnd = pszStart; (pszEnd = wcschr(pszEnd, ' ')); pszEnd++)
400 WCHAR c = *pszEnd;
401 *pszEnd = 0;
402 if ((*len = SearchPathW(NULL, pszStart, NULL, pathlen, path, NULL)))
403 break;
404 *pszEnd = c;
406 if (!pszEnd)
407 *len = SearchPathW(NULL, pszStart, NULL, pathlen, path, NULL);
410 heap_free(pszCommand);
411 if (!*len)
412 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
413 return S_OK;
416 static HRESULT ASSOC_ReturnData(void *out, DWORD *outlen, const void *data,
417 DWORD datalen)
419 if (out)
421 if (*outlen < datalen)
423 *outlen = datalen;
424 return E_POINTER;
426 *outlen = datalen;
427 memcpy(out, data, datalen);
428 return S_OK;
430 else
432 *outlen = datalen;
433 return S_FALSE;
437 static HRESULT ASSOC_ReturnString(ASSOCF flags, LPWSTR out, DWORD *outlen, LPCWSTR data, DWORD datalen)
439 HRESULT hr = S_OK;
440 DWORD len;
442 TRACE("flags=0x%08lx, data=%s\n", flags, debugstr_w(data));
444 if (!out)
446 *outlen = datalen;
447 return S_FALSE;
450 if (*outlen < datalen)
452 if (flags & ASSOCF_NOTRUNCATE)
454 len = 0;
455 if (*outlen > 0) out[0] = 0;
456 hr = E_POINTER;
458 else
460 len = min(*outlen, datalen);
461 hr = E_NOT_SUFFICIENT_BUFFER;
463 *outlen = datalen;
465 else
466 len = datalen;
468 if (len)
469 memcpy(out, data, len*sizeof(WCHAR));
471 return hr;
474 /**************************************************************************
475 * IQueryAssociations_GetString
477 * Get a file association string from the registry.
479 * PARAMS
480 * iface [I] IQueryAssociations interface to query
481 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
482 * str [I] Type of string to get (ASSOCSTR enum from "shlwapi.h")
483 * pszExtra [I] Extra information about the string location
484 * pszOut [O] Destination for the association string
485 * pcchOut [I/O] Length of pszOut
487 * RETURNS
488 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
489 * Failure: An HRESULT error code indicating the error.
491 static HRESULT WINAPI IQueryAssociations_fnGetString(
492 IQueryAssociations *iface,
493 ASSOCF flags,
494 ASSOCSTR str,
495 LPCWSTR pszExtra,
496 LPWSTR pszOut,
497 DWORD *pcchOut)
499 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
500 const ASSOCF unimplemented_flags = ~ASSOCF_NOTRUNCATE;
501 DWORD len = 0;
502 HRESULT hr;
503 WCHAR path[MAX_PATH];
505 TRACE("(%p)->(0x%08lx, %u, %s, %p, %p)\n", This, flags, str, debugstr_w(pszExtra), pszOut, pcchOut);
507 if (flags & unimplemented_flags)
508 FIXME("%08lx: unimplemented flags\n", flags & unimplemented_flags);
510 if (!pcchOut)
511 return E_UNEXPECTED;
513 if (!This->hkeySource && !This->hkeyProgID)
514 return HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION);
516 switch (str)
518 case ASSOCSTR_COMMAND:
520 WCHAR *command;
521 hr = ASSOC_GetCommand(This, pszExtra, &command);
522 if (SUCCEEDED(hr))
524 hr = ASSOC_ReturnString(flags, pszOut, pcchOut, command, lstrlenW(command) + 1);
525 heap_free(command);
527 return hr;
530 case ASSOCSTR_EXECUTABLE:
532 hr = ASSOC_GetExecutable(This, pszExtra, path, MAX_PATH, &len);
533 if (FAILED(hr))
534 return hr;
535 len++;
536 return ASSOC_ReturnString(flags, pszOut, pcchOut, path, len);
539 case ASSOCSTR_FRIENDLYDOCNAME:
541 WCHAR *docName;
543 hr = ASSOC_GetValue(This->hkeyProgID, NULL, (void**)&docName, NULL);
544 if (FAILED(hr)) {
545 /* hKeyProgID is NULL or there is no default value, so fail */
546 return HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION);
548 hr = ASSOC_ReturnString(flags, pszOut, pcchOut, docName, lstrlenW(docName) + 1);
549 heap_free(docName);
550 return hr;
553 case ASSOCSTR_FRIENDLYAPPNAME:
555 PVOID verinfoW = NULL;
556 DWORD size, retval = 0;
557 UINT flen;
558 WCHAR *bufW;
559 WCHAR fileDescW[41];
561 hr = ASSOC_GetExecutable(This, pszExtra, path, MAX_PATH, &len);
562 if (FAILED(hr))
563 return hr;
565 retval = GetFileVersionInfoSizeW(path, &size);
566 if (!retval)
567 goto get_friendly_name_fail;
568 verinfoW = heap_alloc_zero(retval);
569 if (!verinfoW)
570 return E_OUTOFMEMORY;
571 if (!GetFileVersionInfoW(path, 0, retval, verinfoW))
572 goto get_friendly_name_fail;
573 if (VerQueryValueW(verinfoW, L"\\VarFileInfo\\Translation", (LPVOID *)&bufW, &flen))
575 UINT i;
576 DWORD *langCodeDesc = (DWORD *)bufW;
577 for (i = 0; i < flen / sizeof(DWORD); i++)
579 swprintf(fileDescW, ARRAY_SIZE(fileDescW), L"\\StringFileInfo\\%04x%04x\\FileDescription",
580 LOWORD(langCodeDesc[i]), HIWORD(langCodeDesc[i]));
581 if (VerQueryValueW(verinfoW, fileDescW, (LPVOID *)&bufW, &flen))
583 /* Does lstrlenW(bufW) == 0 mean we use the filename? */
584 len = lstrlenW(bufW) + 1;
585 TRACE("found FileDescription: %s\n", debugstr_w(bufW));
586 hr = ASSOC_ReturnString(flags, pszOut, pcchOut, bufW, len);
587 heap_free(verinfoW);
588 return hr;
592 get_friendly_name_fail:
593 PathRemoveExtensionW(path);
594 PathStripPathW(path);
595 TRACE("using filename: %s\n", debugstr_w(path));
596 hr = ASSOC_ReturnString(flags, pszOut, pcchOut, path, lstrlenW(path) + 1);
597 heap_free(verinfoW);
598 return hr;
601 case ASSOCSTR_CONTENTTYPE:
603 WCHAR *contentType;
604 DWORD ret;
605 DWORD size;
607 size = 0;
608 ret = RegGetValueW(This->hkeySource, NULL, L"Content Type", RRF_RT_REG_SZ, NULL, NULL, &size);
609 if (ret != ERROR_SUCCESS)
610 return HRESULT_FROM_WIN32(ret);
611 contentType = heap_alloc(size);
612 if (contentType != NULL)
614 ret = RegGetValueW(This->hkeySource, NULL, L"Content Type", RRF_RT_REG_SZ, NULL, contentType, &size);
615 if (ret == ERROR_SUCCESS)
616 hr = ASSOC_ReturnString(flags, pszOut, pcchOut, contentType, lstrlenW(contentType) + 1);
617 else
618 hr = HRESULT_FROM_WIN32(ret);
619 heap_free(contentType);
621 else
622 hr = E_OUTOFMEMORY;
623 return hr;
626 case ASSOCSTR_DEFAULTICON:
628 DWORD ret;
629 DWORD size;
631 size = 0;
632 ret = RegGetValueW(This->hkeyProgID, L"DefaultIcon", NULL, RRF_RT_REG_SZ, NULL, NULL, &size);
633 if (ret == ERROR_SUCCESS)
635 WCHAR *icon = heap_alloc(size);
636 if (icon)
638 ret = RegGetValueW(This->hkeyProgID, L"DefaultIcon", NULL, RRF_RT_REG_SZ, NULL, icon, &size);
639 if (ret == ERROR_SUCCESS)
640 hr = ASSOC_ReturnString(flags, pszOut, pcchOut, icon, lstrlenW(icon) + 1);
641 else
642 hr = HRESULT_FROM_WIN32(ret);
643 heap_free(icon);
645 else
646 hr = E_OUTOFMEMORY;
647 } else {
648 /* there is no DefaultIcon subkey or hkeyProgID is NULL, so return the default document icon */
649 if (This->hkeyProgID == NULL)
650 hr = ASSOC_ReturnString(flags, pszOut, pcchOut, L"shell32.dll,0", lstrlenW(L"shell32.dll,0") + 1);
651 else
652 return HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION);
654 return hr;
656 case ASSOCSTR_SHELLEXTENSION:
658 WCHAR keypath[ARRAY_SIZE(L"ShellEx\\") + 39], guid[39];
659 CLSID clsid;
660 HKEY hkey;
661 DWORD size;
662 LONG ret;
664 hr = CLSIDFromString(pszExtra, &clsid);
665 if (FAILED(hr)) return hr;
667 lstrcpyW(keypath, L"ShellEx\\");
668 lstrcatW(keypath, pszExtra);
669 ret = RegOpenKeyExW(This->hkeySource, keypath, 0, KEY_READ, &hkey);
670 if (ret) return HRESULT_FROM_WIN32(ret);
672 size = sizeof(guid);
673 ret = RegGetValueW(hkey, NULL, NULL, RRF_RT_REG_SZ, NULL, guid, &size);
674 RegCloseKey(hkey);
675 if (ret) return HRESULT_FROM_WIN32(ret);
677 return ASSOC_ReturnString(flags, pszOut, pcchOut, guid, size / sizeof(WCHAR));
680 default:
681 FIXME("assocstr %d unimplemented!\n", str);
682 return E_NOTIMPL;
686 /**************************************************************************
687 * IQueryAssociations_GetKey
689 * Get a file association key from the registry.
691 * PARAMS
692 * iface [I] IQueryAssociations interface to query
693 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
694 * assockey [I] Type of key to get (ASSOCKEY enum from "shlwapi.h")
695 * pszExtra [I] Extra information about the key location
696 * phkeyOut [O] Destination for the association key
698 * RETURNS
699 * Success: S_OK. phkeyOut contains a handle to the key.
700 * Failure: An HRESULT error code indicating the error.
702 static HRESULT WINAPI IQueryAssociations_fnGetKey(
703 IQueryAssociations *iface,
704 ASSOCF cfFlags,
705 ASSOCKEY assockey,
706 LPCWSTR pszExtra,
707 HKEY *phkeyOut)
709 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
711 FIXME("(%p,0x%8lx,0x%8x,%s,%p)-stub!\n", This, cfFlags, assockey,
712 debugstr_w(pszExtra), phkeyOut);
713 return E_NOTIMPL;
716 /**************************************************************************
717 * IQueryAssociations_GetData
719 * Get the data for a file association key from the registry.
721 * PARAMS
722 * iface [I] IQueryAssociations interface to query
723 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
724 * assocdata [I] Type of data to get (ASSOCDATA enum from "shlwapi.h")
725 * pszExtra [I] Extra information about the data location
726 * pvOut [O] Destination for the association key
727 * pcbOut [I/O] Size of pvOut
729 * RETURNS
730 * Success: S_OK. pszOut contains the data, pcbOut contains its length.
731 * Failure: An HRESULT error code indicating the error.
733 static HRESULT WINAPI IQueryAssociations_fnGetData(IQueryAssociations *iface,
734 ASSOCF cfFlags, ASSOCDATA assocdata, LPCWSTR pszExtra, LPVOID pvOut,
735 DWORD *pcbOut)
737 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
738 void *data = NULL;
739 DWORD size;
740 HRESULT hres;
742 TRACE("(%p,0x%8lx,0x%8x,%s,%p,%p)\n", This, cfFlags, assocdata,
743 debugstr_w(pszExtra), pvOut, pcbOut);
745 if(cfFlags)
746 FIXME("Unsupported flags: %lx\n", cfFlags);
748 switch(assocdata) {
749 case ASSOCDATA_EDITFLAGS:
750 if(!This->hkeyProgID)
751 return HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION);
753 hres = ASSOC_GetValue(This->hkeyProgID, L"EditFlags", &data, &size);
754 if(SUCCEEDED(hres) && pcbOut)
755 hres = ASSOC_ReturnData(pvOut, pcbOut, data, size);
756 heap_free(data);
757 return hres;
758 default:
759 FIXME("Unsupported ASSOCDATA value: %d\n", assocdata);
760 return E_NOTIMPL;
764 /**************************************************************************
765 * IQueryAssociations_GetEnum
767 * Not yet implemented in native Win32.
769 * PARAMS
770 * iface [I] IQueryAssociations interface to query
771 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
772 * assocenum [I] Type of enum to get (ASSOCENUM enum from "shlwapi.h")
773 * pszExtra [I] Extra information about the enum location
774 * riid [I] REFIID to look for
775 * ppvOut [O] Destination for the interface.
777 * RETURNS
778 * Success: S_OK.
779 * Failure: An HRESULT error code indicating the error.
781 * NOTES
782 * Presumably this function returns an enumerator object.
784 static HRESULT WINAPI IQueryAssociations_fnGetEnum(
785 IQueryAssociations *iface,
786 ASSOCF cfFlags,
787 ASSOCENUM assocenum,
788 LPCWSTR pszExtra,
789 REFIID riid,
790 LPVOID *ppvOut)
792 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
794 FIXME("(%p,0x%8lx,0x%8x,%s,%s,%p)-stub!\n", This, cfFlags, assocenum,
795 debugstr_w(pszExtra), debugstr_guid(riid), ppvOut);
796 return E_NOTIMPL;
799 static const IQueryAssociationsVtbl IQueryAssociations_vtbl =
801 IQueryAssociations_fnQueryInterface,
802 IQueryAssociations_fnAddRef,
803 IQueryAssociations_fnRelease,
804 IQueryAssociations_fnInit,
805 IQueryAssociations_fnGetString,
806 IQueryAssociations_fnGetKey,
807 IQueryAssociations_fnGetData,
808 IQueryAssociations_fnGetEnum
811 /**************************************************************************
812 * IApplicationAssociationRegistration implementation
814 static inline IApplicationAssociationRegistrationImpl *impl_from_IApplicationAssociationRegistration(IApplicationAssociationRegistration *iface)
816 return CONTAINING_RECORD(iface, IApplicationAssociationRegistrationImpl, IApplicationAssociationRegistration_iface);
819 static HRESULT WINAPI ApplicationAssociationRegistration_QueryInterface(
820 IApplicationAssociationRegistration* iface, REFIID riid, LPVOID *ppv)
822 IApplicationAssociationRegistrationImpl *This = impl_from_IApplicationAssociationRegistration(iface);
824 TRACE("(%p, %s, %p)\n",This, debugstr_guid(riid), ppv);
826 if (ppv == NULL)
827 return E_POINTER;
829 if (IsEqualGUID(&IID_IUnknown, riid) ||
830 IsEqualGUID(&IID_IApplicationAssociationRegistration, riid)) {
831 *ppv = &This->IApplicationAssociationRegistration_iface;
832 IUnknown_AddRef((IUnknown*)*ppv);
833 TRACE("returning IApplicationAssociationRegistration: %p\n", *ppv);
834 return S_OK;
837 *ppv = NULL;
838 FIXME("(%p)->(%s %p) interface not supported\n", This, debugstr_guid(riid), ppv);
839 return E_NOINTERFACE;
842 static ULONG WINAPI ApplicationAssociationRegistration_AddRef(IApplicationAssociationRegistration *iface)
844 IApplicationAssociationRegistrationImpl *This = impl_from_IApplicationAssociationRegistration(iface);
845 ULONG ref = InterlockedIncrement(&This->ref);
847 TRACE("(%p) ref=%ld\n", This, ref);
848 return ref;
851 static ULONG WINAPI ApplicationAssociationRegistration_Release(IApplicationAssociationRegistration *iface)
853 IApplicationAssociationRegistrationImpl *This = impl_from_IApplicationAssociationRegistration(iface);
854 ULONG ref = InterlockedDecrement(&This->ref);
856 TRACE("(%p) ref=%ld\n", This, ref);
858 if (!ref) {
859 SHFree(This);
861 return ref;
864 static HRESULT WINAPI ApplicationAssociationRegistration_QueryCurrentDefault(IApplicationAssociationRegistration *iface, LPCWSTR query,
865 ASSOCIATIONTYPE type, ASSOCIATIONLEVEL level, LPWSTR *association)
867 IApplicationAssociationRegistrationImpl *This = impl_from_IApplicationAssociationRegistration(iface);
868 WCHAR path[MAX_PATH];
869 DWORD ret, keytype, size;
870 HKEY hkey = NULL;
871 HRESULT hr = HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION);
873 TRACE("(%p)->(%s, %d, %d, %p)\n", This, debugstr_w(query), type, level, association);
875 if(!association)
876 return E_INVALIDARG;
878 *association = NULL;
880 if((type == AT_URLPROTOCOL || type == AT_FILEEXTENSION) && !query[0])
881 return E_INVALIDARG;
882 else if(type == AT_FILEEXTENSION && query[0] != '.')
883 return E_INVALIDARG;
885 if(type == AT_FILEEXTENSION)
887 ret = RegOpenKeyExW(HKEY_CLASSES_ROOT, query, 0, KEY_READ, &hkey);
888 if(ret == ERROR_SUCCESS)
890 ret = RegGetValueW(hkey, NULL, NULL, RRF_RT_REG_SZ, &keytype, NULL, &size);
891 if(ret == ERROR_SUCCESS)
893 *association = CoTaskMemAlloc(size);
894 if(*association)
896 ret = RegGetValueW(hkey, NULL, NULL, RRF_RT_REG_SZ, &keytype, *association, &size);
897 if(ret == ERROR_SUCCESS)
898 hr = S_OK;
899 else
901 CoTaskMemFree(*association);
902 *association = NULL;
905 else
906 hr = E_OUTOFMEMORY;
910 else
912 ret = RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\Shell\\Associations", 0, KEY_READ, &hkey);
913 if(ret == ERROR_SUCCESS)
915 if(type == AT_URLPROTOCOL)
916 swprintf( path, ARRAY_SIZE(path), L"UrlAssociations\\%s\\UserChoice", query );
917 else if(type == AT_MIMETYPE)
918 swprintf( path, ARRAY_SIZE(path), L"MIMEAssociations\\%s\\UserChoice", query );
919 else
921 WARN("Unsupported type (%d).\n", type);
922 RegCloseKey(hkey);
923 return hr;
926 ret = RegGetValueW(hkey, path, L"Progid", RRF_RT_REG_SZ, &keytype, NULL, &size);
927 if(ret == ERROR_SUCCESS)
929 *association = CoTaskMemAlloc(size);
930 if(*association)
932 ret = RegGetValueW(hkey, path, L"Progid", RRF_RT_REG_SZ, &keytype, *association, &size);
933 if(ret == ERROR_SUCCESS)
934 hr = S_OK;
935 else
937 CoTaskMemFree(*association);
938 *association = NULL;
941 else
942 hr = E_OUTOFMEMORY;
947 RegCloseKey(hkey);
949 return hr;
952 static HRESULT WINAPI ApplicationAssociationRegistration_QueryAppIsDefault(IApplicationAssociationRegistration* This, LPCWSTR query,
953 ASSOCIATIONTYPE type, ASSOCIATIONLEVEL level, LPCWSTR appname, BOOL *is_default)
955 FIXME("(%p)->(%s, %d, %d, %s, %p)\n", This, debugstr_w(query), type, level, debugstr_w(appname), is_default);
956 return E_NOTIMPL;
959 static HRESULT WINAPI ApplicationAssociationRegistration_QueryAppIsDefaultAll(IApplicationAssociationRegistration* This, ASSOCIATIONLEVEL level,
960 LPCWSTR appname, BOOL *is_default)
962 FIXME("(%p)->(%d, %s, %p)\n", This, level, debugstr_w(appname), is_default);
963 return E_NOTIMPL;
966 static HRESULT WINAPI ApplicationAssociationRegistration_SetAppAsDefault(IApplicationAssociationRegistration* This, LPCWSTR appname,
967 LPCWSTR set, ASSOCIATIONTYPE set_type)
969 FIXME("(%p)->(%s, %s, %d)\n", This, debugstr_w(appname), debugstr_w(set), set_type);
970 return E_NOTIMPL;
973 static HRESULT WINAPI ApplicationAssociationRegistration_SetAppAsDefaultAll(IApplicationAssociationRegistration* This, LPCWSTR appname)
975 FIXME("(%p)->(%s)\n", This, debugstr_w(appname));
976 return E_NOTIMPL;
980 static HRESULT WINAPI ApplicationAssociationRegistration_ClearUserAssociations(IApplicationAssociationRegistration* This)
982 FIXME("(%p)\n", This);
983 return E_NOTIMPL;
987 static const IApplicationAssociationRegistrationVtbl IApplicationAssociationRegistration_vtbl =
989 ApplicationAssociationRegistration_QueryInterface,
990 ApplicationAssociationRegistration_AddRef,
991 ApplicationAssociationRegistration_Release,
992 ApplicationAssociationRegistration_QueryCurrentDefault,
993 ApplicationAssociationRegistration_QueryAppIsDefault,
994 ApplicationAssociationRegistration_QueryAppIsDefaultAll,
995 ApplicationAssociationRegistration_SetAppAsDefault,
996 ApplicationAssociationRegistration_SetAppAsDefaultAll,
997 ApplicationAssociationRegistration_ClearUserAssociations
1000 /**************************************************************************
1001 * IQueryAssociations_Constructor [internal]
1003 * Construct a new IQueryAssociations object.
1005 HRESULT WINAPI QueryAssociations_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppOutput)
1007 IQueryAssociationsImpl* this;
1008 HRESULT ret;
1010 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1012 if (!(this = SHAlloc(sizeof(*this)))) return E_OUTOFMEMORY;
1013 this->IQueryAssociations_iface.lpVtbl = &IQueryAssociations_vtbl;
1014 this->ref = 0;
1015 this->hkeySource = 0;
1016 this->hkeyProgID = 0;
1017 if (FAILED(ret = IQueryAssociations_QueryInterface(&this->IQueryAssociations_iface, riid, ppOutput))) SHFree( this );
1018 TRACE("returning %p\n", *ppOutput);
1019 return ret;
1022 /**************************************************************************
1023 * ApplicationAssociationRegistration_Constructor [internal]
1025 * Construct a IApplicationAssociationRegistration object.
1027 HRESULT WINAPI ApplicationAssociationRegistration_Constructor(IUnknown *outer, REFIID riid, LPVOID *ppv)
1029 IApplicationAssociationRegistrationImpl *This;
1030 HRESULT hr;
1032 if (outer)
1033 return CLASS_E_NOAGGREGATION;
1035 if (!(This = SHAlloc(sizeof(*This))))
1036 return E_OUTOFMEMORY;
1038 This->IApplicationAssociationRegistration_iface.lpVtbl = &IApplicationAssociationRegistration_vtbl;
1039 This->ref = 0;
1041 hr = IApplicationAssociationRegistration_QueryInterface(&This->IApplicationAssociationRegistration_iface, riid, ppv);
1042 if (FAILED(hr))
1043 SHFree(This);
1045 TRACE("returning 0x%lx with %p\n", hr, *ppv);
1046 return hr;
1049 static HRESULT WINAPI enumassochandlers_QueryInterface(IEnumAssocHandlers *iface, REFIID riid, void **obj)
1051 struct enumassochandlers *This = impl_from_IEnumAssocHandlers(iface);
1053 TRACE("(%p %s %p)\n", This, debugstr_guid(riid), obj);
1055 if (IsEqualIID(riid, &IID_IEnumAssocHandlers) ||
1056 IsEqualIID(riid, &IID_IUnknown))
1058 *obj = iface;
1059 IEnumAssocHandlers_AddRef(iface);
1060 return S_OK;
1063 *obj = NULL;
1064 return E_NOINTERFACE;
1067 static ULONG WINAPI enumassochandlers_AddRef(IEnumAssocHandlers *iface)
1069 struct enumassochandlers *This = impl_from_IEnumAssocHandlers(iface);
1070 ULONG ref = InterlockedIncrement(&This->ref);
1072 TRACE("(%p)->(%lu)\n", This, ref);
1073 return ref;
1076 static ULONG WINAPI enumassochandlers_Release(IEnumAssocHandlers *iface)
1078 struct enumassochandlers *This = impl_from_IEnumAssocHandlers(iface);
1079 ULONG ref = InterlockedDecrement(&This->ref);
1081 TRACE("(%p)->(%lu)\n", This, ref);
1083 if (!ref)
1084 SHFree(This);
1086 return ref;
1089 static HRESULT WINAPI enumassochandlers_Next(IEnumAssocHandlers *iface, ULONG count, IAssocHandler **handlers,
1090 ULONG *fetched)
1092 struct enumassochandlers *This = impl_from_IEnumAssocHandlers(iface);
1094 FIXME("(%p)->(%lu %p %p): stub\n", This, count, handlers, fetched);
1096 return E_NOTIMPL;
1099 static const IEnumAssocHandlersVtbl enumassochandlersvtbl = {
1100 enumassochandlers_QueryInterface,
1101 enumassochandlers_AddRef,
1102 enumassochandlers_Release,
1103 enumassochandlers_Next
1106 /**************************************************************************
1107 * SHCreateAssociationRegistration [SHELL32.@]
1109 HRESULT WINAPI SHCreateAssociationRegistration(REFIID riid, LPVOID *ppv)
1111 return ApplicationAssociationRegistration_Constructor(NULL, riid, ppv);
1114 /**************************************************************************
1115 * SHAssocEnumHandlers [SHELL32.@]
1117 HRESULT WINAPI SHAssocEnumHandlers(const WCHAR *extra, ASSOC_FILTER filter, IEnumAssocHandlers **enumhandlers)
1119 struct enumassochandlers *enumassoc;
1121 FIXME("(%s %d %p): stub\n", debugstr_w(extra), filter, enumhandlers);
1123 *enumhandlers = NULL;
1125 enumassoc = SHAlloc(sizeof(*enumassoc));
1126 if (!enumassoc)
1127 return E_OUTOFMEMORY;
1129 enumassoc->IEnumAssocHandlers_iface.lpVtbl = &enumassochandlersvtbl;
1130 enumassoc->ref = 1;
1132 *enumhandlers = &enumassoc->IEnumAssocHandlers_iface;
1133 return S_OK;