comdlg32: Fix a crash in LookIn combo box WM_COMMAND message handler.
[wine/hacks.git] / dlls / shlwapi / assoc.c
blobd13f0aa765fa6c8756bac2e53e134c8e95003a8c
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 = 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_GetCommand(IQueryAssociationsImpl *This,
598 LPCWSTR pszExtra, WCHAR **ppszCommand)
600 HKEY hkeyCommand;
601 HKEY hkeyFile;
602 HKEY hkeyShell;
603 HKEY hkeyVerb;
604 HRESULT hr;
605 LONG ret;
606 WCHAR * pszExtraFromReg = NULL;
607 WCHAR * pszFileType;
608 static const WCHAR commandW[] = { 'c','o','m','m','a','n','d',0 };
609 static const WCHAR shellW[] = { 's','h','e','l','l',0 };
611 hr = ASSOC_GetValue(This->hkeySource, &pszFileType);
612 if (FAILED(hr))
613 return hr;
614 ret = RegOpenKeyExW(HKEY_CLASSES_ROOT, pszFileType, 0, KEY_READ, &hkeyFile);
615 HeapFree(GetProcessHeap(), 0, pszFileType);
616 if (ret != ERROR_SUCCESS)
617 return HRESULT_FROM_WIN32(ret);
619 ret = RegOpenKeyExW(hkeyFile, shellW, 0, KEY_READ, &hkeyShell);
620 RegCloseKey(hkeyFile);
621 if (ret != ERROR_SUCCESS)
622 return HRESULT_FROM_WIN32(ret);
624 if (!pszExtra)
626 hr = ASSOC_GetValue(hkeyShell, &pszExtraFromReg);
627 /* if no default action */
628 if (hr == E_FAIL || hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
630 DWORD rlen;
631 ret = RegQueryInfoKeyW(hkeyShell, 0, 0, 0, 0, &rlen, 0, 0, 0, 0, 0, 0);
632 if (ret != ERROR_SUCCESS)
634 RegCloseKey(hkeyShell);
635 return HRESULT_FROM_WIN32(ret);
637 rlen++;
638 pszExtraFromReg = HeapAlloc(GetProcessHeap(), 0, rlen * sizeof(WCHAR));
639 if (!pszExtraFromReg)
641 RegCloseKey(hkeyShell);
642 return E_OUTOFMEMORY;
644 ret = RegEnumKeyExW(hkeyShell, 0, pszExtraFromReg, &rlen, 0, NULL, NULL, NULL);
645 if (ret != ERROR_SUCCESS)
647 RegCloseKey(hkeyShell);
648 return HRESULT_FROM_WIN32(ret);
651 else if (FAILED(hr))
653 RegCloseKey(hkeyShell);
654 return hr;
658 ret = RegOpenKeyExW(hkeyShell, pszExtra ? pszExtra : pszExtraFromReg, 0,
659 KEY_READ, &hkeyVerb);
660 HeapFree(GetProcessHeap(), 0, pszExtraFromReg);
661 RegCloseKey(hkeyShell);
662 if (ret != ERROR_SUCCESS)
663 return HRESULT_FROM_WIN32(ret);
665 ret = RegOpenKeyExW(hkeyVerb, commandW, 0, KEY_READ, &hkeyCommand);
666 RegCloseKey(hkeyVerb);
667 if (ret != ERROR_SUCCESS)
668 return HRESULT_FROM_WIN32(ret);
669 hr = ASSOC_GetValue(hkeyCommand, ppszCommand);
670 RegCloseKey(hkeyCommand);
671 return hr;
674 static HRESULT ASSOC_GetExecutable(IQueryAssociationsImpl *This,
675 LPCWSTR pszExtra, LPWSTR path,
676 DWORD pathlen, DWORD *len)
678 WCHAR *pszCommand;
679 WCHAR *pszStart;
680 WCHAR *pszEnd;
681 HRESULT hr;
683 assert(len);
685 hr = ASSOC_GetCommand(This, pszExtra, &pszCommand);
686 if (FAILED(hr))
687 return hr;
689 /* cleanup pszCommand */
690 if (pszCommand[0] == '"')
692 pszStart = pszCommand + 1;
693 pszEnd = strchrW(pszStart, '"');
695 else
697 pszStart = pszCommand;
698 pszEnd = strchrW(pszStart, ' ');
700 if (pszEnd)
701 *pszEnd = 0;
703 *len = SearchPathW(NULL, pszStart, NULL, pathlen, path, NULL);
704 HeapFree(GetProcessHeap(), 0, pszCommand);
705 if (!*len)
706 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
707 return S_OK;
710 static HRESULT ASSOC_ReturnData(LPWSTR out, DWORD *outlen, LPCWSTR data,
711 DWORD datalen)
713 assert(outlen);
715 if (out)
717 if (*outlen < datalen)
719 *outlen = datalen;
720 return E_POINTER;
722 *outlen = datalen;
723 lstrcpynW(out, data, datalen);
724 return S_OK;
726 else
728 *outlen = datalen;
729 return S_FALSE;
733 /**************************************************************************
734 * IQueryAssociations_GetString {SHLWAPI}
736 * Get a file association string from the registry.
738 * PARAMS
739 * iface [I] IQueryAssociations interface to query
740 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
741 * str [I] Type of string to get (ASSOCSTR enum from "shlwapi.h")
742 * pszExtra [I] Extra information about the string location
743 * pszOut [O] Destination for the association string
744 * pcchOut [I/O] Length of pszOut
746 * RETURNS
747 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
748 * Failure: An HRESULT error code indicating the error.
750 static HRESULT WINAPI IQueryAssociations_fnGetString(
751 IQueryAssociations *iface,
752 ASSOCF cfFlags,
753 ASSOCSTR str,
754 LPCWSTR pszExtra,
755 LPWSTR pszOut,
756 DWORD *pcchOut)
758 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
759 const ASSOCF cfUnimplemented = ~(0);
760 DWORD len = 0;
761 HRESULT hr;
762 WCHAR path[MAX_PATH];
764 TRACE("(%p,0x%8x,0x%8x,%s,%p,%p)\n", This, cfFlags, str,
765 debugstr_w(pszExtra), pszOut, pcchOut);
767 if (cfFlags & cfUnimplemented)
768 FIXME("%08x: unimplemented flags!\n", cfFlags & cfUnimplemented);
770 if (!pcchOut)
771 return E_UNEXPECTED;
773 switch (str)
775 case ASSOCSTR_COMMAND:
777 WCHAR *command;
778 hr = ASSOC_GetCommand(This, pszExtra, &command);
779 if (SUCCEEDED(hr))
781 hr = ASSOC_ReturnData(pszOut, pcchOut, command, strlenW(command) + 1);
782 HeapFree(GetProcessHeap(), 0, command);
784 return hr;
787 case ASSOCSTR_EXECUTABLE:
789 hr = ASSOC_GetExecutable(This, pszExtra, path, MAX_PATH, &len);
790 if (FAILED(hr))
791 return hr;
792 len++;
793 return ASSOC_ReturnData(pszOut, pcchOut, path, len);
796 case ASSOCSTR_FRIENDLYDOCNAME:
798 WCHAR *pszFileType;
799 DWORD ret;
800 DWORD size;
802 hr = ASSOC_GetValue(This->hkeySource, &pszFileType);
803 if (FAILED(hr))
804 return hr;
805 size = 0;
806 ret = RegGetValueW(HKEY_CLASSES_ROOT, pszFileType, NULL, RRF_RT_REG_SZ, NULL, NULL, &size);
807 if (ret == ERROR_SUCCESS)
809 WCHAR *docName = HeapAlloc(GetProcessHeap(), 0, size);
810 if (docName)
812 ret = RegGetValueW(HKEY_CLASSES_ROOT, pszFileType, NULL, RRF_RT_REG_SZ, NULL, docName, &size);
813 if (ret == ERROR_SUCCESS)
814 hr = ASSOC_ReturnData(pszOut, pcchOut, docName, strlenW(docName) + 1);
815 else
816 hr = HRESULT_FROM_WIN32(ret);
817 HeapFree(GetProcessHeap(), 0, docName);
819 else
820 hr = E_OUTOFMEMORY;
822 else
823 hr = HRESULT_FROM_WIN32(ret);
824 HeapFree(GetProcessHeap(), 0, pszFileType);
825 return hr;
828 case ASSOCSTR_FRIENDLYAPPNAME:
830 PVOID verinfoW = NULL;
831 DWORD size, retval = 0;
832 UINT flen;
833 WCHAR *bufW;
834 static const WCHAR translationW[] = {
835 '\\','V','a','r','F','i','l','e','I','n','f','o',
836 '\\','T','r','a','n','s','l','a','t','i','o','n',0
838 static const WCHAR fileDescFmtW[] = {
839 '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
840 '\\','%','0','4','x','%','0','4','x',
841 '\\','F','i','l','e','D','e','s','c','r','i','p','t','i','o','n',0
843 WCHAR fileDescW[41];
845 hr = ASSOC_GetExecutable(This, pszExtra, path, MAX_PATH, &len);
846 if (FAILED(hr))
847 return hr;
849 retval = GetFileVersionInfoSizeW(path, &size);
850 if (!retval)
851 goto get_friendly_name_fail;
852 verinfoW = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, retval);
853 if (!verinfoW)
854 return E_OUTOFMEMORY;
855 if (!GetFileVersionInfoW(path, 0, retval, verinfoW))
856 goto get_friendly_name_fail;
857 if (VerQueryValueW(verinfoW, translationW, (LPVOID *)&bufW, &flen))
859 UINT i;
860 DWORD *langCodeDesc = (DWORD *)bufW;
861 for (i = 0; i < flen / sizeof(DWORD); i++)
863 sprintfW(fileDescW, fileDescFmtW, LOWORD(langCodeDesc[i]),
864 HIWORD(langCodeDesc[i]));
865 if (VerQueryValueW(verinfoW, fileDescW, (LPVOID *)&bufW, &flen))
867 /* Does strlenW(bufW) == 0 mean we use the filename? */
868 len = strlenW(bufW) + 1;
869 TRACE("found FileDescription: %s\n", debugstr_w(bufW));
870 return ASSOC_ReturnData(pszOut, pcchOut, bufW, len);
874 get_friendly_name_fail:
875 PathRemoveExtensionW(path);
876 PathStripPathW(path);
877 TRACE("using filename: %s\n", debugstr_w(path));
878 return ASSOC_ReturnData(pszOut, pcchOut, path, strlenW(path) + 1);
881 case ASSOCSTR_CONTENTTYPE:
883 static const WCHAR Content_TypeW[] = {'C','o','n','t','e','n','t',' ','T','y','p','e',0};
884 WCHAR *contentType;
885 DWORD ret;
886 DWORD size;
888 size = 0;
889 ret = RegGetValueW(This->hkeySource, NULL, Content_TypeW, RRF_RT_REG_SZ, NULL, NULL, &size);
890 if (ret != ERROR_SUCCESS)
891 return HRESULT_FROM_WIN32(ret);
892 contentType = HeapAlloc(GetProcessHeap(), 0, size);
893 if (contentType != NULL)
895 ret = RegGetValueW(This->hkeySource, NULL, Content_TypeW, RRF_RT_REG_SZ, NULL, contentType, &size);
896 if (ret == ERROR_SUCCESS)
897 hr = ASSOC_ReturnData(pszOut, pcchOut, contentType, strlenW(contentType) + 1);
898 else
899 hr = HRESULT_FROM_WIN32(ret);
900 HeapFree(GetProcessHeap(), 0, contentType);
902 else
903 hr = E_OUTOFMEMORY;
904 return hr;
907 case ASSOCSTR_DEFAULTICON:
909 static const WCHAR DefaultIconW[] = {'D','e','f','a','u','l','t','I','c','o','n',0};
910 WCHAR *pszFileType;
911 DWORD ret;
912 DWORD size;
913 HKEY hkeyFile;
915 hr = ASSOC_GetValue(This->hkeySource, &pszFileType);
916 if (FAILED(hr))
917 return hr;
918 ret = RegOpenKeyExW(HKEY_CLASSES_ROOT, pszFileType, 0, KEY_READ, &hkeyFile);
919 if (ret == ERROR_SUCCESS)
921 size = 0;
922 ret = RegGetValueW(hkeyFile, DefaultIconW, NULL, RRF_RT_REG_SZ, NULL, NULL, &size);
923 if (ret == ERROR_SUCCESS)
925 WCHAR *icon = HeapAlloc(GetProcessHeap(), 0, size);
926 if (icon)
928 ret = RegGetValueW(hkeyFile, DefaultIconW, NULL, RRF_RT_REG_SZ, NULL, icon, &size);
929 if (ret == ERROR_SUCCESS)
930 hr = ASSOC_ReturnData(pszOut, pcchOut, icon, strlenW(icon) + 1);
931 else
932 hr = HRESULT_FROM_WIN32(ret);
933 HeapFree(GetProcessHeap(), 0, icon);
935 else
936 hr = E_OUTOFMEMORY;
938 else
939 hr = HRESULT_FROM_WIN32(ret);
940 RegCloseKey(hkeyFile);
942 else
943 hr = HRESULT_FROM_WIN32(ret);
944 HeapFree(GetProcessHeap(), 0, pszFileType);
945 return hr;
948 default:
949 FIXME("assocstr %d unimplemented!\n", str);
950 return E_NOTIMPL;
954 /**************************************************************************
955 * IQueryAssociations_GetKey {SHLWAPI}
957 * Get a file association key from the registry.
959 * PARAMS
960 * iface [I] IQueryAssociations interface to query
961 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
962 * assockey [I] Type of key to get (ASSOCKEY enum from "shlwapi.h")
963 * pszExtra [I] Extra information about the key location
964 * phkeyOut [O] Destination for the association key
966 * RETURNS
967 * Success: S_OK. phkeyOut contains a handle to the key.
968 * Failure: An HRESULT error code indicating the error.
970 static HRESULT WINAPI IQueryAssociations_fnGetKey(
971 IQueryAssociations *iface,
972 ASSOCF cfFlags,
973 ASSOCKEY assockey,
974 LPCWSTR pszExtra,
975 HKEY *phkeyOut)
977 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
979 FIXME("(%p,0x%8x,0x%8x,%s,%p)-stub!\n", This, cfFlags, assockey,
980 debugstr_w(pszExtra), phkeyOut);
981 return E_NOTIMPL;
984 /**************************************************************************
985 * IQueryAssociations_GetData {SHLWAPI}
987 * Get the data for a file association key from the registry.
989 * PARAMS
990 * iface [I] IQueryAssociations interface to query
991 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
992 * assocdata [I] Type of data to get (ASSOCDATA enum from "shlwapi.h")
993 * pszExtra [I] Extra information about the data location
994 * pvOut [O] Destination for the association key
995 * pcbOut [I/O] Size of pvOut
997 * RETURNS
998 * Success: S_OK. pszOut contains the data, pcbOut contains its length.
999 * Failure: An HRESULT error code indicating the error.
1001 static HRESULT WINAPI IQueryAssociations_fnGetData(
1002 IQueryAssociations *iface,
1003 ASSOCF cfFlags,
1004 ASSOCDATA assocdata,
1005 LPCWSTR pszExtra,
1006 LPVOID pvOut,
1007 DWORD *pcbOut)
1009 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
1011 FIXME("(%p,0x%8x,0x%8x,%s,%p,%p)-stub!\n", This, cfFlags, assocdata,
1012 debugstr_w(pszExtra), pvOut, pcbOut);
1013 return E_NOTIMPL;
1016 /**************************************************************************
1017 * IQueryAssociations_GetEnum {SHLWAPI}
1019 * Not yet implemented in native Win32.
1021 * PARAMS
1022 * iface [I] IQueryAssociations interface to query
1023 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
1024 * assocenum [I] Type of enum to get (ASSOCENUM enum from "shlwapi.h")
1025 * pszExtra [I] Extra information about the enum location
1026 * riid [I] REFIID to look for
1027 * ppvOut [O] Destination for the interface.
1029 * RETURNS
1030 * Success: S_OK.
1031 * Failure: An HRESULT error code indicating the error.
1033 * NOTES
1034 * Presumably this function returns an enumerator object.
1036 static HRESULT WINAPI IQueryAssociations_fnGetEnum(
1037 IQueryAssociations *iface,
1038 ASSOCF cfFlags,
1039 ASSOCENUM assocenum,
1040 LPCWSTR pszExtra,
1041 REFIID riid,
1042 LPVOID *ppvOut)
1044 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
1046 FIXME("(%p,0x%8x,0x%8x,%s,%s,%p)-stub!\n", This, cfFlags, assocenum,
1047 debugstr_w(pszExtra), debugstr_guid(riid), ppvOut);
1048 return E_NOTIMPL;
1051 static const IQueryAssociationsVtbl IQueryAssociations_vtbl =
1053 IQueryAssociations_fnQueryInterface,
1054 IQueryAssociations_fnAddRef,
1055 IQueryAssociations_fnRelease,
1056 IQueryAssociations_fnInit,
1057 IQueryAssociations_fnGetString,
1058 IQueryAssociations_fnGetKey,
1059 IQueryAssociations_fnGetData,
1060 IQueryAssociations_fnGetEnum