push 6ad4febc802779d39881ae7cccabf16fa19e6741
[wine/hacks.git] / dlls / shlwapi / assoc.c
blob31a842068b781f69f5d8be414ac9f01d7f286ae7
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));
307 if (!lpszReturnW)
308 hRet = E_OUTOFMEMORY;
309 else
311 hRet = AssocQueryStringW(cfFlags, str, lpszAssocW, lpszExtraW,
312 lpszReturnW, &dwLenOut);
314 if (SUCCEEDED(hRet))
315 WideCharToMultiByte(CP_ACP,0,szReturnW,-1,pszOut,dwLenOut,0,0);
316 *pcchOut = dwLenOut;
318 if (lpszReturnW != szReturnW)
319 HeapFree(GetProcessHeap(), 0, lpszReturnW);
323 if (lpszAssocW != szAssocW)
324 HeapFree(GetProcessHeap(), 0, lpszAssocW);
325 if (lpszExtraW != szExtraW)
326 HeapFree(GetProcessHeap(), 0, lpszExtraW);
327 return hRet;
330 /*************************************************************************
331 * AssocQueryStringByKeyW [SHLWAPI.@]
333 * See AssocQueryStringByKeyA.
335 HRESULT WINAPI AssocQueryStringByKeyW(ASSOCF cfFlags, ASSOCSTR str, HKEY hkAssoc,
336 LPCWSTR pszExtra, LPWSTR pszOut,
337 DWORD *pcchOut)
339 HRESULT hRet;
340 IQueryAssociations* lpAssoc;
342 TRACE("(0x%8x,0x%8x,%p,%s,%p,%p)\n", cfFlags, str, hkAssoc,
343 debugstr_w(pszExtra), pszOut, pcchOut);
345 lpAssoc = IQueryAssociations_Constructor();
347 if (!lpAssoc)
348 return E_OUTOFMEMORY;
350 cfFlags &= SHLWAPI_DEF_ASSOCF;
351 hRet = IQueryAssociations_Init(lpAssoc, cfFlags, 0, hkAssoc, NULL);
353 if (SUCCEEDED(hRet))
354 hRet = IQueryAssociations_GetString(lpAssoc, cfFlags, str, pszExtra,
355 pszOut, pcchOut);
357 IQueryAssociations_Release(lpAssoc);
358 return hRet;
361 /*************************************************************************
362 * AssocQueryStringByKeyA [SHLWAPI.@]
364 * Get a file association string from the registry, given a starting key.
366 * PARAMS
367 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
368 * str [I] Type of string to get
369 * hkAssoc [I] Key to search below
370 * pszExtra [I] Extra information about the string location
371 * pszOut [O] Destination for the association string
372 * pcchOut [O] Length of pszOut
374 * RETURNS
375 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
376 * Failure: An HRESULT error code indicating the error.
378 HRESULT WINAPI AssocQueryStringByKeyA(ASSOCF cfFlags, ASSOCSTR str, HKEY hkAssoc,
379 LPCSTR pszExtra, LPSTR pszOut,
380 DWORD *pcchOut)
382 WCHAR szExtraW[MAX_PATH], *lpszExtraW = szExtraW;
383 WCHAR szReturnW[MAX_PATH], *lpszReturnW = szReturnW;
384 HRESULT hRet = E_OUTOFMEMORY;
386 TRACE("(0x%8x,0x%8x,%p,%s,%p,%p)\n", cfFlags, str, hkAssoc,
387 debugstr_a(pszExtra), pszOut, pcchOut);
389 if (!pcchOut)
390 hRet = E_INVALIDARG;
391 else if (SHLWAPI_ParamAToW(pszExtra, szExtraW, MAX_PATH, &lpszExtraW))
393 DWORD dwLenOut = *pcchOut;
394 if (dwLenOut >= MAX_PATH)
395 lpszReturnW = HeapAlloc(GetProcessHeap(), 0,
396 (dwLenOut + 1) * sizeof(WCHAR));
398 if (lpszReturnW)
400 hRet = AssocQueryStringByKeyW(cfFlags, str, hkAssoc, lpszExtraW,
401 lpszReturnW, &dwLenOut);
403 if (SUCCEEDED(hRet))
404 WideCharToMultiByte(CP_ACP,0,szReturnW,-1,pszOut,dwLenOut,0,0);
405 *pcchOut = dwLenOut;
407 if (lpszReturnW != szReturnW)
408 HeapFree(GetProcessHeap(), 0, lpszReturnW);
412 if (lpszExtraW != szExtraW)
413 HeapFree(GetProcessHeap(), 0, lpszExtraW);
414 return hRet;
418 /**************************************************************************
419 * AssocIsDangerous (SHLWAPI.@)
421 * Determine if a file association is dangerous (potentially malware).
423 * PARAMS
424 * lpszAssoc [I] Name of file or file extension to check.
426 * RETURNS
427 * TRUE, if lpszAssoc may potentially be malware (executable),
428 * FALSE, Otherwise.
430 BOOL WINAPI AssocIsDangerous(LPCWSTR lpszAssoc)
432 FIXME("%s\n", debugstr_w(lpszAssoc));
433 return FALSE;
436 /**************************************************************************
437 * IQueryAssociations_QueryInterface {SHLWAPI}
439 * See IUnknown_QueryInterface.
441 static HRESULT WINAPI IQueryAssociations_fnQueryInterface(
442 IQueryAssociations* iface,
443 REFIID riid,
444 LPVOID *ppvObj)
446 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
448 TRACE("(%p,%s,%p)\n",This, debugstr_guid(riid), ppvObj);
450 *ppvObj = NULL;
452 if (IsEqualIID(riid, &IID_IUnknown) ||
453 IsEqualIID(riid, &IID_IQueryAssociations))
455 *ppvObj = (IQueryAssociations*)This;
457 IQueryAssociations_AddRef((IQueryAssociations*)*ppvObj);
458 TRACE("Returning IQueryAssociations (%p)\n", *ppvObj);
459 return S_OK;
461 TRACE("Returning E_NOINTERFACE\n");
462 return E_NOINTERFACE;
465 /**************************************************************************
466 * IQueryAssociations_AddRef {SHLWAPI}
468 * See IUnknown_AddRef.
470 static ULONG WINAPI IQueryAssociations_fnAddRef(IQueryAssociations *iface)
472 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
473 ULONG refCount = InterlockedIncrement(&This->ref);
475 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
477 return refCount;
480 /**************************************************************************
481 * IQueryAssociations_Release {SHLWAPI}
483 * See IUnknown_Release.
485 static ULONG WINAPI IQueryAssociations_fnRelease(IQueryAssociations *iface)
487 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
488 ULONG refCount = InterlockedDecrement(&This->ref);
490 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
492 if (!refCount)
494 TRACE("Destroying IQueryAssociations (%p)\n", This);
495 RegCloseKey(This->hkeySource);
496 RegCloseKey(This->hkeyProgID);
497 HeapFree(GetProcessHeap(), 0, This);
500 return refCount;
503 /**************************************************************************
504 * IQueryAssociations_Init {SHLWAPI}
506 * Initialise an IQueryAssociations object.
508 * PARAMS
509 * iface [I] IQueryAssociations interface to initialise
510 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
511 * pszAssoc [I] String for the root key name, or NULL if hkeyProgid is given
512 * hkeyProgid [I] Handle for the root key, or NULL if pszAssoc is given
513 * hWnd [I] Reserved, must be NULL.
515 * RETURNS
516 * Success: S_OK. iface is initialised with the parameters given.
517 * Failure: An HRESULT error code indicating the error.
519 static HRESULT WINAPI IQueryAssociations_fnInit(
520 IQueryAssociations *iface,
521 ASSOCF cfFlags,
522 LPCWSTR pszAssoc,
523 HKEY hkeyProgid,
524 HWND hWnd)
526 static const WCHAR szProgID[] = {'P','r','o','g','I','D',0};
527 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
528 LONG ret;
530 TRACE("(%p)->(%d,%s,%p,%p)\n", iface,
531 cfFlags,
532 debugstr_w(pszAssoc),
533 hkeyProgid,
534 hWnd);
535 if (hWnd != NULL)
536 FIXME("hwnd != NULL not supported\n");
537 if (cfFlags != 0)
538 FIXME("unsupported flags: %x\n", cfFlags);
539 if (pszAssoc != NULL)
541 ret = RegOpenKeyExW(HKEY_CLASSES_ROOT,
542 pszAssoc,
544 KEY_READ,
545 &This->hkeySource);
546 if (ret != ERROR_SUCCESS)
547 return E_FAIL;
548 /* if this is not a prog id */
549 if ((*pszAssoc == '.') || (*pszAssoc == '{'))
551 RegOpenKeyExW(This->hkeySource,
552 szProgID,
554 KEY_READ,
555 &This->hkeyProgID);
557 else
558 This->hkeyProgID = This->hkeySource;
559 return S_OK;
561 else if (hkeyProgid != NULL)
563 This->hkeyProgID = hkeyProgid;
564 return S_OK;
566 else
567 return E_INVALIDARG;
570 static HRESULT ASSOC_GetValue(HKEY hkey, WCHAR ** pszText)
572 DWORD len;
573 LONG ret;
575 assert(pszText);
576 ret = RegQueryValueExW(hkey, NULL, 0, NULL, NULL, &len);
577 if (ret != ERROR_SUCCESS)
578 return HRESULT_FROM_WIN32(ret);
579 if (!len)
580 return E_FAIL;
581 *pszText = HeapAlloc(GetProcessHeap(), 0, len);
582 if (!*pszText)
583 return E_OUTOFMEMORY;
584 ret = RegQueryValueExW(hkey, NULL, 0, NULL, (LPBYTE)*pszText,
585 &len);
586 if (ret != ERROR_SUCCESS)
588 HeapFree(GetProcessHeap(), 0, *pszText);
589 return HRESULT_FROM_WIN32(ret);
591 return S_OK;
594 static HRESULT ASSOC_GetExecutable(IQueryAssociationsImpl *This,
595 LPCWSTR pszExtra, LPWSTR path,
596 DWORD pathlen, DWORD *len)
598 HKEY hkeyCommand;
599 HKEY hkeyFile;
600 HKEY hkeyShell;
601 HKEY hkeyVerb;
602 HRESULT hr;
603 LONG ret;
604 WCHAR * pszCommand;
605 WCHAR * pszEnd;
606 WCHAR * pszExtraFromReg = NULL;
607 WCHAR * pszFileType;
608 WCHAR * pszStart;
609 static const WCHAR commandW[] = { 'c','o','m','m','a','n','d',0 };
610 static const WCHAR shellW[] = { 's','h','e','l','l',0 };
612 assert(len);
614 hr = ASSOC_GetValue(This->hkeySource, &pszFileType);
615 if (FAILED(hr))
616 return hr;
617 ret = RegOpenKeyExW(HKEY_CLASSES_ROOT, pszFileType, 0, KEY_READ, &hkeyFile);
618 HeapFree(GetProcessHeap(), 0, pszFileType);
619 if (ret != ERROR_SUCCESS)
620 return HRESULT_FROM_WIN32(ret);
622 ret = RegOpenKeyExW(hkeyFile, shellW, 0, KEY_READ, &hkeyShell);
623 RegCloseKey(hkeyFile);
624 if (ret != ERROR_SUCCESS)
625 return HRESULT_FROM_WIN32(ret);
627 if (!pszExtra)
629 hr = ASSOC_GetValue(hkeyShell, &pszExtraFromReg);
630 /* if no default action */
631 if (hr == E_FAIL || hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
633 DWORD rlen;
634 ret = RegQueryInfoKeyW(hkeyShell, 0, 0, 0, 0, &rlen, 0, 0, 0, 0, 0, 0);
635 if (ret != ERROR_SUCCESS)
637 RegCloseKey(hkeyShell);
638 return HRESULT_FROM_WIN32(ret);
640 rlen++;
641 pszExtraFromReg = HeapAlloc(GetProcessHeap(), 0, rlen * sizeof(WCHAR));
642 if (!pszExtraFromReg)
644 RegCloseKey(hkeyShell);
645 return E_OUTOFMEMORY;
647 ret = RegEnumKeyExW(hkeyShell, 0, pszExtraFromReg, &rlen, 0, NULL, NULL, NULL);
648 if (ret != ERROR_SUCCESS)
650 RegCloseKey(hkeyShell);
651 return HRESULT_FROM_WIN32(ret);
654 else if (FAILED(hr))
656 RegCloseKey(hkeyShell);
657 return hr;
661 ret = RegOpenKeyExW(hkeyShell, pszExtra ? pszExtra : pszExtraFromReg, 0,
662 KEY_READ, &hkeyVerb);
663 HeapFree(GetProcessHeap(), 0, pszExtraFromReg);
664 RegCloseKey(hkeyShell);
665 if (ret != ERROR_SUCCESS)
666 return HRESULT_FROM_WIN32(ret);
668 ret = RegOpenKeyExW(hkeyVerb, commandW, 0, KEY_READ, &hkeyCommand);
669 RegCloseKey(hkeyVerb);
670 if (ret != ERROR_SUCCESS)
671 return HRESULT_FROM_WIN32(ret);
672 hr = ASSOC_GetValue(hkeyCommand, &pszCommand);
673 RegCloseKey(hkeyCommand);
674 if (FAILED(hr))
675 return hr;
677 /* cleanup pszCommand */
678 if (pszCommand[0] == '"')
680 pszStart = pszCommand + 1;
681 pszEnd = strchrW(pszStart, '"');
683 else
685 pszStart = pszCommand;
686 pszEnd = strchrW(pszStart, ' ');
688 if (pszEnd)
689 *pszEnd = 0;
691 *len = SearchPathW(NULL, pszStart, NULL, pathlen, path, NULL);
692 HeapFree(GetProcessHeap(), 0, pszCommand);
693 if (!*len)
694 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
695 return S_OK;
698 static HRESULT ASSOC_ReturnData(LPWSTR out, DWORD *outlen, LPCWSTR data,
699 DWORD datalen)
701 assert(outlen);
703 if (out)
705 if (*outlen < datalen)
707 *outlen = datalen;
708 return E_POINTER;
710 *outlen = datalen;
711 lstrcpynW(out, data, datalen);
712 return S_OK;
714 else
716 *outlen = datalen;
717 return S_FALSE;
721 /**************************************************************************
722 * IQueryAssociations_GetString {SHLWAPI}
724 * Get a file association string from the registry.
726 * PARAMS
727 * iface [I] IQueryAssociations interface to query
728 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
729 * str [I] Type of string to get (ASSOCSTR enum from "shlwapi.h")
730 * pszExtra [I] Extra information about the string location
731 * pszOut [O] Destination for the association string
732 * pcchOut [I/O] Length of pszOut
734 * RETURNS
735 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
736 * Failure: An HRESULT error code indicating the error.
738 static HRESULT WINAPI IQueryAssociations_fnGetString(
739 IQueryAssociations *iface,
740 ASSOCF cfFlags,
741 ASSOCSTR str,
742 LPCWSTR pszExtra,
743 LPWSTR pszOut,
744 DWORD *pcchOut)
746 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
747 const ASSOCF cfUnimplemented = ~(0);
748 DWORD len = 0;
749 HRESULT hr;
750 WCHAR path[MAX_PATH];
752 TRACE("(%p,0x%8x,0x%8x,%s,%p,%p)\n", This, cfFlags, str,
753 debugstr_w(pszExtra), pszOut, pcchOut);
755 if (cfFlags & cfUnimplemented)
756 FIXME("%08x: unimplemented flags!\n", cfFlags & cfUnimplemented);
758 if (!pcchOut)
759 return E_UNEXPECTED;
761 switch (str)
763 case ASSOCSTR_EXECUTABLE:
765 hr = ASSOC_GetExecutable(This, pszExtra, path, MAX_PATH, &len);
766 if (FAILED(hr))
767 return hr;
768 len++;
769 return ASSOC_ReturnData(pszOut, pcchOut, path, len);
772 case ASSOCSTR_FRIENDLYAPPNAME:
774 PVOID verinfoW = NULL;
775 DWORD size, retval = 0;
776 UINT flen;
777 WCHAR *bufW;
778 static const WCHAR translationW[] = {
779 '\\','V','a','r','F','i','l','e','I','n','f','o',
780 '\\','T','r','a','n','s','l','a','t','i','o','n',0
782 static const WCHAR fileDescFmtW[] = {
783 '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
784 '\\','%','0','4','x','%','0','4','x',
785 '\\','F','i','l','e','D','e','s','c','r','i','p','t','i','o','n',0
787 WCHAR fileDescW[41];
789 hr = ASSOC_GetExecutable(This, pszExtra, path, MAX_PATH, &len);
790 if (FAILED(hr))
791 return hr;
793 retval = GetFileVersionInfoSizeW(path, &size);
794 if (!retval)
795 goto get_friendly_name_fail;
796 verinfoW = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, retval);
797 if (!verinfoW)
798 return E_OUTOFMEMORY;
799 if (!GetFileVersionInfoW(path, 0, retval, verinfoW))
800 goto get_friendly_name_fail;
801 if (VerQueryValueW(verinfoW, translationW, (LPVOID *)&bufW, &flen))
803 int i;
804 DWORD *langCodeDesc = (DWORD *)bufW;
805 for (i = 0; i < flen / sizeof(DWORD); i++)
807 sprintfW(fileDescW, fileDescFmtW, LOWORD(langCodeDesc[i]),
808 HIWORD(langCodeDesc[i]));
809 if (VerQueryValueW(verinfoW, fileDescW, (LPVOID *)&bufW, &flen))
811 /* Does strlenW(bufW) == 0 mean we use the filename? */
812 len = strlenW(bufW) + 1;
813 TRACE("found FileDescription: %s\n", debugstr_w(bufW));
814 return ASSOC_ReturnData(pszOut, pcchOut, bufW, len);
818 get_friendly_name_fail:
819 PathRemoveExtensionW(path);
820 PathStripPathW(path);
821 TRACE("using filename: %s\n", debugstr_w(path));
822 return ASSOC_ReturnData(pszOut, pcchOut, path, strlenW(path) + 1);
825 default:
826 FIXME("assocstr %d unimplemented!\n", str);
827 return E_NOTIMPL;
831 /**************************************************************************
832 * IQueryAssociations_GetKey {SHLWAPI}
834 * Get a file association key from the registry.
836 * PARAMS
837 * iface [I] IQueryAssociations interface to query
838 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
839 * assockey [I] Type of key to get (ASSOCKEY enum from "shlwapi.h")
840 * pszExtra [I] Extra information about the key location
841 * phkeyOut [O] Destination for the association key
843 * RETURNS
844 * Success: S_OK. phkeyOut contains a handle to the key.
845 * Failure: An HRESULT error code indicating the error.
847 static HRESULT WINAPI IQueryAssociations_fnGetKey(
848 IQueryAssociations *iface,
849 ASSOCF cfFlags,
850 ASSOCKEY assockey,
851 LPCWSTR pszExtra,
852 HKEY *phkeyOut)
854 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
856 FIXME("(%p,0x%8x,0x%8x,%s,%p)-stub!\n", This, cfFlags, assockey,
857 debugstr_w(pszExtra), phkeyOut);
858 return E_NOTIMPL;
861 /**************************************************************************
862 * IQueryAssociations_GetData {SHLWAPI}
864 * Get the data for a file association key from the registry.
866 * PARAMS
867 * iface [I] IQueryAssociations interface to query
868 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
869 * assocdata [I] Type of data to get (ASSOCDATA enum from "shlwapi.h")
870 * pszExtra [I] Extra information about the data location
871 * pvOut [O] Destination for the association key
872 * pcbOut [I/O] Size of pvOut
874 * RETURNS
875 * Success: S_OK. pszOut contains the data, pcbOut contains its length.
876 * Failure: An HRESULT error code indicating the error.
878 static HRESULT WINAPI IQueryAssociations_fnGetData(
879 IQueryAssociations *iface,
880 ASSOCF cfFlags,
881 ASSOCDATA assocdata,
882 LPCWSTR pszExtra,
883 LPVOID pvOut,
884 DWORD *pcbOut)
886 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
888 FIXME("(%p,0x%8x,0x%8x,%s,%p,%p)-stub!\n", This, cfFlags, assocdata,
889 debugstr_w(pszExtra), pvOut, pcbOut);
890 return E_NOTIMPL;
893 /**************************************************************************
894 * IQueryAssociations_GetEnum {SHLWAPI}
896 * Not yet implemented in native Win32.
898 * PARAMS
899 * iface [I] IQueryAssociations interface to query
900 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
901 * assocenum [I] Type of enum to get (ASSOCENUM enum from "shlwapi.h")
902 * pszExtra [I] Extra information about the enum location
903 * riid [I] REFIID to look for
904 * ppvOut [O] Destination for the interface.
906 * RETURNS
907 * Success: S_OK.
908 * Failure: An HRESULT error code indicating the error.
910 * NOTES
911 * Presumably this function returns an enumerator object.
913 static HRESULT WINAPI IQueryAssociations_fnGetEnum(
914 IQueryAssociations *iface,
915 ASSOCF cfFlags,
916 ASSOCENUM assocenum,
917 LPCWSTR pszExtra,
918 REFIID riid,
919 LPVOID *ppvOut)
921 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
923 FIXME("(%p,0x%8x,0x%8x,%s,%s,%p)-stub!\n", This, cfFlags, assocenum,
924 debugstr_w(pszExtra), debugstr_guid(riid), ppvOut);
925 return E_NOTIMPL;
928 static const IQueryAssociationsVtbl IQueryAssociations_vtbl =
930 IQueryAssociations_fnQueryInterface,
931 IQueryAssociations_fnAddRef,
932 IQueryAssociations_fnRelease,
933 IQueryAssociations_fnInit,
934 IQueryAssociations_fnGetString,
935 IQueryAssociations_fnGetKey,
936 IQueryAssociations_fnGetData,
937 IQueryAssociations_fnGetEnum