shlwapi: Support the content type query.
[wine/multimedia.git] / dlls / shlwapi / assoc.c
blob070ca1ab77a20ab481f33f8390f8ab8184791db9
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>
21 #include <assert.h>
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 "ver.h"
31 #include "wine/unicode.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(shell);
36 /**************************************************************************
37 * IQueryAssociations {SHLWAPI}
39 * DESCRIPTION
40 * This object provides a layer of abstraction over the system registry in
41 * order to simplify the process of parsing associations between files.
42 * Associations in this context means the registry entries that link (for
43 * example) the extension of a file with its description, list of
44 * applications to open the file with, and actions that can be performed on it
45 * (the shell displays such information in the context menu of explorer
46 * when you right-click on a file).
48 * HELPERS
49 * You can use this object transparently by calling the helper functions
50 * AssocQueryKeyA(), AssocQueryStringA() and AssocQueryStringByKeyA(). These
51 * create an IQueryAssociations object, perform the requested actions
52 * and then dispose of the object. Alternatively, you can create an instance
53 * of the object using AssocCreate() and call the following methods on it:
55 * METHODS
58 /* Default IQueryAssociations::Init() flags */
59 #define SHLWAPI_DEF_ASSOCF (ASSOCF_INIT_BYEXENAME|ASSOCF_INIT_DEFAULTTOSTAR| \
60 ASSOCF_INIT_DEFAULTTOFOLDER)
62 typedef struct
64 const IQueryAssociationsVtbl *lpVtbl;
65 LONG ref;
66 HKEY hkeySource;
67 HKEY hkeyProgID;
68 } IQueryAssociationsImpl;
70 static const IQueryAssociationsVtbl IQueryAssociations_vtbl;
72 /**************************************************************************
73 * IQueryAssociations_Constructor [internal]
75 * Construct a new IQueryAssociations object.
77 static IQueryAssociations* IQueryAssociations_Constructor(void)
79 IQueryAssociationsImpl* iface;
81 iface = HeapAlloc(GetProcessHeap(),0,sizeof(IQueryAssociationsImpl));
82 iface->lpVtbl = &IQueryAssociations_vtbl;
83 iface->ref = 1;
84 iface->hkeySource = NULL;
85 iface->hkeyProgID = NULL;
87 TRACE("Returning IQueryAssociations* %p\n", iface);
88 return (IQueryAssociations*)iface;
91 /*************************************************************************
92 * SHLWAPI_ParamAToW
94 * Internal helper function: Convert ASCII parameter to Unicode.
96 static BOOL SHLWAPI_ParamAToW(LPCSTR lpszParam, LPWSTR lpszBuff, DWORD dwLen,
97 LPWSTR* lpszOut)
99 if (lpszParam)
101 DWORD dwStrLen = MultiByteToWideChar(CP_ACP, 0, lpszParam, -1, NULL, 0);
103 if (dwStrLen < dwLen)
105 *lpszOut = lpszBuff; /* Use Buffer, it is big enough */
107 else
109 /* Create a new buffer big enough for the string */
110 *lpszOut = HeapAlloc(GetProcessHeap(), 0,
111 dwStrLen * sizeof(WCHAR));
112 if (!*lpszOut)
113 return FALSE;
115 MultiByteToWideChar(CP_ACP, 0, lpszParam, -1, *lpszOut, dwStrLen);
117 else
118 *lpszOut = NULL;
119 return TRUE;
122 /*************************************************************************
123 * AssocCreate [SHLWAPI.@]
125 * Create a new IQueryAssociations object.
127 * PARAMS
128 * clsid [I] CLSID of object
129 * refiid [I] REFIID of interface
130 * lpInterface [O] Destination for the created IQueryAssociations object
132 * RETURNS
133 * Success: S_OK. lpInterface contains the new object.
134 * Failure: An HRESULT error code indicating the error.
136 * NOTES
137 * refiid must be equal to IID_IQueryAssociations, or this function will fail.
139 HRESULT WINAPI AssocCreate(CLSID clsid, REFIID refiid, void **lpInterface)
141 HRESULT hRet;
142 IQueryAssociations* lpAssoc;
144 TRACE("(%s,%s,%p)\n", debugstr_guid(&clsid), debugstr_guid(refiid),
145 lpInterface);
147 if (!lpInterface)
148 return E_INVALIDARG;
150 *(DWORD*)lpInterface = 0;
152 if (!IsEqualGUID(&clsid, &IID_IQueryAssociations))
153 return E_NOTIMPL;
155 lpAssoc = IQueryAssociations_Constructor();
157 if (!lpAssoc)
158 return E_OUTOFMEMORY;
160 hRet = IQueryAssociations_QueryInterface(lpAssoc, refiid, lpInterface);
161 IQueryAssociations_Release(lpAssoc);
162 return hRet;
165 /*************************************************************************
166 * AssocQueryKeyW [SHLWAPI.@]
168 * See AssocQueryKeyA.
170 HRESULT WINAPI AssocQueryKeyW(ASSOCF cfFlags, ASSOCKEY assockey, LPCWSTR pszAssoc,
171 LPCWSTR pszExtra, HKEY *phkeyOut)
173 HRESULT hRet;
174 IQueryAssociations* lpAssoc;
176 TRACE("(0x%8x,0x%8x,%s,%s,%p)\n", cfFlags, assockey, debugstr_w(pszAssoc),
177 debugstr_w(pszExtra), phkeyOut);
179 lpAssoc = IQueryAssociations_Constructor();
181 if (!lpAssoc)
182 return E_OUTOFMEMORY;
184 cfFlags &= SHLWAPI_DEF_ASSOCF;
185 hRet = IQueryAssociations_Init(lpAssoc, cfFlags, pszAssoc, NULL, NULL);
187 if (SUCCEEDED(hRet))
188 hRet = IQueryAssociations_GetKey(lpAssoc, cfFlags, assockey, pszExtra, phkeyOut);
190 IQueryAssociations_Release(lpAssoc);
191 return hRet;
194 /*************************************************************************
195 * AssocQueryKeyA [SHLWAPI.@]
197 * Get a file association key from the registry.
199 * PARAMS
200 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
201 * assockey [I] Type of key to get
202 * pszAssoc [I] Key name to search below
203 * pszExtra [I] Extra information about the key location
204 * phkeyOut [O] Destination for the association key
206 * RETURNS
207 * Success: S_OK. phkeyOut contains the key.
208 * Failure: An HRESULT error code indicating the error.
210 HRESULT WINAPI AssocQueryKeyA(ASSOCF cfFlags, ASSOCKEY assockey, LPCSTR pszAssoc,
211 LPCSTR pszExtra, HKEY *phkeyOut)
213 WCHAR szAssocW[MAX_PATH], *lpszAssocW = NULL;
214 WCHAR szExtraW[MAX_PATH], *lpszExtraW = NULL;
215 HRESULT hRet = E_OUTOFMEMORY;
217 TRACE("(0x%8x,0x%8x,%s,%s,%p)\n", cfFlags, assockey, debugstr_a(pszAssoc),
218 debugstr_a(pszExtra), phkeyOut);
220 if (SHLWAPI_ParamAToW(pszAssoc, szAssocW, MAX_PATH, &lpszAssocW) &&
221 SHLWAPI_ParamAToW(pszExtra, szExtraW, MAX_PATH, &lpszExtraW))
223 hRet = AssocQueryKeyW(cfFlags, assockey, lpszAssocW, lpszExtraW, phkeyOut);
226 if (lpszAssocW != szAssocW)
227 HeapFree(GetProcessHeap(), 0, lpszAssocW);
229 if (lpszExtraW != szExtraW)
230 HeapFree(GetProcessHeap(), 0, lpszExtraW);
232 return hRet;
235 /*************************************************************************
236 * AssocQueryStringW [SHLWAPI.@]
238 * See AssocQueryStringA.
240 HRESULT WINAPI AssocQueryStringW(ASSOCF cfFlags, ASSOCSTR str, LPCWSTR pszAssoc,
241 LPCWSTR pszExtra, LPWSTR pszOut, DWORD *pcchOut)
243 HRESULT hRet;
244 IQueryAssociations* lpAssoc;
246 TRACE("(0x%8x,0x%8x,%s,%s,%p,%p)\n", cfFlags, str, debugstr_w(pszAssoc),
247 debugstr_w(pszExtra), pszOut, pcchOut);
249 if (!pcchOut)
250 return E_UNEXPECTED;
252 lpAssoc = IQueryAssociations_Constructor();
254 if (!lpAssoc)
255 return E_OUTOFMEMORY;
257 hRet = IQueryAssociations_Init(lpAssoc, cfFlags & SHLWAPI_DEF_ASSOCF,
258 pszAssoc, NULL, NULL);
260 if (SUCCEEDED(hRet))
261 hRet = IQueryAssociations_GetString(lpAssoc, cfFlags, str, pszExtra,
262 pszOut, pcchOut);
264 IQueryAssociations_Release(lpAssoc);
265 return hRet;
268 /*************************************************************************
269 * AssocQueryStringA [SHLWAPI.@]
271 * Get a file association string from the registry.
273 * PARAMS
274 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
275 * str [I] Type of string to get (ASSOCSTR enum from "shlwapi.h")
276 * pszAssoc [I] Key name to search below
277 * pszExtra [I] Extra information about the string location
278 * pszOut [O] Destination for the association string
279 * pcchOut [O] Length of pszOut
281 * RETURNS
282 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
283 * Failure: An HRESULT error code indicating the error.
285 HRESULT WINAPI AssocQueryStringA(ASSOCF cfFlags, ASSOCSTR str, LPCSTR pszAssoc,
286 LPCSTR pszExtra, LPSTR pszOut, DWORD *pcchOut)
288 WCHAR szAssocW[MAX_PATH], *lpszAssocW = NULL;
289 WCHAR szExtraW[MAX_PATH], *lpszExtraW = NULL;
290 HRESULT hRet = E_OUTOFMEMORY;
292 TRACE("(0x%8x,0x%8x,%s,%s,%p,%p)\n", cfFlags, str, debugstr_a(pszAssoc),
293 debugstr_a(pszExtra), pszOut, pcchOut);
295 if (!pcchOut)
296 hRet = E_UNEXPECTED;
297 else if (SHLWAPI_ParamAToW(pszAssoc, szAssocW, MAX_PATH, &lpszAssocW) &&
298 SHLWAPI_ParamAToW(pszExtra, szExtraW, MAX_PATH, &lpszExtraW))
300 WCHAR szReturnW[MAX_PATH], *lpszReturnW = szReturnW;
301 DWORD dwLenOut = *pcchOut;
303 if (dwLenOut >= MAX_PATH)
304 lpszReturnW = HeapAlloc(GetProcessHeap(), 0,
305 (dwLenOut + 1) * sizeof(WCHAR));
306 else
307 dwLenOut = sizeof(szReturnW) / sizeof(szReturnW[0]);
309 if (!lpszReturnW)
310 hRet = E_OUTOFMEMORY;
311 else
313 hRet = AssocQueryStringW(cfFlags, str, lpszAssocW, lpszExtraW,
314 lpszReturnW, &dwLenOut);
316 if (SUCCEEDED(hRet))
317 dwLenOut = WideCharToMultiByte(CP_ACP, 0, lpszReturnW, -1,
318 pszOut, *pcchOut, NULL, NULL);
320 *pcchOut = dwLenOut;
321 if (lpszReturnW != szReturnW)
322 HeapFree(GetProcessHeap(), 0, lpszReturnW);
326 if (lpszAssocW != szAssocW)
327 HeapFree(GetProcessHeap(), 0, lpszAssocW);
328 if (lpszExtraW != szExtraW)
329 HeapFree(GetProcessHeap(), 0, lpszExtraW);
330 return hRet;
333 /*************************************************************************
334 * AssocQueryStringByKeyW [SHLWAPI.@]
336 * See AssocQueryStringByKeyA.
338 HRESULT WINAPI AssocQueryStringByKeyW(ASSOCF cfFlags, ASSOCSTR str, HKEY hkAssoc,
339 LPCWSTR pszExtra, LPWSTR pszOut,
340 DWORD *pcchOut)
342 HRESULT hRet;
343 IQueryAssociations* lpAssoc;
345 TRACE("(0x%8x,0x%8x,%p,%s,%p,%p)\n", cfFlags, str, hkAssoc,
346 debugstr_w(pszExtra), pszOut, pcchOut);
348 lpAssoc = IQueryAssociations_Constructor();
350 if (!lpAssoc)
351 return E_OUTOFMEMORY;
353 cfFlags &= SHLWAPI_DEF_ASSOCF;
354 hRet = IQueryAssociations_Init(lpAssoc, cfFlags, 0, hkAssoc, NULL);
356 if (SUCCEEDED(hRet))
357 hRet = IQueryAssociations_GetString(lpAssoc, cfFlags, str, pszExtra,
358 pszOut, pcchOut);
360 IQueryAssociations_Release(lpAssoc);
361 return hRet;
364 /*************************************************************************
365 * AssocQueryStringByKeyA [SHLWAPI.@]
367 * Get a file association string from the registry, given a starting key.
369 * PARAMS
370 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
371 * str [I] Type of string to get
372 * hkAssoc [I] Key to search below
373 * pszExtra [I] Extra information about the string location
374 * pszOut [O] Destination for the association string
375 * pcchOut [O] Length of pszOut
377 * RETURNS
378 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
379 * Failure: An HRESULT error code indicating the error.
381 HRESULT WINAPI AssocQueryStringByKeyA(ASSOCF cfFlags, ASSOCSTR str, HKEY hkAssoc,
382 LPCSTR pszExtra, LPSTR pszOut,
383 DWORD *pcchOut)
385 WCHAR szExtraW[MAX_PATH], *lpszExtraW = szExtraW;
386 WCHAR szReturnW[MAX_PATH], *lpszReturnW = szReturnW;
387 HRESULT hRet = E_OUTOFMEMORY;
389 TRACE("(0x%8x,0x%8x,%p,%s,%p,%p)\n", cfFlags, str, hkAssoc,
390 debugstr_a(pszExtra), pszOut, pcchOut);
392 if (!pcchOut)
393 hRet = E_INVALIDARG;
394 else if (SHLWAPI_ParamAToW(pszExtra, szExtraW, MAX_PATH, &lpszExtraW))
396 DWORD dwLenOut = *pcchOut;
397 if (dwLenOut >= MAX_PATH)
398 lpszReturnW = HeapAlloc(GetProcessHeap(), 0,
399 (dwLenOut + 1) * sizeof(WCHAR));
401 if (lpszReturnW)
403 hRet = AssocQueryStringByKeyW(cfFlags, str, hkAssoc, lpszExtraW,
404 lpszReturnW, &dwLenOut);
406 if (SUCCEEDED(hRet))
407 WideCharToMultiByte(CP_ACP,0,szReturnW,-1,pszOut,dwLenOut,0,0);
408 *pcchOut = dwLenOut;
410 if (lpszReturnW != szReturnW)
411 HeapFree(GetProcessHeap(), 0, lpszReturnW);
415 if (lpszExtraW != szExtraW)
416 HeapFree(GetProcessHeap(), 0, lpszExtraW);
417 return hRet;
421 /**************************************************************************
422 * AssocIsDangerous (SHLWAPI.@)
424 * Determine if a file association is dangerous (potentially malware).
426 * PARAMS
427 * lpszAssoc [I] Name of file or file extension to check.
429 * RETURNS
430 * TRUE, if lpszAssoc may potentially be malware (executable),
431 * FALSE, Otherwise.
433 BOOL WINAPI AssocIsDangerous(LPCWSTR lpszAssoc)
435 FIXME("%s\n", debugstr_w(lpszAssoc));
436 return FALSE;
439 /**************************************************************************
440 * IQueryAssociations_QueryInterface {SHLWAPI}
442 * See IUnknown_QueryInterface.
444 static HRESULT WINAPI IQueryAssociations_fnQueryInterface(
445 IQueryAssociations* iface,
446 REFIID riid,
447 LPVOID *ppvObj)
449 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
451 TRACE("(%p,%s,%p)\n",This, debugstr_guid(riid), ppvObj);
453 *ppvObj = NULL;
455 if (IsEqualIID(riid, &IID_IUnknown) ||
456 IsEqualIID(riid, &IID_IQueryAssociations))
458 *ppvObj = (IQueryAssociations*)This;
460 IQueryAssociations_AddRef((IQueryAssociations*)*ppvObj);
461 TRACE("Returning IQueryAssociations (%p)\n", *ppvObj);
462 return S_OK;
464 TRACE("Returning E_NOINTERFACE\n");
465 return E_NOINTERFACE;
468 /**************************************************************************
469 * IQueryAssociations_AddRef {SHLWAPI}
471 * See IUnknown_AddRef.
473 static ULONG WINAPI IQueryAssociations_fnAddRef(IQueryAssociations *iface)
475 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
476 ULONG refCount = InterlockedIncrement(&This->ref);
478 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
480 return refCount;
483 /**************************************************************************
484 * IQueryAssociations_Release {SHLWAPI}
486 * See IUnknown_Release.
488 static ULONG WINAPI IQueryAssociations_fnRelease(IQueryAssociations *iface)
490 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
491 ULONG refCount = InterlockedDecrement(&This->ref);
493 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
495 if (!refCount)
497 TRACE("Destroying IQueryAssociations (%p)\n", This);
498 RegCloseKey(This->hkeySource);
499 RegCloseKey(This->hkeyProgID);
500 HeapFree(GetProcessHeap(), 0, This);
503 return refCount;
506 /**************************************************************************
507 * IQueryAssociations_Init {SHLWAPI}
509 * Initialise an IQueryAssociations object.
511 * PARAMS
512 * iface [I] IQueryAssociations interface to initialise
513 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
514 * pszAssoc [I] String for the root key name, or NULL if hkeyProgid is given
515 * hkeyProgid [I] Handle for the root key, or NULL if pszAssoc is given
516 * hWnd [I] Reserved, must be NULL.
518 * RETURNS
519 * Success: S_OK. iface is initialised with the parameters given.
520 * Failure: An HRESULT error code indicating the error.
522 static HRESULT WINAPI IQueryAssociations_fnInit(
523 IQueryAssociations *iface,
524 ASSOCF cfFlags,
525 LPCWSTR pszAssoc,
526 HKEY hkeyProgid,
527 HWND hWnd)
529 static const WCHAR szProgID[] = {'P','r','o','g','I','D',0};
530 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
531 LONG ret;
533 TRACE("(%p)->(%d,%s,%p,%p)\n", iface,
534 cfFlags,
535 debugstr_w(pszAssoc),
536 hkeyProgid,
537 hWnd);
538 if (hWnd != NULL)
539 FIXME("hwnd != NULL not supported\n");
540 if (cfFlags != 0)
541 FIXME("unsupported flags: %x\n", cfFlags);
542 if (pszAssoc != NULL)
544 ret = RegOpenKeyExW(HKEY_CLASSES_ROOT,
545 pszAssoc,
547 KEY_READ,
548 &This->hkeySource);
549 if (ret != ERROR_SUCCESS)
550 return E_FAIL;
551 /* if this is not a prog id */
552 if ((*pszAssoc == '.') || (*pszAssoc == '{'))
554 RegOpenKeyExW(This->hkeySource,
555 szProgID,
557 KEY_READ,
558 &This->hkeyProgID);
560 else
561 This->hkeyProgID = This->hkeySource;
562 return S_OK;
564 else if (hkeyProgid != NULL)
566 This->hkeyProgID = hkeyProgid;
567 return S_OK;
569 else
570 return E_INVALIDARG;
573 static HRESULT ASSOC_GetValue(HKEY hkey, WCHAR ** pszText)
575 DWORD len;
576 LONG ret;
578 assert(pszText);
579 ret = RegQueryValueExW(hkey, NULL, 0, NULL, NULL, &len);
580 if (ret != ERROR_SUCCESS)
581 return HRESULT_FROM_WIN32(ret);
582 if (!len)
583 return E_FAIL;
584 *pszText = HeapAlloc(GetProcessHeap(), 0, len);
585 if (!*pszText)
586 return E_OUTOFMEMORY;
587 ret = RegQueryValueExW(hkey, NULL, 0, NULL, (LPBYTE)*pszText,
588 &len);
589 if (ret != ERROR_SUCCESS)
591 HeapFree(GetProcessHeap(), 0, *pszText);
592 return HRESULT_FROM_WIN32(ret);
594 return S_OK;
597 static HRESULT ASSOC_GetExecutable(IQueryAssociationsImpl *This,
598 LPCWSTR pszExtra, LPWSTR path,
599 DWORD pathlen, DWORD *len)
601 HKEY hkeyCommand;
602 HKEY hkeyFile;
603 HKEY hkeyShell;
604 HKEY hkeyVerb;
605 HRESULT hr;
606 LONG ret;
607 WCHAR * pszCommand;
608 WCHAR * pszEnd;
609 WCHAR * pszExtraFromReg = NULL;
610 WCHAR * pszFileType;
611 WCHAR * pszStart;
612 static const WCHAR commandW[] = { 'c','o','m','m','a','n','d',0 };
613 static const WCHAR shellW[] = { 's','h','e','l','l',0 };
615 assert(len);
617 hr = ASSOC_GetValue(This->hkeySource, &pszFileType);
618 if (FAILED(hr))
619 return hr;
620 ret = RegOpenKeyExW(HKEY_CLASSES_ROOT, pszFileType, 0, KEY_READ, &hkeyFile);
621 HeapFree(GetProcessHeap(), 0, pszFileType);
622 if (ret != ERROR_SUCCESS)
623 return HRESULT_FROM_WIN32(ret);
625 ret = RegOpenKeyExW(hkeyFile, shellW, 0, KEY_READ, &hkeyShell);
626 RegCloseKey(hkeyFile);
627 if (ret != ERROR_SUCCESS)
628 return HRESULT_FROM_WIN32(ret);
630 if (!pszExtra)
632 hr = ASSOC_GetValue(hkeyShell, &pszExtraFromReg);
633 /* if no default action */
634 if (hr == E_FAIL || hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
636 DWORD rlen;
637 ret = RegQueryInfoKeyW(hkeyShell, 0, 0, 0, 0, &rlen, 0, 0, 0, 0, 0, 0);
638 if (ret != ERROR_SUCCESS)
640 RegCloseKey(hkeyShell);
641 return HRESULT_FROM_WIN32(ret);
643 rlen++;
644 pszExtraFromReg = HeapAlloc(GetProcessHeap(), 0, rlen * sizeof(WCHAR));
645 if (!pszExtraFromReg)
647 RegCloseKey(hkeyShell);
648 return E_OUTOFMEMORY;
650 ret = RegEnumKeyExW(hkeyShell, 0, pszExtraFromReg, &rlen, 0, NULL, NULL, NULL);
651 if (ret != ERROR_SUCCESS)
653 RegCloseKey(hkeyShell);
654 return HRESULT_FROM_WIN32(ret);
657 else if (FAILED(hr))
659 RegCloseKey(hkeyShell);
660 return hr;
664 ret = RegOpenKeyExW(hkeyShell, pszExtra ? pszExtra : pszExtraFromReg, 0,
665 KEY_READ, &hkeyVerb);
666 HeapFree(GetProcessHeap(), 0, pszExtraFromReg);
667 RegCloseKey(hkeyShell);
668 if (ret != ERROR_SUCCESS)
669 return HRESULT_FROM_WIN32(ret);
671 ret = RegOpenKeyExW(hkeyVerb, commandW, 0, KEY_READ, &hkeyCommand);
672 RegCloseKey(hkeyVerb);
673 if (ret != ERROR_SUCCESS)
674 return HRESULT_FROM_WIN32(ret);
675 hr = ASSOC_GetValue(hkeyCommand, &pszCommand);
676 RegCloseKey(hkeyCommand);
677 if (FAILED(hr))
678 return hr;
680 /* cleanup pszCommand */
681 if (pszCommand[0] == '"')
683 pszStart = pszCommand + 1;
684 pszEnd = strchrW(pszStart, '"');
686 else
688 pszStart = pszCommand;
689 pszEnd = strchrW(pszStart, ' ');
691 if (pszEnd)
692 *pszEnd = 0;
694 *len = SearchPathW(NULL, pszStart, NULL, pathlen, path, NULL);
695 HeapFree(GetProcessHeap(), 0, pszCommand);
696 if (!*len)
697 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
698 return S_OK;
701 static HRESULT ASSOC_ReturnData(LPWSTR out, DWORD *outlen, LPCWSTR data,
702 DWORD datalen)
704 assert(outlen);
706 if (out)
708 if (*outlen < datalen)
710 *outlen = datalen;
711 return E_POINTER;
713 *outlen = datalen;
714 lstrcpynW(out, data, datalen);
715 return S_OK;
717 else
719 *outlen = datalen;
720 return S_FALSE;
724 /**************************************************************************
725 * IQueryAssociations_GetString {SHLWAPI}
727 * Get a file association string from the registry.
729 * PARAMS
730 * iface [I] IQueryAssociations interface to query
731 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
732 * str [I] Type of string to get (ASSOCSTR enum from "shlwapi.h")
733 * pszExtra [I] Extra information about the string location
734 * pszOut [O] Destination for the association string
735 * pcchOut [I/O] Length of pszOut
737 * RETURNS
738 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
739 * Failure: An HRESULT error code indicating the error.
741 static HRESULT WINAPI IQueryAssociations_fnGetString(
742 IQueryAssociations *iface,
743 ASSOCF cfFlags,
744 ASSOCSTR str,
745 LPCWSTR pszExtra,
746 LPWSTR pszOut,
747 DWORD *pcchOut)
749 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
750 const ASSOCF cfUnimplemented = ~(0);
751 DWORD len = 0;
752 HRESULT hr;
753 WCHAR path[MAX_PATH];
755 TRACE("(%p,0x%8x,0x%8x,%s,%p,%p)\n", This, cfFlags, str,
756 debugstr_w(pszExtra), pszOut, pcchOut);
758 if (cfFlags & cfUnimplemented)
759 FIXME("%08x: unimplemented flags!\n", cfFlags & cfUnimplemented);
761 if (!pcchOut)
762 return E_UNEXPECTED;
764 switch (str)
766 case ASSOCSTR_EXECUTABLE:
768 hr = ASSOC_GetExecutable(This, pszExtra, path, MAX_PATH, &len);
769 if (FAILED(hr))
770 return hr;
771 len++;
772 return ASSOC_ReturnData(pszOut, pcchOut, path, len);
775 case ASSOCSTR_FRIENDLYAPPNAME:
777 PVOID verinfoW = NULL;
778 DWORD size, retval = 0;
779 UINT flen;
780 WCHAR *bufW;
781 static const WCHAR translationW[] = {
782 '\\','V','a','r','F','i','l','e','I','n','f','o',
783 '\\','T','r','a','n','s','l','a','t','i','o','n',0
785 static const WCHAR fileDescFmtW[] = {
786 '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
787 '\\','%','0','4','x','%','0','4','x',
788 '\\','F','i','l','e','D','e','s','c','r','i','p','t','i','o','n',0
790 WCHAR fileDescW[41];
792 hr = ASSOC_GetExecutable(This, pszExtra, path, MAX_PATH, &len);
793 if (FAILED(hr))
794 return hr;
796 retval = GetFileVersionInfoSizeW(path, &size);
797 if (!retval)
798 goto get_friendly_name_fail;
799 verinfoW = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, retval);
800 if (!verinfoW)
801 return E_OUTOFMEMORY;
802 if (!GetFileVersionInfoW(path, 0, retval, verinfoW))
803 goto get_friendly_name_fail;
804 if (VerQueryValueW(verinfoW, translationW, (LPVOID *)&bufW, &flen))
806 UINT i;
807 DWORD *langCodeDesc = (DWORD *)bufW;
808 for (i = 0; i < flen / sizeof(DWORD); i++)
810 sprintfW(fileDescW, fileDescFmtW, LOWORD(langCodeDesc[i]),
811 HIWORD(langCodeDesc[i]));
812 if (VerQueryValueW(verinfoW, fileDescW, (LPVOID *)&bufW, &flen))
814 /* Does strlenW(bufW) == 0 mean we use the filename? */
815 len = strlenW(bufW) + 1;
816 TRACE("found FileDescription: %s\n", debugstr_w(bufW));
817 return ASSOC_ReturnData(pszOut, pcchOut, bufW, len);
821 get_friendly_name_fail:
822 PathRemoveExtensionW(path);
823 PathStripPathW(path);
824 TRACE("using filename: %s\n", debugstr_w(path));
825 return ASSOC_ReturnData(pszOut, pcchOut, path, strlenW(path) + 1);
828 case ASSOCSTR_CONTENTTYPE:
830 static const WCHAR Content_TypeW[] = {'C','o','n','t','e','n','t',' ','T','y','p','e',0};
831 WCHAR *contentType;
832 DWORD ret;
833 DWORD size;
835 size = 0;
836 ret = RegGetValueW(This->hkeySource, NULL, Content_TypeW, RRF_RT_REG_SZ, NULL, NULL, &size);
837 if (ret != ERROR_SUCCESS)
838 return HRESULT_FROM_WIN32(ret);
839 contentType = HeapAlloc(GetProcessHeap(), 0, size);
840 if (contentType != NULL)
842 ret = RegGetValueW(This->hkeySource, NULL, Content_TypeW, RRF_RT_REG_SZ, NULL, contentType, &size);
843 if (ret == ERROR_SUCCESS)
844 hr = ASSOC_ReturnData(pszOut, pcchOut, contentType, strlenW(contentType) + 1);
845 else
846 hr = HRESULT_FROM_WIN32(ret);
847 HeapFree(GetProcessHeap(), 0, contentType);
849 else
850 hr = E_OUTOFMEMORY;
851 return hr;
854 default:
855 FIXME("assocstr %d unimplemented!\n", str);
856 return E_NOTIMPL;
860 /**************************************************************************
861 * IQueryAssociations_GetKey {SHLWAPI}
863 * Get a file association key from the registry.
865 * PARAMS
866 * iface [I] IQueryAssociations interface to query
867 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
868 * assockey [I] Type of key to get (ASSOCKEY enum from "shlwapi.h")
869 * pszExtra [I] Extra information about the key location
870 * phkeyOut [O] Destination for the association key
872 * RETURNS
873 * Success: S_OK. phkeyOut contains a handle to the key.
874 * Failure: An HRESULT error code indicating the error.
876 static HRESULT WINAPI IQueryAssociations_fnGetKey(
877 IQueryAssociations *iface,
878 ASSOCF cfFlags,
879 ASSOCKEY assockey,
880 LPCWSTR pszExtra,
881 HKEY *phkeyOut)
883 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
885 FIXME("(%p,0x%8x,0x%8x,%s,%p)-stub!\n", This, cfFlags, assockey,
886 debugstr_w(pszExtra), phkeyOut);
887 return E_NOTIMPL;
890 /**************************************************************************
891 * IQueryAssociations_GetData {SHLWAPI}
893 * Get the data for a file association key from the registry.
895 * PARAMS
896 * iface [I] IQueryAssociations interface to query
897 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
898 * assocdata [I] Type of data to get (ASSOCDATA enum from "shlwapi.h")
899 * pszExtra [I] Extra information about the data location
900 * pvOut [O] Destination for the association key
901 * pcbOut [I/O] Size of pvOut
903 * RETURNS
904 * Success: S_OK. pszOut contains the data, pcbOut contains its length.
905 * Failure: An HRESULT error code indicating the error.
907 static HRESULT WINAPI IQueryAssociations_fnGetData(
908 IQueryAssociations *iface,
909 ASSOCF cfFlags,
910 ASSOCDATA assocdata,
911 LPCWSTR pszExtra,
912 LPVOID pvOut,
913 DWORD *pcbOut)
915 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
917 FIXME("(%p,0x%8x,0x%8x,%s,%p,%p)-stub!\n", This, cfFlags, assocdata,
918 debugstr_w(pszExtra), pvOut, pcbOut);
919 return E_NOTIMPL;
922 /**************************************************************************
923 * IQueryAssociations_GetEnum {SHLWAPI}
925 * Not yet implemented in native Win32.
927 * PARAMS
928 * iface [I] IQueryAssociations interface to query
929 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
930 * assocenum [I] Type of enum to get (ASSOCENUM enum from "shlwapi.h")
931 * pszExtra [I] Extra information about the enum location
932 * riid [I] REFIID to look for
933 * ppvOut [O] Destination for the interface.
935 * RETURNS
936 * Success: S_OK.
937 * Failure: An HRESULT error code indicating the error.
939 * NOTES
940 * Presumably this function returns an enumerator object.
942 static HRESULT WINAPI IQueryAssociations_fnGetEnum(
943 IQueryAssociations *iface,
944 ASSOCF cfFlags,
945 ASSOCENUM assocenum,
946 LPCWSTR pszExtra,
947 REFIID riid,
948 LPVOID *ppvOut)
950 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
952 FIXME("(%p,0x%8x,0x%8x,%s,%s,%p)-stub!\n", This, cfFlags, assocenum,
953 debugstr_w(pszExtra), debugstr_guid(riid), ppvOut);
954 return E_NOTIMPL;
957 static const IQueryAssociationsVtbl IQueryAssociations_vtbl =
959 IQueryAssociations_fnQueryInterface,
960 IQueryAssociations_fnAddRef,
961 IQueryAssociations_fnRelease,
962 IQueryAssociations_fnInit,
963 IQueryAssociations_fnGetString,
964 IQueryAssociations_fnGetKey,
965 IQueryAssociations_fnGetData,
966 IQueryAssociations_fnGetEnum