1 /*
2 * IQueryAssociations object and helper functions
3 *
4 * Copyright 2002 Jon Griffiths
5 *
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.
10 *
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.
15 *
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
19 */
20 #include <stdarg.h>
21 #include <assert.h>
22
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"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(shell);
35
36 /**************************************************************************
37 * IQueryAssociations {SHLWAPI}
38 *
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).
47 *
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:
54 *
55 * METHODS
56 */
57
58 /* Default IQueryAssociations::Init() flags */
59 #define SHLWAPI_DEF_ASSOCF (ASSOCF_INIT_BYEXENAME|ASSOCF_INIT_DEFAULTTOSTAR| \
60 ASSOCF_INIT_DEFAULTTOFOLDER)
61
62 typedef struct
63 {
64 const IQueryAssociationsVtbl *lpVtbl;
65 LONG ref;
66 HKEY hkeySource;
67 HKEY hkeyProgID;
68 } IQueryAssociationsImpl;
69
70 static const IQueryAssociationsVtbl IQueryAssociations_vtbl;
71
72 /**************************************************************************
73 * IQueryAssociations_Constructor [internal]
74 *
75 * Construct a new IQueryAssociations object.
76 */
77 static IQueryAssociations* IQueryAssociations_Constructor(void)
78 {
79 IQueryAssociationsImpl* iface;
80
81 iface = HeapAlloc(GetProcessHeap(),0,sizeof(IQueryAssociationsImpl));
82 iface->lpVtbl = &IQueryAssociations_vtbl;
83 iface->ref = 1;
84 iface->hkeySource = NULL;
85 iface->hkeyProgID = NULL;
86
87 TRACE("Returning IQueryAssociations* %p\n", iface);
88 return (IQueryAssociations*)iface;
89 }
90
91 /*************************************************************************
92 * SHLWAPI_ParamAToW
93 *
94 * Internal helper function: Convert ASCII parameter to Unicode.
95 */
96 static BOOL SHLWAPI_ParamAToW(LPCSTR lpszParam, LPWSTR lpszBuff, DWORD dwLen,
97 LPWSTR* lpszOut)
98 {
99 if (lpszParam)
100 {
101 DWORD dwStrLen = MultiByteToWideChar(CP_ACP, 0, lpszParam, -1, NULL, 0);
102
103 if (dwStrLen < dwLen)
104 {
105 *lpszOut = lpszBuff; /* Use Buffer, it is big enough */
106 }
107 else
108 {
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;
114 }
115 MultiByteToWideChar(CP_ACP, 0, lpszParam, -1, *lpszOut, dwStrLen);
116 }
117 else
118 *lpszOut = NULL;
119 return TRUE;
120 }
121
122 /*************************************************************************
123 * AssocCreate [SHLWAPI.@]
124 *
125 * Create a new IQueryAssociations object.
126 *
127 * PARAMS
128 * clsid [I] CLSID of object
129 * refiid [I] REFIID of interface
130 * lpInterface [O] Destination for the created IQueryAssociations object
131 *
132 * RETURNS
133 * Success: S_OK. lpInterface contains the new object.
134 * Failure: An HRESULT error code indicating the error.
135 *
136 * NOTES
137 * refiid must be equal to IID_IQueryAssociations, or this function will fail.
138 */
139 HRESULT WINAPI AssocCreate(CLSID clsid, REFIID refiid, void **lpInterface)
140 {
141 HRESULT hRet;
142 IQueryAssociations* lpAssoc;
143
144 TRACE("(%s,%s,%p)\n", debugstr_guid(&clsid), debugstr_guid(refiid),
145 lpInterface);
146
147 if (!lpInterface)
148 return E_INVALIDARG;
149
150 *(DWORD*)lpInterface = 0;
151
152 if (!IsEqualGUID(&clsid, &IID_IQueryAssociations))
153 return E_NOTIMPL;
154
155 lpAssoc = IQueryAssociations_Constructor();
156
157 if (!lpAssoc)
158 return E_OUTOFMEMORY;
159
160 hRet = IQueryAssociations_QueryInterface(lpAssoc, refiid, lpInterface);
161 IQueryAssociations_Release(lpAssoc);
162 return hRet;
163 }
164
165 /*************************************************************************
166 * AssocQueryKeyW [SHLWAPI.@]
167 *
168 * See AssocQueryKeyA.
169 */
170 HRESULT WINAPI AssocQueryKeyW(ASSOCF cfFlags, ASSOCKEY assockey, LPCWSTR pszAssoc,
171 LPCWSTR pszExtra, HKEY *phkeyOut)
172 {
173 HRESULT hRet;
174 IQueryAssociations* lpAssoc;
175
176 TRACE("(0x%8x,0x%8x,%s,%s,%p)\n", cfFlags, assockey, debugstr_w(pszAssoc),
177 debugstr_w(pszExtra), phkeyOut);
178
179 lpAssoc = IQueryAssociations_Constructor();
180
181 if (!lpAssoc)
182 return E_OUTOFMEMORY;
183
184 cfFlags &= SHLWAPI_DEF_ASSOCF;
185 hRet = IQueryAssociations_Init(lpAssoc, cfFlags, pszAssoc, NULL, NULL);
186
187 if (SUCCEEDED(hRet))
188 hRet = IQueryAssociations_GetKey(lpAssoc, cfFlags, assockey, pszExtra, phkeyOut);
189
190 IQueryAssociations_Release(lpAssoc);
191 return hRet;
192 }
193
194 /*************************************************************************
195 * AssocQueryKeyA [SHLWAPI.@]
196 *
197 * Get a file association key from the registry.
198 *
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
205 *
206 * RETURNS
207 * Success: S_OK. phkeyOut contains the key.
208 * Failure: An HRESULT error code indicating the error.
209 */
210 HRESULT WINAPI AssocQueryKeyA(ASSOCF cfFlags, ASSOCKEY assockey, LPCSTR pszAssoc,
211 LPCSTR pszExtra, HKEY *phkeyOut)
212 {
213 WCHAR szAssocW[MAX_PATH], *lpszAssocW = NULL;
214 WCHAR szExtraW[MAX_PATH], *lpszExtraW = NULL;
215 HRESULT hRet = E_OUTOFMEMORY;
216
217 TRACE("(0x%8x,0x%8x,%s,%s,%p)\n", cfFlags, assockey, debugstr_a(pszAssoc),
218 debugstr_a(pszExtra), phkeyOut);
219
220 if (SHLWAPI_ParamAToW(pszAssoc, szAssocW, MAX_PATH, &lpszAssocW) &&
221 SHLWAPI_ParamAToW(pszExtra, szExtraW, MAX_PATH, &lpszExtraW))
222 {
223 hRet = AssocQueryKeyW(cfFlags, assockey, lpszAssocW, lpszExtraW, phkeyOut);
224 }
225
226 if (lpszAssocW != szAssocW)
227 HeapFree(GetProcessHeap(), 0, lpszAssocW);
228
229 if (lpszExtraW != szExtraW)
230 HeapFree(GetProcessHeap(), 0, lpszExtraW);
231
232 return hRet;
233 }
234
235 /*************************************************************************
236 * AssocQueryStringW [SHLWAPI.@]
237 *
238 * See AssocQueryStringA.
239 */
240 HRESULT WINAPI AssocQueryStringW(ASSOCF cfFlags, ASSOCSTR str, LPCWSTR pszAssoc,
241 LPCWSTR pszExtra, LPWSTR pszOut, DWORD *pcchOut)
242 {
243 HRESULT hRet;
244 IQueryAssociations* lpAssoc;
245
246 TRACE("(0x%8x,0x%8x,%s,%s,%p,%p)\n", cfFlags, str, debugstr_w(pszAssoc),
247 debugstr_w(pszExtra), pszOut, pcchOut);
248
249 if (!pcchOut)
250 return E_UNEXPECTED;
251
252 lpAssoc = IQueryAssociations_Constructor();
253
254 if (!lpAssoc)
255 return E_OUTOFMEMORY;
256
257 hRet = IQueryAssociations_Init(lpAssoc, cfFlags & SHLWAPI_DEF_ASSOCF,
258 pszAssoc, NULL, NULL);
259
260 if (SUCCEEDED(hRet))
261 hRet = IQueryAssociations_GetString(lpAssoc, cfFlags, str, pszExtra,
262 pszOut, pcchOut);
263
264 IQueryAssociations_Release(lpAssoc);
265 return hRet;
266 }
267
268 /*************************************************************************
269 * AssocQueryStringA [SHLWAPI.@]
270 *
271 * Get a file association string from the registry.
272 *
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
280 *
281 * RETURNS
282 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
283 * Failure: An HRESULT error code indicating the error.
284 */
285 HRESULT WINAPI AssocQueryStringA(ASSOCF cfFlags, ASSOCSTR str, LPCSTR pszAssoc,
286 LPCSTR pszExtra, LPSTR pszOut, DWORD *pcchOut)
287 {
288 WCHAR szAssocW[MAX_PATH], *lpszAssocW = NULL;
289 WCHAR szExtraW[MAX_PATH], *lpszExtraW = NULL;
290 HRESULT hRet = E_OUTOFMEMORY;
291
292 TRACE("(0x%8x,0x%8x,%s,%s,%p,%p)\n", cfFlags, str, debugstr_a(pszAssoc),
293 debugstr_a(pszExtra), pszOut, pcchOut);
294
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))
299 {
300 WCHAR szReturnW[MAX_PATH], *lpszReturnW = szReturnW;
301 DWORD dwLenOut = *pcchOut;
302
303 if (dwLenOut >= MAX_PATH)
304 lpszReturnW = HeapAlloc(GetProcessHeap(), 0,
305 (dwLenOut + 1) * sizeof(WCHAR));
306 else
307 dwLenOut = sizeof(szReturnW) / sizeof(szReturnW[0]);
308
309 if (!lpszReturnW)
310 hRet = E_OUTOFMEMORY;
311 else
312 {
313 hRet = AssocQueryStringW(cfFlags, str, lpszAssocW, lpszExtraW,
314 lpszReturnW, &dwLenOut);
315
316 if (SUCCEEDED(hRet))
317 dwLenOut = WideCharToMultiByte(CP_ACP, 0, lpszReturnW, -1,
318 pszOut, *pcchOut, NULL, NULL);
319
320 *pcchOut = dwLenOut;
321 if (lpszReturnW != szReturnW)
322 HeapFree(GetProcessHeap(), 0, lpszReturnW);
323 }
324 }
325
326 if (lpszAssocW != szAssocW)
327 HeapFree(GetProcessHeap(), 0, lpszAssocW);
328 if (lpszExtraW != szExtraW)
329 HeapFree(GetProcessHeap(), 0, lpszExtraW);
330 return hRet;
331 }
332
333 /*************************************************************************
334 * AssocQueryStringByKeyW [SHLWAPI.@]
335 *
336 * See AssocQueryStringByKeyA.
337 */
338 HRESULT WINAPI AssocQueryStringByKeyW(ASSOCF cfFlags, ASSOCSTR str, HKEY hkAssoc,
339 LPCWSTR pszExtra, LPWSTR pszOut,
340 DWORD *pcchOut)
341 {
342 HRESULT hRet;
343 IQueryAssociations* lpAssoc;
344
345 TRACE("(0x%8x,0x%8x,%p,%s,%p,%p)\n", cfFlags, str, hkAssoc,
346 debugstr_w(pszExtra), pszOut, pcchOut);
347
348 lpAssoc = IQueryAssociations_Constructor();
349
350 if (!lpAssoc)
351 return E_OUTOFMEMORY;
352
353 cfFlags &= SHLWAPI_DEF_ASSOCF;
354 hRet = IQueryAssociations_Init(lpAssoc, cfFlags, 0, hkAssoc, NULL);
355
356 if (SUCCEEDED(hRet))
357 hRet = IQueryAssociations_GetString(lpAssoc, cfFlags, str, pszExtra,
358 pszOut, pcchOut);
359
360 IQueryAssociations_Release(lpAssoc);
361 return hRet;
362 }
363
364 /*************************************************************************
365 * AssocQueryStringByKeyA [SHLWAPI.@]
366 *
367 * Get a file association string from the registry, given a starting key.
368 *
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
376 *
377 * RETURNS
378 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
379 * Failure: An HRESULT error code indicating the error.
380 */
381 HRESULT WINAPI AssocQueryStringByKeyA(ASSOCF cfFlags, ASSOCSTR str, HKEY hkAssoc,
382 LPCSTR pszExtra, LPSTR pszOut,
383 DWORD *pcchOut)
384 {
385 WCHAR szExtraW[MAX_PATH], *lpszExtraW = szExtraW;
386 WCHAR szReturnW[MAX_PATH], *lpszReturnW = szReturnW;
387 HRESULT hRet = E_OUTOFMEMORY;
388
389 TRACE("(0x%8x,0x%8x,%p,%s,%p,%p)\n", cfFlags, str, hkAssoc,
390 debugstr_a(pszExtra), pszOut, pcchOut);
391
392 if (!pcchOut)
393 hRet = E_INVALIDARG;
394 else if (SHLWAPI_ParamAToW(pszExtra, szExtraW, MAX_PATH, &lpszExtraW))
395 {
396 DWORD dwLenOut = *pcchOut;
397 if (dwLenOut >= MAX_PATH)
398 lpszReturnW = HeapAlloc(GetProcessHeap(), 0,
399 (dwLenOut + 1) * sizeof(WCHAR));
400
401 if (lpszReturnW)
402 {
403 hRet = AssocQueryStringByKeyW(cfFlags, str, hkAssoc, lpszExtraW,
404 lpszReturnW, &dwLenOut);
405
406 if (SUCCEEDED(hRet))
407 WideCharToMultiByte(CP_ACP,0,szReturnW,-1,pszOut,dwLenOut,0,0);
408 *pcchOut = dwLenOut;
409
410 if (lpszReturnW != szReturnW)
411 HeapFree(GetProcessHeap(), 0, lpszReturnW);
412 }
413 }
414
415 if (lpszExtraW != szExtraW)
416 HeapFree(GetProcessHeap(), 0, lpszExtraW);
417 return hRet;
418 }
419
420
421 /**************************************************************************
422 * AssocIsDangerous (SHLWAPI.@)
423 *
424 * Determine if a file association is dangerous (potentially malware).
425 *
426 * PARAMS
427 * lpszAssoc [I] Name of file or file extension to check.
428 *
429 * RETURNS
430 * TRUE, if lpszAssoc may potentially be malware (executable),
431 * FALSE, Otherwise.
432 */
433 BOOL WINAPI AssocIsDangerous(LPCWSTR lpszAssoc)
434 {
435 FIXME("%s\n", debugstr_w(lpszAssoc));
436 return FALSE;
437 }
438
439 /**************************************************************************
440 * IQueryAssociations_QueryInterface {SHLWAPI}
441 *
442 * See IUnknown_QueryInterface.
443 */
444 static HRESULT WINAPI IQueryAssociations_fnQueryInterface(
445 IQueryAssociations* iface,
446 REFIID riid,
447 LPVOID *ppvObj)
448 {
449 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
450
451 TRACE("(%p,%s,%p)\n",This, debugstr_guid(riid), ppvObj);
452
453 *ppvObj = NULL;
454
455 if (IsEqualIID(riid, &IID_IUnknown) ||
456 IsEqualIID(riid, &IID_IQueryAssociations))
457 {
458 *ppvObj = This;
459
460 IQueryAssociations_AddRef((IQueryAssociations*)*ppvObj);
461 TRACE("Returning IQueryAssociations (%p)\n", *ppvObj);
462 return S_OK;
463 }
464 TRACE("Returning E_NOINTERFACE\n");
465 return E_NOINTERFACE;
466 }
467
468 /**************************************************************************
469 * IQueryAssociations_AddRef {SHLWAPI}
470 *
471 * See IUnknown_AddRef.
472 */
473 static ULONG WINAPI IQueryAssociations_fnAddRef(IQueryAssociations *iface)
474 {
475 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
476 ULONG refCount = InterlockedIncrement(&This->ref);
477
478 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
479
480 return refCount;
481 }
482
483 /**************************************************************************
484 * IQueryAssociations_Release {SHLWAPI}
485 *
486 * See IUnknown_Release.
487 */
488 static ULONG WINAPI IQueryAssociations_fnRelease(IQueryAssociations *iface)
489 {
490 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
491 ULONG refCount = InterlockedDecrement(&This->ref);
492
493 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
494
495 if (!refCount)
496 {
497 TRACE("Destroying IQueryAssociations (%p)\n", This);
498 RegCloseKey(This->hkeySource);
499 RegCloseKey(This->hkeyProgID);
500 HeapFree(GetProcessHeap(), 0, This);
501 }
502
503 return refCount;
504 }
505
506 /**************************************************************************
507 * IQueryAssociations_Init {SHLWAPI}
508 *
509 * Initialise an IQueryAssociations object.
510 *
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.
517 *
518 * RETURNS
519 * Success: S_OK. iface is initialised with the parameters given.
520 * Failure: An HRESULT error code indicating the error.
521 */
522 static HRESULT WINAPI IQueryAssociations_fnInit(
523 IQueryAssociations *iface,
524 ASSOCF cfFlags,
525 LPCWSTR pszAssoc,
526 HKEY hkeyProgid,
527 HWND hWnd)
528 {
529 static const WCHAR szProgID[] = {'P','r','o','g','I','D',0};
530 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
531 LONG ret;
532
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)
543 {
544 ret = RegOpenKeyExW(HKEY_CLASSES_ROOT,
545 pszAssoc,
546 0,
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 == '{'))
553 {
554 RegOpenKeyExW(This->hkeySource,
555 szProgID,
556 0,
557 KEY_READ,
558 &This->hkeyProgID);
559 }
560 else
561 This->hkeyProgID = This->hkeySource;
562 return S_OK;
563 }
564 else if (hkeyProgid != NULL)
565 {
566 This->hkeyProgID = hkeyProgid;
567 return S_OK;
568 }
569 else
570 return E_INVALIDARG;
571 }
572
573 static HRESULT ASSOC_GetValue(HKEY hkey, WCHAR ** pszText)
574 {
575 DWORD len;
576 LONG ret;
577
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)
590 {
591 HeapFree(GetProcessHeap(), 0, *pszText);
592 return HRESULT_FROM_WIN32(ret);
593 }
594 return S_OK;
595 }
596
597 static HRESULT ASSOC_GetCommand(IQueryAssociationsImpl *This,
598 LPCWSTR pszExtra, WCHAR **ppszCommand)
599 {
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 };
610
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);
618
619 ret = RegOpenKeyExW(hkeyFile, shellW, 0, KEY_READ, &hkeyShell);
620 RegCloseKey(hkeyFile);
621 if (ret != ERROR_SUCCESS)
622 return HRESULT_FROM_WIN32(ret);
623
624 if (!pszExtra)
625 {
626 hr = ASSOC_GetValue(hkeyShell, &pszExtraFromReg);
627 /* if no default action */
628 if (hr == E_FAIL || hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
629 {
630 DWORD rlen;
631 ret = RegQueryInfoKeyW(hkeyShell, 0, 0, 0, 0, &rlen, 0, 0, 0, 0, 0, 0);
632 if (ret != ERROR_SUCCESS)
633 {
634 RegCloseKey(hkeyShell);
635 return HRESULT_FROM_WIN32(ret);
636 }
637 rlen++;
638 pszExtraFromReg = HeapAlloc(GetProcessHeap(), 0, rlen * sizeof(WCHAR));
639 if (!pszExtraFromReg)
640 {
641 RegCloseKey(hkeyShell);
642 return E_OUTOFMEMORY;
643 }
644 ret = RegEnumKeyExW(hkeyShell, 0, pszExtraFromReg, &rlen, 0, NULL, NULL, NULL);
645 if (ret != ERROR_SUCCESS)
646 {
647 RegCloseKey(hkeyShell);
648 return HRESULT_FROM_WIN32(ret);
649 }
650 }
651 else if (FAILED(hr))
652 {
653 RegCloseKey(hkeyShell);
654 return hr;
655 }
656 }
657
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);
664
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;
672 }
673
674 static HRESULT ASSOC_GetExecutable(IQueryAssociationsImpl *This,
675 LPCWSTR pszExtra, LPWSTR path,
676 DWORD pathlen, DWORD *len)
677 {
678 WCHAR *pszCommand;
679 WCHAR *pszStart;
680 WCHAR *pszEnd;
681 HRESULT hr;
682
683 assert(len);
684
685 hr = ASSOC_GetCommand(This, pszExtra, &pszCommand);
686 if (FAILED(hr))
687 return hr;
688
689 /* cleanup pszCommand */
690 if (pszCommand[0] == '"')
691 {
692 pszStart = pszCommand + 1;
693 pszEnd = strchrW(pszStart, '"');
694 }
695 else
696 {
697 pszStart = pszCommand;
698 pszEnd = strchrW(pszStart, ' ');
699 }
700 if (pszEnd)
701 *pszEnd = 0;
702
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;
708 }
709
710 static HRESULT ASSOC_ReturnData(LPWSTR out, DWORD *outlen, LPCWSTR data,
711 DWORD datalen)
712 {
713 assert(outlen);
714
715 if (out)
716 {
717 if (*outlen < datalen)
718 {
719 *outlen = datalen;
720 return E_POINTER;
721 }
722 *outlen = datalen;
723 lstrcpynW(out, data, datalen);
724 return S_OK;
725 }
726 else
727 {
728 *outlen = datalen;
729 return S_FALSE;
730 }
731 }
732
733 /**************************************************************************
734 * IQueryAssociations_GetString {SHLWAPI}
735 *
736 * Get a file association string from the registry.
737 *
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
745 *
746 * RETURNS
747 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
748 * Failure: An HRESULT error code indicating the error.
749 */
750 static HRESULT WINAPI IQueryAssociations_fnGetString(
751 IQueryAssociations *iface,
752 ASSOCF cfFlags,
753 ASSOCSTR str,
754 LPCWSTR pszExtra,
755 LPWSTR pszOut,
756 DWORD *pcchOut)
757 {
758 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
759 const ASSOCF cfUnimplemented = ~(0);
760 DWORD len = 0;
761 HRESULT hr;
762 WCHAR path[MAX_PATH];
763
764 TRACE("(%p,0x%8x,0x%8x,%s,%p,%p)\n", This, cfFlags, str,
765 debugstr_w(pszExtra), pszOut, pcchOut);
766
767 if (cfFlags & cfUnimplemented)
768 FIXME("%08x: unimplemented flags!\n", cfFlags & cfUnimplemented);
769
770 if (!pcchOut)
771 return E_UNEXPECTED;
772
773 switch (str)
774 {
775 case ASSOCSTR_COMMAND:
776 {
777 WCHAR *command;
778 hr = ASSOC_GetCommand(This, pszExtra, &command);
779 if (SUCCEEDED(hr))
780 {
781 hr = ASSOC_ReturnData(pszOut, pcchOut, command, strlenW(command) + 1);
782 HeapFree(GetProcessHeap(), 0, command);
783 }
784 return hr;
785 }
786
787 case ASSOCSTR_EXECUTABLE:
788 {
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);
794 }
795
796 case ASSOCSTR_FRIENDLYDOCNAME:
797 {
798 WCHAR *pszFileType;
799 DWORD ret;
800 DWORD size;
801
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)
808 {
809 WCHAR *docName = HeapAlloc(GetProcessHeap(), 0, size);
810 if (docName)
811 {
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);
818 }
819 else
820 hr = E_OUTOFMEMORY;
821 }
822 else
823 hr = HRESULT_FROM_WIN32(ret);
824 HeapFree(GetProcessHeap(), 0, pszFileType);
825 return hr;
826 }
827
828 case ASSOCSTR_FRIENDLYAPPNAME:
829 {
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
837 };
838 static const WCHAR fileDescFmtW[] = {
839 '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
840 '\\','%','','4','x','%','','4','x',
841 '\\','F','i','l','e','D','e','s','c','r','i','p','t','i','o','n',0
842 };
843 WCHAR fileDescW[41];
844
845 hr = ASSOC_GetExecutable(This, pszExtra, path, MAX_PATH, &len);
846 if (FAILED(hr))
847 return hr;
848
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))
858 {
859 UINT i;
860 DWORD *langCodeDesc = (DWORD *)bufW;
861 for (i = 0; i < flen / sizeof(DWORD); i++)
862 {
863 sprintfW(fileDescW, fileDescFmtW, LOWORD(langCodeDesc[i]),
864 HIWORD(langCodeDesc[i]));
865 if (VerQueryValueW(verinfoW, fileDescW, (LPVOID *)&bufW, &flen))
866 {
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);
871 }
872 }
873 }
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);
879 }
880
881 case ASSOCSTR_CONTENTTYPE:
882 {
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;
887
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)
894 {
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);
901 }
902 else
903 hr = E_OUTOFMEMORY;
904 return hr;
905 }
906
907 case ASSOCSTR_DEFAULTICON:
908 {
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;
914
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)
920 {
921 size = 0;
922 ret = RegGetValueW(hkeyFile, DefaultIconW, NULL, RRF_RT_REG_SZ, NULL, NULL, &size);
923 if (ret == ERROR_SUCCESS)
924 {
925 WCHAR *icon = HeapAlloc(GetProcessHeap(), 0, size);
926 if (icon)
927 {
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);
934 }
935 else
936 hr = E_OUTOFMEMORY;
937 }
938 else
939 hr = HRESULT_FROM_WIN32(ret);
940 RegCloseKey(hkeyFile);
941 }
942 else
943 hr = HRESULT_FROM_WIN32(ret);
944 HeapFree(GetProcessHeap(), 0, pszFileType);
945 return hr;
946 }
947
948 default:
949 FIXME("assocstr %d unimplemented!\n", str);
950 return E_NOTIMPL;
951 }
952 }
953
954 /**************************************************************************
955 * IQueryAssociations_GetKey {SHLWAPI}
956 *
957 * Get a file association key from the registry.
958 *
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
965 *
966 * RETURNS
967 * Success: S_OK. phkeyOut contains a handle to the key.
968 * Failure: An HRESULT error code indicating the error.
969 */
970 static HRESULT WINAPI IQueryAssociations_fnGetKey(
971 IQueryAssociations *iface,
972 ASSOCF cfFlags,
973 ASSOCKEY assockey,
974 LPCWSTR pszExtra,
975 HKEY *phkeyOut)
976 {
977 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
978
979 FIXME("(%p,0x%8x,0x%8x,%s,%p)-stub!\n", This, cfFlags, assockey,
980 debugstr_w(pszExtra), phkeyOut);
981 return E_NOTIMPL;
982 }
983
984 /**************************************************************************
985 * IQueryAssociations_GetData {SHLWAPI}
986 *
987 * Get the data for a file association key from the registry.
988 *
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
996 *
997 * RETURNS
998 * Success: S_OK. pszOut contains the data, pcbOut contains its length.
999 * Failure: An HRESULT error code indicating the error.
1000 */
1001 static HRESULT WINAPI IQueryAssociations_fnGetData(
1002 IQueryAssociations *iface,
1003 ASSOCF cfFlags,
1004 ASSOCDATA assocdata,
1005 LPCWSTR pszExtra,
1006 LPVOID pvOut,
1007 DWORD *pcbOut)
1008 {
1009 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
1010
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;
1014 }
1015
1016 /**************************************************************************
1017 * IQueryAssociations_GetEnum {SHLWAPI}
1018 *
1019 * Not yet implemented in native Win32.
1020 *
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.
1028 *
1029 * RETURNS
1030 * Success: S_OK.
1031 * Failure: An HRESULT error code indicating the error.
1032 *
1033 * NOTES
1034 * Presumably this function returns an enumerator object.
1035 */
1036 static HRESULT WINAPI IQueryAssociations_fnGetEnum(
1037 IQueryAssociations *iface,
1038 ASSOCF cfFlags,
1039 ASSOCENUM assocenum,
1040 LPCWSTR pszExtra,
1041 REFIID riid,
1042 LPVOID *ppvOut)
1043 {
1044 IQueryAssociationsImpl *This = (IQueryAssociationsImpl *)iface;
1045
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;
1049 }
1050
1051 static const IQueryAssociationsVtbl IQueryAssociations_vtbl =
1052 {
1053 IQueryAssociations_fnQueryInterface,
1054 IQueryAssociations_fnAddRef,
1055 IQueryAssociations_fnRelease,
1056 IQueryAssociations_fnInit,
1057 IQueryAssociations_fnGetString,
1058 IQueryAssociations_fnGetKey,
1059 IQueryAssociations_fnGetData,
1060 IQueryAssociations_fnGetEnum
1061 };
1062
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.