TESTING -- override pthreads to fix gstreamer v5
[wine/multimedia.git] / dlls / shell32 / assoc.c
blob90d00a84213946d27904d9644d1f533a3a5d6057
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 /**************************************************************************
92 * IQueryAssociations_QueryInterface
94 * See IUnknown_QueryInterface.
96 static HRESULT WINAPI IQueryAssociations_fnQueryInterface(
97 IQueryAssociations* iface,
98 REFIID riid,
99 LPVOID *ppvObj)
101 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
103 TRACE("(%p,%s,%p)\n",This, debugstr_guid(riid), ppvObj);
105 if (ppvObj == NULL)
106 return E_POINTER;
108 *ppvObj = NULL;
110 if (IsEqualIID(riid, &IID_IUnknown) ||
111 IsEqualIID(riid, &IID_IQueryAssociations))
113 *ppvObj = &This->IQueryAssociations_iface;
115 IQueryAssociations_AddRef((IQueryAssociations*)*ppvObj);
116 TRACE("Returning IQueryAssociations (%p)\n", *ppvObj);
117 return S_OK;
119 TRACE("Returning E_NOINTERFACE\n");
120 return E_NOINTERFACE;
123 /**************************************************************************
124 * IQueryAssociations_AddRef
126 * See IUnknown_AddRef.
128 static ULONG WINAPI IQueryAssociations_fnAddRef(IQueryAssociations *iface)
130 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
131 ULONG refCount = InterlockedIncrement(&This->ref);
133 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
135 return refCount;
138 /**************************************************************************
139 * IQueryAssociations_Release
141 * See IUnknown_Release.
143 static ULONG WINAPI IQueryAssociations_fnRelease(IQueryAssociations *iface)
145 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
146 ULONG refCount = InterlockedDecrement(&This->ref);
148 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
150 if (!refCount)
152 TRACE("Destroying IQueryAssociations (%p)\n", This);
153 RegCloseKey(This->hkeySource);
154 RegCloseKey(This->hkeyProgID);
155 SHFree(This);
158 return refCount;
161 /**************************************************************************
162 * IQueryAssociations_Init
164 * Initialise an IQueryAssociations object.
166 * PARAMS
167 * iface [I] IQueryAssociations interface to initialise
168 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
169 * pszAssoc [I] String for the root key name, or NULL if hkeyProgid is given
170 * hkeyProgid [I] Handle for the root key, or NULL if pszAssoc is given
171 * hWnd [I] Reserved, must be NULL.
173 * RETURNS
174 * Success: S_OK. iface is initialised with the parameters given.
175 * Failure: An HRESULT error code indicating the error.
177 static HRESULT WINAPI IQueryAssociations_fnInit(
178 IQueryAssociations *iface,
179 ASSOCF cfFlags,
180 LPCWSTR pszAssoc,
181 HKEY hkeyProgid,
182 HWND hWnd)
184 static const WCHAR szProgID[] = {'P','r','o','g','I','D',0};
185 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
186 LONG ret;
188 TRACE("(%p)->(%d,%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: %x\n", cfFlags);
198 RegCloseKey(This->hkeySource);
199 RegCloseKey(This->hkeyProgID);
200 This->hkeySource = This->hkeyProgID = NULL;
201 if (pszAssoc != NULL)
203 ret = RegOpenKeyExW(HKEY_CLASSES_ROOT,
204 pszAssoc,
206 KEY_READ,
207 &This->hkeySource);
208 if (ret)
209 return S_OK;
210 /* if this is not a prog id */
211 if ((*pszAssoc == '.') || (*pszAssoc == '{'))
213 RegOpenKeyExW(This->hkeySource,
214 szProgID,
216 KEY_READ,
217 &This->hkeyProgID);
219 else
220 This->hkeyProgID = This->hkeySource;
221 return S_OK;
223 else if (hkeyProgid != NULL)
225 This->hkeyProgID = hkeyProgid;
226 return S_OK;
228 else
229 return E_INVALIDARG;
232 static HRESULT ASSOC_GetValue(HKEY hkey, const WCHAR *name, void **data, DWORD *data_size)
234 DWORD size;
235 LONG ret;
237 ret = RegQueryValueExW(hkey, name, 0, NULL, NULL, &size);
238 if (ret != ERROR_SUCCESS)
239 return HRESULT_FROM_WIN32(ret);
240 if (!size)
241 return E_FAIL;
242 *data = HeapAlloc(GetProcessHeap(), 0, size);
243 if (!*data)
244 return E_OUTOFMEMORY;
245 ret = RegQueryValueExW(hkey, name, 0, NULL, (LPBYTE)*data, &size);
246 if (ret != ERROR_SUCCESS)
248 HeapFree(GetProcessHeap(), 0, *data);
249 return HRESULT_FROM_WIN32(ret);
251 if(data_size)
252 *data_size = size;
253 return S_OK;
256 static HRESULT ASSOC_GetCommand(IQueryAssociationsImpl *This, const WCHAR *extra, WCHAR **command)
258 HKEY hkeyCommand;
259 HKEY hkeyShell;
260 HKEY hkeyVerb;
261 HRESULT hr;
262 LONG ret;
263 WCHAR *extra_from_reg = NULL;
264 WCHAR *filetype;
265 static const WCHAR commandW[] = { 'c','o','m','m','a','n','d',0 };
266 static const WCHAR shellW[] = { 's','h','e','l','l',0 };
268 /* When looking for file extension it's possible to have a default value
269 that points to another key that contains 'shell/<verb>/command' subtree. */
270 hr = ASSOC_GetValue(This->hkeySource, NULL, (void**)&filetype, NULL);
271 if (hr == S_OK)
273 HKEY hkeyFile;
275 ret = RegOpenKeyExW(HKEY_CLASSES_ROOT, filetype, 0, KEY_READ, &hkeyFile);
276 HeapFree(GetProcessHeap(), 0, filetype);
278 if (ret == ERROR_SUCCESS)
280 ret = RegOpenKeyExW(hkeyFile, shellW, 0, KEY_READ, &hkeyShell);
281 RegCloseKey(hkeyFile);
283 else
284 ret = RegOpenKeyExW(This->hkeySource, shellW, 0, KEY_READ, &hkeyShell);
286 else
287 ret = RegOpenKeyExW(This->hkeySource, shellW, 0, KEY_READ, &hkeyShell);
289 if (ret) return HRESULT_FROM_WIN32(ret);
291 if (!extra)
293 /* check for default verb */
294 hr = ASSOC_GetValue(hkeyShell, NULL, (void**)&extra_from_reg, NULL);
295 if (FAILED(hr))
297 /* no default verb, try first subkey */
298 DWORD max_subkey_len;
300 ret = RegQueryInfoKeyW(hkeyShell, NULL, NULL, NULL, NULL, &max_subkey_len, NULL, NULL, NULL, NULL, NULL, NULL);
301 if (ret)
303 RegCloseKey(hkeyShell);
304 return HRESULT_FROM_WIN32(ret);
307 max_subkey_len++;
308 extra_from_reg = HeapAlloc(GetProcessHeap(), 0, max_subkey_len * sizeof(WCHAR));
309 if (!extra_from_reg)
311 RegCloseKey(hkeyShell);
312 return E_OUTOFMEMORY;
315 ret = RegEnumKeyExW(hkeyShell, 0, extra_from_reg, &max_subkey_len, NULL, NULL, NULL, NULL);
316 if (ret)
318 HeapFree(GetProcessHeap(), 0, extra_from_reg);
319 RegCloseKey(hkeyShell);
320 return HRESULT_FROM_WIN32(ret);
323 extra = extra_from_reg;
326 /* open verb subkey */
327 ret = RegOpenKeyExW(hkeyShell, extra, 0, KEY_READ, &hkeyVerb);
328 HeapFree(GetProcessHeap(), 0, extra_from_reg);
329 RegCloseKey(hkeyShell);
330 if (ret) return HRESULT_FROM_WIN32(ret);
332 /* open command subkey */
333 ret = RegOpenKeyExW(hkeyVerb, commandW, 0, KEY_READ, &hkeyCommand);
334 RegCloseKey(hkeyVerb);
335 if (ret) return HRESULT_FROM_WIN32(ret);
337 hr = ASSOC_GetValue(hkeyCommand, NULL, (void**)command, NULL);
338 RegCloseKey(hkeyCommand);
339 return hr;
342 static HRESULT ASSOC_GetExecutable(IQueryAssociationsImpl *This,
343 LPCWSTR pszExtra, LPWSTR path,
344 DWORD pathlen, DWORD *len)
346 WCHAR *pszCommand;
347 WCHAR *pszStart;
348 WCHAR *pszEnd;
349 HRESULT hr;
351 hr = ASSOC_GetCommand(This, pszExtra, &pszCommand);
352 if (FAILED(hr))
353 return hr;
355 /* cleanup pszCommand */
356 if (pszCommand[0] == '"')
358 pszStart = pszCommand + 1;
359 pszEnd = strchrW(pszStart, '"');
360 if (pszEnd)
361 *pszEnd = 0;
362 *len = SearchPathW(NULL, pszStart, NULL, pathlen, path, NULL);
364 else
366 pszStart = pszCommand;
367 for (pszEnd = pszStart; (pszEnd = strchrW(pszEnd, ' ')); pszEnd++)
369 WCHAR c = *pszEnd;
370 *pszEnd = 0;
371 if ((*len = SearchPathW(NULL, pszStart, NULL, pathlen, path, NULL)))
372 break;
373 *pszEnd = c;
375 if (!pszEnd)
376 *len = SearchPathW(NULL, pszStart, NULL, pathlen, path, NULL);
379 HeapFree(GetProcessHeap(), 0, pszCommand);
380 if (!*len)
381 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
382 return S_OK;
385 static HRESULT ASSOC_ReturnData(void *out, DWORD *outlen, const void *data,
386 DWORD datalen)
388 if (out)
390 if (*outlen < datalen)
392 *outlen = datalen;
393 return E_POINTER;
395 *outlen = datalen;
396 memcpy(out, data, datalen);
397 return S_OK;
399 else
401 *outlen = datalen;
402 return S_FALSE;
406 static HRESULT ASSOC_ReturnString(ASSOCF flags, LPWSTR out, DWORD *outlen, LPCWSTR data, DWORD datalen)
408 HRESULT hr = S_OK;
409 DWORD len;
411 TRACE("flags=0x%08x, data=%s\n", flags, debugstr_w(data));
413 if (!out)
415 *outlen = datalen;
416 return S_FALSE;
419 if (*outlen < datalen)
421 if (flags & ASSOCF_NOTRUNCATE)
423 len = 0;
424 if (*outlen > 0) out[0] = 0;
425 hr = E_POINTER;
427 else
429 len = min(*outlen, datalen);
430 hr = E_NOT_SUFFICIENT_BUFFER;
432 *outlen = datalen;
434 else
435 len = datalen;
437 if (len)
438 memcpy(out, data, len*sizeof(WCHAR));
440 return hr;
443 /**************************************************************************
444 * IQueryAssociations_GetString
446 * Get a file association string from the registry.
448 * PARAMS
449 * iface [I] IQueryAssociations interface to query
450 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
451 * str [I] Type of string to get (ASSOCSTR enum from "shlwapi.h")
452 * pszExtra [I] Extra information about the string location
453 * pszOut [O] Destination for the association string
454 * pcchOut [I/O] Length of pszOut
456 * RETURNS
457 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
458 * Failure: An HRESULT error code indicating the error.
460 static HRESULT WINAPI IQueryAssociations_fnGetString(
461 IQueryAssociations *iface,
462 ASSOCF flags,
463 ASSOCSTR str,
464 LPCWSTR pszExtra,
465 LPWSTR pszOut,
466 DWORD *pcchOut)
468 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
469 const ASSOCF unimplemented_flags = ~ASSOCF_NOTRUNCATE;
470 DWORD len = 0;
471 HRESULT hr;
472 WCHAR path[MAX_PATH];
474 TRACE("(%p)->(0x%08x, %u, %s, %p, %p)\n", This, flags, str, debugstr_w(pszExtra), pszOut, pcchOut);
476 if (flags & unimplemented_flags)
477 FIXME("%08x: unimplemented flags\n", flags & unimplemented_flags);
479 if (!pcchOut)
480 return E_UNEXPECTED;
482 if (!This->hkeySource && !This->hkeyProgID)
483 return HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION);
485 switch (str)
487 case ASSOCSTR_COMMAND:
489 WCHAR *command;
490 hr = ASSOC_GetCommand(This, pszExtra, &command);
491 if (SUCCEEDED(hr))
493 hr = ASSOC_ReturnString(flags, pszOut, pcchOut, command, strlenW(command) + 1);
494 HeapFree(GetProcessHeap(), 0, command);
496 return hr;
499 case ASSOCSTR_EXECUTABLE:
501 hr = ASSOC_GetExecutable(This, pszExtra, path, MAX_PATH, &len);
502 if (FAILED(hr))
503 return hr;
504 len++;
505 return ASSOC_ReturnString(flags, pszOut, pcchOut, path, len);
508 case ASSOCSTR_FRIENDLYDOCNAME:
510 WCHAR *pszFileType;
511 DWORD ret;
512 DWORD size;
514 hr = ASSOC_GetValue(This->hkeySource, NULL, (void**)&pszFileType, NULL);
515 if (FAILED(hr))
516 return hr;
517 size = 0;
518 ret = RegGetValueW(HKEY_CLASSES_ROOT, pszFileType, NULL, RRF_RT_REG_SZ, NULL, NULL, &size);
519 if (ret == ERROR_SUCCESS)
521 WCHAR *docName = HeapAlloc(GetProcessHeap(), 0, size);
522 if (docName)
524 ret = RegGetValueW(HKEY_CLASSES_ROOT, pszFileType, NULL, RRF_RT_REG_SZ, NULL, docName, &size);
525 if (ret == ERROR_SUCCESS)
526 hr = ASSOC_ReturnString(flags, pszOut, pcchOut, docName, strlenW(docName) + 1);
527 else
528 hr = HRESULT_FROM_WIN32(ret);
529 HeapFree(GetProcessHeap(), 0, docName);
531 else
532 hr = E_OUTOFMEMORY;
534 else
535 hr = HRESULT_FROM_WIN32(ret);
536 HeapFree(GetProcessHeap(), 0, pszFileType);
537 return hr;
540 case ASSOCSTR_FRIENDLYAPPNAME:
542 PVOID verinfoW = NULL;
543 DWORD size, retval = 0;
544 UINT flen;
545 WCHAR *bufW;
546 static const WCHAR translationW[] = {
547 '\\','V','a','r','F','i','l','e','I','n','f','o',
548 '\\','T','r','a','n','s','l','a','t','i','o','n',0
550 static const WCHAR fileDescFmtW[] = {
551 '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
552 '\\','%','0','4','x','%','0','4','x',
553 '\\','F','i','l','e','D','e','s','c','r','i','p','t','i','o','n',0
555 WCHAR fileDescW[41];
557 hr = ASSOC_GetExecutable(This, pszExtra, path, MAX_PATH, &len);
558 if (FAILED(hr))
559 return hr;
561 retval = GetFileVersionInfoSizeW(path, &size);
562 if (!retval)
563 goto get_friendly_name_fail;
564 verinfoW = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, retval);
565 if (!verinfoW)
566 return E_OUTOFMEMORY;
567 if (!GetFileVersionInfoW(path, 0, retval, verinfoW))
568 goto get_friendly_name_fail;
569 if (VerQueryValueW(verinfoW, translationW, (LPVOID *)&bufW, &flen))
571 UINT i;
572 DWORD *langCodeDesc = (DWORD *)bufW;
573 for (i = 0; i < flen / sizeof(DWORD); i++)
575 sprintfW(fileDescW, fileDescFmtW, LOWORD(langCodeDesc[i]),
576 HIWORD(langCodeDesc[i]));
577 if (VerQueryValueW(verinfoW, fileDescW, (LPVOID *)&bufW, &flen))
579 /* Does strlenW(bufW) == 0 mean we use the filename? */
580 len = strlenW(bufW) + 1;
581 TRACE("found FileDescription: %s\n", debugstr_w(bufW));
582 hr = ASSOC_ReturnString(flags, pszOut, pcchOut, bufW, len);
583 HeapFree(GetProcessHeap(), 0, verinfoW);
584 return hr;
588 get_friendly_name_fail:
589 PathRemoveExtensionW(path);
590 PathStripPathW(path);
591 TRACE("using filename: %s\n", debugstr_w(path));
592 hr = ASSOC_ReturnString(flags, pszOut, pcchOut, path, strlenW(path) + 1);
593 HeapFree(GetProcessHeap(), 0, verinfoW);
594 return hr;
597 case ASSOCSTR_CONTENTTYPE:
599 static const WCHAR Content_TypeW[] = {'C','o','n','t','e','n','t',' ','T','y','p','e',0};
600 WCHAR *contentType;
601 DWORD ret;
602 DWORD size;
604 size = 0;
605 ret = RegGetValueW(This->hkeySource, NULL, Content_TypeW, RRF_RT_REG_SZ, NULL, NULL, &size);
606 if (ret != ERROR_SUCCESS)
607 return HRESULT_FROM_WIN32(ret);
608 contentType = HeapAlloc(GetProcessHeap(), 0, size);
609 if (contentType != NULL)
611 ret = RegGetValueW(This->hkeySource, NULL, Content_TypeW, RRF_RT_REG_SZ, NULL, contentType, &size);
612 if (ret == ERROR_SUCCESS)
613 hr = ASSOC_ReturnString(flags, pszOut, pcchOut, contentType, strlenW(contentType) + 1);
614 else
615 hr = HRESULT_FROM_WIN32(ret);
616 HeapFree(GetProcessHeap(), 0, contentType);
618 else
619 hr = E_OUTOFMEMORY;
620 return hr;
623 case ASSOCSTR_DEFAULTICON:
625 static const WCHAR DefaultIconW[] = {'D','e','f','a','u','l','t','I','c','o','n',0};
626 WCHAR *pszFileType;
627 DWORD ret;
628 DWORD size;
629 HKEY hkeyFile;
631 hr = ASSOC_GetValue(This->hkeySource, NULL, (void**)&pszFileType, NULL);
632 if (FAILED(hr))
633 return hr;
634 ret = RegOpenKeyExW(HKEY_CLASSES_ROOT, pszFileType, 0, KEY_READ, &hkeyFile);
635 if (ret == ERROR_SUCCESS)
637 size = 0;
638 ret = RegGetValueW(hkeyFile, DefaultIconW, NULL, RRF_RT_REG_SZ, NULL, NULL, &size);
639 if (ret == ERROR_SUCCESS)
641 WCHAR *icon = HeapAlloc(GetProcessHeap(), 0, size);
642 if (icon)
644 ret = RegGetValueW(hkeyFile, DefaultIconW, NULL, RRF_RT_REG_SZ, NULL, icon, &size);
645 if (ret == ERROR_SUCCESS)
646 hr = ASSOC_ReturnString(flags, pszOut, pcchOut, icon, strlenW(icon) + 1);
647 else
648 hr = HRESULT_FROM_WIN32(ret);
649 HeapFree(GetProcessHeap(), 0, icon);
651 else
652 hr = E_OUTOFMEMORY;
654 else
655 hr = HRESULT_FROM_WIN32(ret);
656 RegCloseKey(hkeyFile);
658 else
659 hr = HRESULT_FROM_WIN32(ret);
660 HeapFree(GetProcessHeap(), 0, pszFileType);
661 return hr;
663 case ASSOCSTR_SHELLEXTENSION:
665 static const WCHAR shellexW[] = {'S','h','e','l','l','E','x','\\',0};
666 WCHAR keypath[sizeof(shellexW) / sizeof(shellexW[0]) + 39], guid[39];
667 CLSID clsid;
668 HKEY hkey;
669 DWORD size;
670 LONG ret;
672 hr = CLSIDFromString(pszExtra, &clsid);
673 if (FAILED(hr)) return hr;
675 strcpyW(keypath, shellexW);
676 strcatW(keypath, pszExtra);
677 ret = RegOpenKeyExW(This->hkeySource, keypath, 0, KEY_READ, &hkey);
678 if (ret) return HRESULT_FROM_WIN32(ret);
680 size = sizeof(guid);
681 ret = RegGetValueW(hkey, NULL, NULL, RRF_RT_REG_SZ, NULL, guid, &size);
682 RegCloseKey(hkey);
683 if (ret) return HRESULT_FROM_WIN32(ret);
685 return ASSOC_ReturnString(flags, pszOut, pcchOut, guid, size / sizeof(WCHAR));
688 default:
689 FIXME("assocstr %d unimplemented!\n", str);
690 return E_NOTIMPL;
694 /**************************************************************************
695 * IQueryAssociations_GetKey
697 * Get a file association key from the registry.
699 * PARAMS
700 * iface [I] IQueryAssociations interface to query
701 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
702 * assockey [I] Type of key to get (ASSOCKEY enum from "shlwapi.h")
703 * pszExtra [I] Extra information about the key location
704 * phkeyOut [O] Destination for the association key
706 * RETURNS
707 * Success: S_OK. phkeyOut contains a handle to the key.
708 * Failure: An HRESULT error code indicating the error.
710 static HRESULT WINAPI IQueryAssociations_fnGetKey(
711 IQueryAssociations *iface,
712 ASSOCF cfFlags,
713 ASSOCKEY assockey,
714 LPCWSTR pszExtra,
715 HKEY *phkeyOut)
717 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
719 FIXME("(%p,0x%8x,0x%8x,%s,%p)-stub!\n", This, cfFlags, assockey,
720 debugstr_w(pszExtra), phkeyOut);
721 return E_NOTIMPL;
724 /**************************************************************************
725 * IQueryAssociations_GetData
727 * Get the data for a file association key from the registry.
729 * PARAMS
730 * iface [I] IQueryAssociations interface to query
731 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
732 * assocdata [I] Type of data to get (ASSOCDATA enum from "shlwapi.h")
733 * pszExtra [I] Extra information about the data location
734 * pvOut [O] Destination for the association key
735 * pcbOut [I/O] Size of pvOut
737 * RETURNS
738 * Success: S_OK. pszOut contains the data, pcbOut contains its length.
739 * Failure: An HRESULT error code indicating the error.
741 static HRESULT WINAPI IQueryAssociations_fnGetData(IQueryAssociations *iface,
742 ASSOCF cfFlags, ASSOCDATA assocdata, LPCWSTR pszExtra, LPVOID pvOut,
743 DWORD *pcbOut)
745 static const WCHAR edit_flags[] = {'E','d','i','t','F','l','a','g','s',0};
747 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
748 void *data = NULL;
749 DWORD size;
750 HRESULT hres;
752 TRACE("(%p,0x%8x,0x%8x,%s,%p,%p)\n", This, cfFlags, assocdata,
753 debugstr_w(pszExtra), pvOut, pcbOut);
755 if(cfFlags)
756 FIXME("Unsupported flags: %x\n", cfFlags);
758 switch(assocdata) {
759 case ASSOCDATA_EDITFLAGS:
760 if(!This->hkeyProgID)
761 return HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION);
763 hres = ASSOC_GetValue(This->hkeyProgID, edit_flags, &data, &size);
764 if(SUCCEEDED(hres) && pcbOut)
765 hres = ASSOC_ReturnData(pvOut, pcbOut, data, size);
766 HeapFree(GetProcessHeap(), 0, data);
767 return hres;
768 default:
769 FIXME("Unsupported ASSOCDATA value: %d\n", assocdata);
770 return E_NOTIMPL;
774 /**************************************************************************
775 * IQueryAssociations_GetEnum
777 * Not yet implemented in native Win32.
779 * PARAMS
780 * iface [I] IQueryAssociations interface to query
781 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
782 * assocenum [I] Type of enum to get (ASSOCENUM enum from "shlwapi.h")
783 * pszExtra [I] Extra information about the enum location
784 * riid [I] REFIID to look for
785 * ppvOut [O] Destination for the interface.
787 * RETURNS
788 * Success: S_OK.
789 * Failure: An HRESULT error code indicating the error.
791 * NOTES
792 * Presumably this function returns an enumerator object.
794 static HRESULT WINAPI IQueryAssociations_fnGetEnum(
795 IQueryAssociations *iface,
796 ASSOCF cfFlags,
797 ASSOCENUM assocenum,
798 LPCWSTR pszExtra,
799 REFIID riid,
800 LPVOID *ppvOut)
802 IQueryAssociationsImpl *This = impl_from_IQueryAssociations(iface);
804 FIXME("(%p,0x%8x,0x%8x,%s,%s,%p)-stub!\n", This, cfFlags, assocenum,
805 debugstr_w(pszExtra), debugstr_guid(riid), ppvOut);
806 return E_NOTIMPL;
809 static const IQueryAssociationsVtbl IQueryAssociations_vtbl =
811 IQueryAssociations_fnQueryInterface,
812 IQueryAssociations_fnAddRef,
813 IQueryAssociations_fnRelease,
814 IQueryAssociations_fnInit,
815 IQueryAssociations_fnGetString,
816 IQueryAssociations_fnGetKey,
817 IQueryAssociations_fnGetData,
818 IQueryAssociations_fnGetEnum
821 /**************************************************************************
822 * IApplicationAssociationRegistration implementation
824 static inline IApplicationAssociationRegistrationImpl *impl_from_IApplicationAssociationRegistration(IApplicationAssociationRegistration *iface)
826 return CONTAINING_RECORD(iface, IApplicationAssociationRegistrationImpl, IApplicationAssociationRegistration_iface);
829 static HRESULT WINAPI ApplicationAssociationRegistration_QueryInterface(
830 IApplicationAssociationRegistration* iface, REFIID riid, LPVOID *ppv)
832 IApplicationAssociationRegistrationImpl *This = impl_from_IApplicationAssociationRegistration(iface);
834 TRACE("(%p, %s, %p)\n",This, debugstr_guid(riid), ppv);
836 if (ppv == NULL)
837 return E_POINTER;
839 if (IsEqualGUID(&IID_IUnknown, riid) ||
840 IsEqualGUID(&IID_IApplicationAssociationRegistration, riid)) {
841 *ppv = &This->IApplicationAssociationRegistration_iface;
842 IUnknown_AddRef((IUnknown*)*ppv);
843 TRACE("returning IApplicationAssociationRegistration: %p\n", *ppv);
844 return S_OK;
847 *ppv = NULL;
848 FIXME("(%p)->(%s %p) interface not supported\n", This, debugstr_guid(riid), ppv);
849 return E_NOINTERFACE;
852 static ULONG WINAPI ApplicationAssociationRegistration_AddRef(IApplicationAssociationRegistration *iface)
854 IApplicationAssociationRegistrationImpl *This = impl_from_IApplicationAssociationRegistration(iface);
855 ULONG ref = InterlockedIncrement(&This->ref);
857 TRACE("(%p) ref=%d\n", This, ref);
858 return ref;
861 static ULONG WINAPI ApplicationAssociationRegistration_Release(IApplicationAssociationRegistration *iface)
863 IApplicationAssociationRegistrationImpl *This = impl_from_IApplicationAssociationRegistration(iface);
864 ULONG ref = InterlockedDecrement(&This->ref);
866 TRACE("(%p) ref=%d\n", This, ref);
868 if (!ref) {
869 SHFree(This);
871 return ref;
874 static HRESULT WINAPI ApplicationAssociationRegistration_QueryCurrentDefault(IApplicationAssociationRegistration *iface, LPCWSTR query,
875 ASSOCIATIONTYPE type, ASSOCIATIONLEVEL level, LPWSTR *association)
877 IApplicationAssociationRegistrationImpl *This = impl_from_IApplicationAssociationRegistration(iface);
878 static WCHAR urlassoc[] = {'U','r','l','A','s','s','o','c','i','a','t','i','o','n','s',0};
879 static WCHAR mimeassoc[] = {'M','I','M','E','A','s','s','o','c','i','a','t','i','o','n','s',0};
880 static WCHAR assocations[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
881 'W','i','n','d','o','w','s','\\','S','h','e','l','l','\\',
882 'A','s','s','o','c','i','a','t','i','o','n','s',0};
883 static WCHAR slash[] = {'\\',0};
884 static WCHAR choice[] = {'U','s','e','r','C','h','o','i','c','e',0};
885 static WCHAR propid[] = {'P','r','o','g','i','d',0};
886 WCHAR path[MAX_PATH] = {0};
887 DWORD ret, keytype, size;
888 HKEY hkey = NULL;
889 HRESULT hr = HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION);
891 TRACE("(%p)->(%s, %d, %d, %p)\n", This, debugstr_w(query), type, level, association);
893 if(!association)
894 return E_INVALIDARG;
896 *association = NULL;
898 if((type == AT_URLPROTOCOL || type == AT_FILEEXTENSION) && !query[0])
899 return E_INVALIDARG;
900 else if(type == AT_FILEEXTENSION && query[0] != '.')
901 return E_INVALIDARG;
903 if(type == AT_FILEEXTENSION)
905 ret = RegOpenKeyExW(HKEY_CLASSES_ROOT, query, 0, KEY_READ, &hkey);
906 if(ret == ERROR_SUCCESS)
908 ret = RegGetValueW(hkey, NULL, NULL, RRF_RT_REG_SZ, &keytype, NULL, &size);
909 if(ret == ERROR_SUCCESS)
911 *association = CoTaskMemAlloc(size);
912 if(*association)
914 ret = RegGetValueW(hkey, NULL, NULL, RRF_RT_REG_SZ, &keytype, *association, &size);
915 if(ret == ERROR_SUCCESS)
916 hr = S_OK;
917 else
919 CoTaskMemFree(*association);
920 *association = NULL;
923 else
924 hr = E_OUTOFMEMORY;
928 else
930 ret = RegOpenKeyExW(HKEY_CURRENT_USER, assocations, 0, KEY_READ, &hkey);
931 if(ret == ERROR_SUCCESS)
933 if(type == AT_URLPROTOCOL)
934 lstrcpyW(path, urlassoc);
935 else if(type == AT_MIMETYPE)
936 lstrcpyW(path, mimeassoc);
937 else
939 WARN("Unsupported type (%d).\n", type);
940 RegCloseKey(hkey);
941 return hr;
944 lstrcatW(path, slash);
945 lstrcatW(path, query);
946 lstrcatW(path, slash);
947 lstrcatW(path, choice);
949 ret = RegGetValueW(hkey, path, propid, RRF_RT_REG_SZ, &keytype, NULL, &size);
950 if(ret == ERROR_SUCCESS)
952 *association = CoTaskMemAlloc(size);
953 if(*association)
955 ret = RegGetValueW(hkey, path, propid, RRF_RT_REG_SZ, &keytype, *association, &size);
956 if(ret == ERROR_SUCCESS)
957 hr = S_OK;
958 else
960 CoTaskMemFree(*association);
961 *association = NULL;
964 else
965 hr = E_OUTOFMEMORY;
970 RegCloseKey(hkey);
972 return hr;
975 static HRESULT WINAPI ApplicationAssociationRegistration_QueryAppIsDefault(IApplicationAssociationRegistration* This, LPCWSTR query,
976 ASSOCIATIONTYPE type, ASSOCIATIONLEVEL level, LPCWSTR appname, BOOL *is_default)
978 FIXME("(%p)->(%s, %d, %d, %s, %p)\n", This, debugstr_w(query), type, level, debugstr_w(appname), is_default);
979 return E_NOTIMPL;
982 static HRESULT WINAPI ApplicationAssociationRegistration_QueryAppIsDefaultAll(IApplicationAssociationRegistration* This, ASSOCIATIONLEVEL level,
983 LPCWSTR appname, BOOL *is_default)
985 FIXME("(%p)->(%d, %s, %p)\n", This, level, debugstr_w(appname), is_default);
986 return E_NOTIMPL;
989 static HRESULT WINAPI ApplicationAssociationRegistration_SetAppAsDefault(IApplicationAssociationRegistration* This, LPCWSTR appname,
990 LPCWSTR set, ASSOCIATIONTYPE set_type)
992 FIXME("(%p)->(%s, %s, %d)\n", This, debugstr_w(appname), debugstr_w(set), set_type);
993 return E_NOTIMPL;
996 static HRESULT WINAPI ApplicationAssociationRegistration_SetAppAsDefaultAll(IApplicationAssociationRegistration* This, LPCWSTR appname)
998 FIXME("(%p)->(%s)\n", This, debugstr_w(appname));
999 return E_NOTIMPL;
1003 static HRESULT WINAPI ApplicationAssociationRegistration_ClearUserAssociations(IApplicationAssociationRegistration* This)
1005 FIXME("(%p)\n", This);
1006 return E_NOTIMPL;
1010 static const IApplicationAssociationRegistrationVtbl IApplicationAssociationRegistration_vtbl =
1012 ApplicationAssociationRegistration_QueryInterface,
1013 ApplicationAssociationRegistration_AddRef,
1014 ApplicationAssociationRegistration_Release,
1015 ApplicationAssociationRegistration_QueryCurrentDefault,
1016 ApplicationAssociationRegistration_QueryAppIsDefault,
1017 ApplicationAssociationRegistration_QueryAppIsDefaultAll,
1018 ApplicationAssociationRegistration_SetAppAsDefault,
1019 ApplicationAssociationRegistration_SetAppAsDefaultAll,
1020 ApplicationAssociationRegistration_ClearUserAssociations
1023 /**************************************************************************
1024 * IQueryAssociations_Constructor [internal]
1026 * Construct a new IQueryAssociations object.
1028 HRESULT WINAPI QueryAssociations_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppOutput)
1030 IQueryAssociationsImpl* this;
1031 HRESULT ret;
1033 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1035 if (!(this = SHAlloc(sizeof(*this)))) return E_OUTOFMEMORY;
1036 this->IQueryAssociations_iface.lpVtbl = &IQueryAssociations_vtbl;
1037 this->ref = 0;
1038 this->hkeySource = 0;
1039 this->hkeyProgID = 0;
1040 if (FAILED(ret = IQueryAssociations_QueryInterface(&this->IQueryAssociations_iface, riid, ppOutput))) SHFree( this );
1041 TRACE("returning %p\n", *ppOutput);
1042 return ret;
1045 /**************************************************************************
1046 * ApplicationAssociationRegistration_Constructor [internal]
1048 * Construct a IApplicationAssociationRegistration object.
1050 HRESULT WINAPI ApplicationAssociationRegistration_Constructor(IUnknown *outer, REFIID riid, LPVOID *ppv)
1052 IApplicationAssociationRegistrationImpl *This;
1053 HRESULT hr;
1055 if (outer)
1056 return CLASS_E_NOAGGREGATION;
1058 if (!(This = SHAlloc(sizeof(*This))))
1059 return E_OUTOFMEMORY;
1061 This->IApplicationAssociationRegistration_iface.lpVtbl = &IApplicationAssociationRegistration_vtbl;
1062 This->ref = 0;
1064 hr = IApplicationAssociationRegistration_QueryInterface(&This->IApplicationAssociationRegistration_iface, riid, ppv);
1065 if (FAILED(hr))
1066 SHFree(This);
1068 TRACE("returning 0x%x with %p\n", hr, *ppv);
1069 return hr;
1072 static HRESULT WINAPI enumassochandlers_QueryInterface(IEnumAssocHandlers *iface, REFIID riid, void **obj)
1074 struct enumassochandlers *This = impl_from_IEnumAssocHandlers(iface);
1076 TRACE("(%p %s %p)\n", This, debugstr_guid(riid), obj);
1078 if (IsEqualIID(riid, &IID_IEnumAssocHandlers) ||
1079 IsEqualIID(riid, &IID_IUnknown))
1081 *obj = iface;
1082 IEnumAssocHandlers_AddRef(iface);
1083 return S_OK;
1086 *obj = NULL;
1087 return E_NOINTERFACE;
1090 static ULONG WINAPI enumassochandlers_AddRef(IEnumAssocHandlers *iface)
1092 struct enumassochandlers *This = impl_from_IEnumAssocHandlers(iface);
1093 ULONG ref = InterlockedIncrement(&This->ref);
1095 TRACE("(%p)->(%u)\n", This, ref);
1096 return ref;
1099 static ULONG WINAPI enumassochandlers_Release(IEnumAssocHandlers *iface)
1101 struct enumassochandlers *This = impl_from_IEnumAssocHandlers(iface);
1102 ULONG ref = InterlockedDecrement(&This->ref);
1104 TRACE("(%p)->(%u)\n", This, ref);
1106 if (!ref)
1107 SHFree(This);
1109 return ref;
1112 static HRESULT WINAPI enumassochandlers_Next(IEnumAssocHandlers *iface, ULONG count, IAssocHandler **handlers,
1113 ULONG *fetched)
1115 struct enumassochandlers *This = impl_from_IEnumAssocHandlers(iface);
1117 FIXME("(%p)->(%u %p %p): stub\n", This, count, handlers, fetched);
1119 return E_NOTIMPL;
1122 static const IEnumAssocHandlersVtbl enumassochandlersvtbl = {
1123 enumassochandlers_QueryInterface,
1124 enumassochandlers_AddRef,
1125 enumassochandlers_Release,
1126 enumassochandlers_Next
1129 /**************************************************************************
1130 * SHAssocEnumHandlers [SHELL32.@]
1132 HRESULT WINAPI SHAssocEnumHandlers(const WCHAR *extra, ASSOC_FILTER filter, IEnumAssocHandlers **enumhandlers)
1134 struct enumassochandlers *enumassoc;
1136 FIXME("(%s %d %p): stub\n", debugstr_w(extra), filter, enumhandlers);
1138 *enumhandlers = NULL;
1140 enumassoc = SHAlloc(sizeof(*enumassoc));
1141 if (!enumassoc)
1142 return E_OUTOFMEMORY;
1144 enumassoc->IEnumAssocHandlers_iface.lpVtbl = &enumassochandlersvtbl;
1145 enumassoc->ref = 1;
1147 *enumhandlers = &enumassoc->IEnumAssocHandlers_iface;
1148 return S_OK;