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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
33 /**************************************************************************
34 * IQueryAssociations {SHLWAPI}
37 * This object provides a layer of abstraction over the system registry in
38 * order to simplify the process of parsing associations between files.
39 * Associations in this context means the registry entries that link (for
40 * example) the extension of a file with its description, list of
41 * applications to open the file with, and actions that can be performed on it
42 * (the shell displays such information in the context menu of explorer
43 * when you right-click on a file).
46 * You can use this object tranparently by calling the helper functions
47 * AssocQueryKeyA(), AssocQueryStringA() and AssocQueryStringByKeyA(). These
48 * create an IQueryAssociations object, perform the requested actions
49 * and then dispose of the object. Alternatively, you can create an instance
50 * of the object using AssocCreate() and call the following methods on it:
55 /* Default IQueryAssociations::Init() flags */
56 #define SHLWAPI_DEF_ASSOCF (ASSOCF_INIT_BYEXENAME|ASSOCF_INIT_DEFAULTTOSTAR| \
57 ASSOCF_INIT_DEFAULTTOFOLDER)
61 IQueryAssociationsVtbl
*lpVtbl
;
65 } IQueryAssociationsImpl
;
67 static struct IQueryAssociationsVtbl IQueryAssociations_vtbl
;
69 /**************************************************************************
70 * IQueryAssociations_Constructor [internal]
72 * Construct a new IQueryAssociations object.
74 static IQueryAssociations
* IQueryAssociations_Constructor(void)
76 IQueryAssociationsImpl
* iface
;
78 iface
= HeapAlloc(GetProcessHeap(),0,sizeof(IQueryAssociationsImpl
));
79 iface
->lpVtbl
= &IQueryAssociations_vtbl
;
81 iface
->hkeySource
= NULL
;
82 iface
->hkeyProgID
= NULL
;
84 TRACE("Returning IQueryAssociations* %p\n", iface
);
85 return (IQueryAssociations
*)iface
;
88 /*************************************************************************
91 * Internal helper function: Convert ASCII parameter to Unicode.
93 static BOOL
SHLWAPI_ParamAToW(LPCSTR lpszParam
, LPWSTR lpszBuff
, DWORD dwLen
,
98 DWORD dwStrLen
= MultiByteToWideChar(CP_ACP
, 0, lpszParam
, -1, NULL
, 0);
100 if (dwStrLen
< dwLen
)
102 *lpszOut
= lpszBuff
; /* Use Buffer, it is big enough */
106 /* Create a new buffer big enough for the string */
107 *lpszOut
= HeapAlloc(GetProcessHeap(), 0,
108 dwStrLen
* sizeof(WCHAR
));
112 MultiByteToWideChar(CP_ACP
, 0, lpszParam
, -1, *lpszOut
, dwStrLen
);
119 /*************************************************************************
120 * AssocCreate [SHLWAPI.@]
122 * Create a new IQueryAssociations object.
125 * clsid [I] CLSID of object
126 * refiid [I] REFIID of interface
127 * lpInterface [O] Destination for the created IQueryAssociations object
130 * Success: S_OK. lpInterface contains the new object.
131 * Failure: An HRESULT error code indicating the error.
134 * refiid must be equal to IID_IQueryAssociations, or this function will fail.
136 HRESULT WINAPI
AssocCreate(CLSID clsid
, REFIID refiid
, void **lpInterface
)
139 IQueryAssociations
* lpAssoc
;
141 TRACE("(%s,%s,%p)\n", debugstr_guid(&clsid
), debugstr_guid(refiid
),
147 *(DWORD
*)lpInterface
= 0;
149 if (!IsEqualGUID(&clsid
, &IID_IQueryAssociations
))
152 lpAssoc
= IQueryAssociations_Constructor();
155 return E_OUTOFMEMORY
;
157 hRet
= IQueryAssociations_QueryInterface(lpAssoc
, refiid
, lpInterface
);
158 IQueryAssociations_Release(lpAssoc
);
162 /*************************************************************************
163 * AssocQueryKeyW [SHLWAPI.@]
165 * See AssocQueryKeyA.
167 HRESULT WINAPI
AssocQueryKeyW(ASSOCF cfFlags
, ASSOCKEY assockey
, LPCWSTR pszAssoc
,
168 LPCWSTR pszExtra
, HKEY
*phkeyOut
)
171 IQueryAssociations
* lpAssoc
;
173 TRACE("(0x%8lx,0x%8x,%s,%s,%p)\n", cfFlags
, assockey
, debugstr_w(pszAssoc
),
174 debugstr_w(pszExtra
), phkeyOut
);
176 lpAssoc
= IQueryAssociations_Constructor();
179 return E_OUTOFMEMORY
;
181 cfFlags
&= SHLWAPI_DEF_ASSOCF
;
182 hRet
= IQueryAssociations_Init(lpAssoc
, cfFlags
, pszAssoc
, NULL
, NULL
);
185 hRet
= IQueryAssociations_GetKey(lpAssoc
, cfFlags
, assockey
, pszExtra
, phkeyOut
);
187 IQueryAssociations_Release(lpAssoc
);
191 /*************************************************************************
192 * AssocQueryKeyA [SHLWAPI.@]
194 * Get a file association key from the registry.
197 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
198 * assockey [I] Type of key to get
199 * pszAssoc [I] Key name to search below
200 * pszExtra [I] Extra information about the key location
201 * phkeyOut [O] Destination for the association key
204 * Success: S_OK. phkeyOut contains the key.
205 * Failure: An HRESULT error code indicating the error.
207 HRESULT WINAPI
AssocQueryKeyA(ASSOCF cfFlags
, ASSOCKEY assockey
, LPCSTR pszAssoc
,
208 LPCSTR pszExtra
, HKEY
*phkeyOut
)
210 WCHAR szAssocW
[MAX_PATH
], *lpszAssocW
= NULL
;
211 WCHAR szExtraW
[MAX_PATH
], *lpszExtraW
= NULL
;
212 HRESULT hRet
= E_OUTOFMEMORY
;
214 TRACE("(0x%8lx,0x%8x,%s,%s,%p)\n", cfFlags
, assockey
, debugstr_a(pszAssoc
),
215 debugstr_a(pszExtra
), phkeyOut
);
217 if (SHLWAPI_ParamAToW(pszAssoc
, szAssocW
, MAX_PATH
, &lpszAssocW
) &&
218 SHLWAPI_ParamAToW(pszExtra
, szExtraW
, MAX_PATH
, &lpszExtraW
))
220 hRet
= AssocQueryKeyW(cfFlags
, assockey
, lpszAssocW
, lpszExtraW
, phkeyOut
);
223 if (lpszAssocW
&& lpszAssocW
!= szAssocW
)
224 HeapFree(GetProcessHeap(), 0, lpszAssocW
);
226 if (lpszExtraW
&& lpszExtraW
!= szExtraW
)
227 HeapFree(GetProcessHeap(), 0, lpszExtraW
);
232 /*************************************************************************
233 * AssocQueryStringW [SHLWAPI.@]
235 * See AssocQueryStringA.
237 HRESULT WINAPI
AssocQueryStringW(ASSOCF cfFlags
, ASSOCSTR str
, LPCWSTR pszAssoc
,
238 LPCWSTR pszExtra
, LPWSTR pszOut
, DWORD
*pcchOut
)
241 IQueryAssociations
* lpAssoc
;
243 TRACE("(0x%8lx,0x%8x,%s,%s,%p,%p)\n", cfFlags
, str
, debugstr_w(pszAssoc
),
244 debugstr_w(pszExtra
), pszOut
, pcchOut
);
249 lpAssoc
= IQueryAssociations_Constructor();
252 return E_OUTOFMEMORY
;
254 hRet
= IQueryAssociations_Init(lpAssoc
, cfFlags
& SHLWAPI_DEF_ASSOCF
,
255 pszAssoc
, NULL
, NULL
);
258 hRet
= IQueryAssociations_GetString(lpAssoc
, cfFlags
, str
, pszExtra
,
261 IQueryAssociations_Release(lpAssoc
);
265 /*************************************************************************
266 * AssocQueryStringA [SHLWAPI.@]
268 * Get a file association string from the registry.
271 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
272 * str [I] Type of string to get (ASSOCSTR enum from "shlwapi.h")
273 * pszAssoc [I] Key name to search below
274 * pszExtra [I] Extra information about the string location
275 * pszOut [O] Destination for the association string
276 * pcchOut [O] Length of pszOut
279 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
280 * Failure: An HRESULT error code indicating the error.
282 HRESULT WINAPI
AssocQueryStringA(ASSOCF cfFlags
, ASSOCSTR str
, LPCSTR pszAssoc
,
283 LPCSTR pszExtra
, LPSTR pszOut
, DWORD
*pcchOut
)
285 WCHAR szAssocW
[MAX_PATH
], *lpszAssocW
= NULL
;
286 WCHAR szExtraW
[MAX_PATH
], *lpszExtraW
= NULL
;
287 HRESULT hRet
= E_OUTOFMEMORY
;
289 TRACE("(0x%8lx,0x%8x,%s,%s,%p,%p)\n", cfFlags
, str
, debugstr_a(pszAssoc
),
290 debugstr_a(pszExtra
), pszOut
, pcchOut
);
294 else if (SHLWAPI_ParamAToW(pszAssoc
, szAssocW
, MAX_PATH
, &lpszAssocW
) &&
295 SHLWAPI_ParamAToW(pszExtra
, szExtraW
, MAX_PATH
, &lpszExtraW
))
297 WCHAR szReturnW
[MAX_PATH
], *lpszReturnW
= szReturnW
;
298 DWORD dwLenOut
= *pcchOut
;
300 if (dwLenOut
>= MAX_PATH
)
301 lpszReturnW
= HeapAlloc(GetProcessHeap(), 0,
302 (dwLenOut
+ 1) * sizeof(WCHAR
));
305 hRet
= E_OUTOFMEMORY
;
308 hRet
= AssocQueryStringW(cfFlags
, str
, lpszAssocW
, lpszExtraW
,
309 lpszReturnW
, &dwLenOut
);
312 WideCharToMultiByte(CP_ACP
,0,szReturnW
,-1,pszOut
,dwLenOut
,0,0);
315 if (lpszReturnW
&& lpszReturnW
!= szReturnW
)
316 HeapFree(GetProcessHeap(), 0, lpszReturnW
);
320 if (lpszAssocW
&& lpszAssocW
!= szAssocW
)
321 HeapFree(GetProcessHeap(), 0, lpszAssocW
);
322 if (lpszExtraW
&& lpszExtraW
!= szExtraW
)
323 HeapFree(GetProcessHeap(), 0, lpszExtraW
);
327 /*************************************************************************
328 * AssocQueryStringByKeyW [SHLWAPI.@]
330 * See AssocQueryStringByKeyA.
332 HRESULT WINAPI
AssocQueryStringByKeyW(ASSOCF cfFlags
, ASSOCSTR str
, HKEY hkAssoc
,
333 LPCWSTR pszExtra
, LPWSTR pszOut
,
337 IQueryAssociations
* lpAssoc
;
339 TRACE("(0x%8lx,0x%8x,%p,%s,%p,%p)\n", cfFlags
, str
, hkAssoc
,
340 debugstr_w(pszExtra
), pszOut
, pcchOut
);
342 lpAssoc
= IQueryAssociations_Constructor();
345 return E_OUTOFMEMORY
;
347 cfFlags
&= SHLWAPI_DEF_ASSOCF
;
348 hRet
= IQueryAssociations_Init(lpAssoc
, cfFlags
, 0, hkAssoc
, NULL
);
351 hRet
= IQueryAssociations_GetString(lpAssoc
, cfFlags
, str
, pszExtra
,
354 IQueryAssociations_Release(lpAssoc
);
358 /*************************************************************************
359 * AssocQueryStringByKeyA [SHLWAPI.@]
361 * Get a file association string from the registry, given a starting key.
364 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
365 * str [I] Type of string to get
366 * hkAssoc [I] Key to search below
367 * pszExtra [I] Extra information about the string location
368 * pszOut [O] Destination for the association string
369 * pcchOut [O] Length of pszOut
372 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
373 * Failure: An HRESULT error code indicating the error.
375 HRESULT WINAPI
AssocQueryStringByKeyA(ASSOCF cfFlags
, ASSOCSTR str
, HKEY hkAssoc
,
376 LPCSTR pszExtra
, LPSTR pszOut
,
379 WCHAR szExtraW
[MAX_PATH
], *lpszExtraW
;
380 WCHAR szReturnW
[MAX_PATH
], *lpszReturnW
= szReturnW
;
381 HRESULT hRet
= E_OUTOFMEMORY
;
383 TRACE("(0x%8lx,0x%8x,%p,%s,%p,%p)\n", cfFlags
, str
, hkAssoc
,
384 debugstr_a(pszExtra
), pszOut
, pcchOut
);
388 else if (SHLWAPI_ParamAToW(pszExtra
, szExtraW
, MAX_PATH
, &lpszExtraW
))
390 DWORD dwLenOut
= *pcchOut
;
391 if (dwLenOut
>= MAX_PATH
)
392 lpszReturnW
= HeapAlloc(GetProcessHeap(), 0,
393 (dwLenOut
+ 1) * sizeof(WCHAR
));
397 hRet
= AssocQueryStringByKeyW(cfFlags
, str
, hkAssoc
, lpszExtraW
,
398 lpszReturnW
, &dwLenOut
);
401 WideCharToMultiByte(CP_ACP
,0,szReturnW
,-1,pszOut
,dwLenOut
,0,0);
404 if (lpszReturnW
!= szReturnW
)
405 HeapFree(GetProcessHeap(), 0, lpszReturnW
);
409 if (lpszExtraW
&& lpszExtraW
!= szExtraW
)
410 HeapFree(GetProcessHeap(), 0, lpszExtraW
);
415 /**************************************************************************
416 * AssocIsDangerous (SHLWAPI.@)
418 * Determine if a file association is dangerous (potentially malware).
421 * lpszAssoc [I] Name of file or file extension to check.
424 * TRUE, if lpszAssoc may potentially be malware (executable),
427 BOOL WINAPI
AssocIsDangerous(LPCWSTR lpszAssoc
)
429 FIXME("%s\n", debugstr_w(lpszAssoc
));
433 /**************************************************************************
434 * IQueryAssociations_QueryInterface {SHLWAPI}
436 * See IUnknown_QueryInterface.
438 static HRESULT WINAPI
IQueryAssociations_fnQueryInterface(
439 IQueryAssociations
* iface
,
443 IQueryAssociationsImpl
*This
= (IQueryAssociationsImpl
*)iface
;
445 TRACE("(%p,%s,%p)\n",This
, debugstr_guid(riid
), ppvObj
);
449 if (IsEqualIID(riid
, &IID_IUnknown
) ||
450 IsEqualIID(riid
, &IID_IQueryAssociations
))
452 *ppvObj
= (IQueryAssociations
*)This
;
454 IQueryAssociations_AddRef((IQueryAssociations
*)*ppvObj
);
455 TRACE("Returning IQueryAssociations (%p)\n", *ppvObj
);
458 TRACE("Returning E_NOINTERFACE\n");
459 return E_NOINTERFACE
;
462 /**************************************************************************
463 * IQueryAssociations_AddRef {SHLWAPI}
465 * See IUnknown_AddRef.
467 static ULONG WINAPI
IQueryAssociations_fnAddRef(IQueryAssociations
*iface
)
469 IQueryAssociationsImpl
*This
= (IQueryAssociationsImpl
*)iface
;
471 TRACE("(%p)->(ref before=%lu)\n",This
, This
->ref
);
473 return InterlockedIncrement(&This
->ref
);
476 /**************************************************************************
477 * IQueryAssociations_Release {SHLWAPI}
479 * See IUnknown_Release.
481 static ULONG WINAPI
IQueryAssociations_fnRelease(IQueryAssociations
*iface
)
483 IQueryAssociationsImpl
*This
= (IQueryAssociationsImpl
*)iface
;
486 TRACE("(%p)->(ref before=%lu)\n",This
, This
->ref
);
488 if (!(ulRet
= InterlockedDecrement(&This
->ref
)))
490 TRACE("Destroying IQueryAssociations (%p)\n", This
);
491 HeapFree(GetProcessHeap(), 0, This
);
496 /**************************************************************************
497 * IQueryAssociations_Init {SHLWAPI}
499 * Initialise an IQueryAssociations object.
502 * iface [I] IQueryAssociations interface to initialise
503 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
504 * pszAssoc [I] String for the root key name, or NULL if hkProgid is given
505 * hkeyProgid [I] Handle for the root key, or NULL if pszAssoc is given
506 * hWnd [I] Reserved, must be NULL.
509 * Success: S_OK. iface is initialised with the parameters given.
510 * Failure: An HRESULT error code indicating the error.
512 static HRESULT WINAPI
IQueryAssociations_fnInit(
513 IQueryAssociations
*iface
,
519 static const WCHAR szProgID
[] = {'P','r','o','g','I','D',0};
520 IQueryAssociationsImpl
*This
= (IQueryAssociationsImpl
*)iface
;
523 TRACE("(%p)->(%ld,%s,%p,%p)\n", iface
,
525 debugstr_w(pszAssoc
),
529 FIXME("hwnd != NULL not supported\n");
531 FIXME("unsupported flags: %lx\n", cfFlags
);
532 if (pszAssoc
!= NULL
)
534 hr
= RegOpenKeyExW(HKEY_CLASSES_ROOT
,
540 return HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION
);
541 /* if this is not a prog id */
542 if ((*pszAssoc
== '.') || (*pszAssoc
== '{'))
544 hr
= RegOpenKeyExW(This
->hkeySource
,
550 FIXME("Don't know what to return\n");
553 This
->hkeyProgID
= This
->hkeySource
;
556 else if (hkeyProgid
!= NULL
)
558 This
->hkeyProgID
= hkeyProgid
;
565 /**************************************************************************
566 * IQueryAssociations_GetString {SHLWAPI}
568 * Get a file association string from the registry.
571 * iface [I] IQueryAssociations interface to query
572 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
573 * str [I] Type of string to get (ASSOCSTR enum from "shlwapi.h")
574 * pszExtra [I] Extra information about the string location
575 * pszOut [O] Destination for the association string
576 * pcchOut [I/O] Length of pszOut
579 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
580 * Failure: An HRESULT error code indicating the error.
582 static HRESULT WINAPI
IQueryAssociations_fnGetString(
583 IQueryAssociations
*iface
,
590 IQueryAssociationsImpl
*This
= (IQueryAssociationsImpl
*)iface
;
592 FIXME("(%p,0x%8lx,0x%8x,%s,%p,%p)-stub!\n", This
, cfFlags
, str
,
593 debugstr_w(pszExtra
), pszOut
, pcchOut
);
597 /**************************************************************************
598 * IQueryAssociations_GetKey {SHLWAPI}
600 * Get a file association key from the registry.
603 * iface [I] IQueryAssociations interface to query
604 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
605 * assockey [I] Type of key to get (ASSOCKEY enum from "shlwapi.h")
606 * pszExtra [I] Extra information about the key location
607 * phkeyOut [O] Destination for the association key
610 * Success: S_OK. phkeyOut contains a handle to the key.
611 * Failure: An HRESULT error code indicating the error.
613 static HRESULT WINAPI
IQueryAssociations_fnGetKey(
614 IQueryAssociations
*iface
,
620 IQueryAssociationsImpl
*This
= (IQueryAssociationsImpl
*)iface
;
622 FIXME("(%p,0x%8lx,0x%8x,%s,%p)-stub!\n", This
, cfFlags
, assockey
,
623 debugstr_w(pszExtra
), phkeyOut
);
627 /**************************************************************************
628 * IQueryAssociations_GetData {SHLWAPI}
630 * Get the data for a file association key from the registry.
633 * iface [I] IQueryAssociations interface to query
634 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
635 * assocdata [I] Type of data to get (ASSOCDATA enum from "shlwapi.h")
636 * pszExtra [I] Extra information about the data location
637 * pvOut [O] Destination for the association key
638 * pcbOut [I/O] Size of pvOut
641 * Success: S_OK. pszOut contains the data, pcbOut contains its length.
642 * Failure: An HRESULT error code indicating the error.
644 static HRESULT WINAPI
IQueryAssociations_fnGetData(
645 IQueryAssociations
*iface
,
652 IQueryAssociationsImpl
*This
= (IQueryAssociationsImpl
*)iface
;
654 FIXME("(%p,0x%8lx,0x%8x,%s,%p,%p)-stub!\n", This
, cfFlags
, assocdata
,
655 debugstr_w(pszExtra
), pvOut
, pcbOut
);
659 /**************************************************************************
660 * IQueryAssociations_GetEnum {SHLWAPI}
662 * Not yet implemented in native Win32.
665 * iface [I] IQueryAssociations interface to query
666 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
667 * assocenum [I] Type of enum to get (ASSOCENUM enum from "shlwapi.h")
668 * pszExtra [I] Extra information about the enum location
669 * riid [I] REFIID to look for
670 * ppvOut [O] Destination for the interface.
674 * Failure: An HRESULT error code indicating the error.
677 * Presumably this function returns an enumerator object.
679 static HRESULT WINAPI
IQueryAssociations_fnGetEnum(
680 IQueryAssociations
*iface
,
687 IQueryAssociationsImpl
*This
= (IQueryAssociationsImpl
*)iface
;
689 FIXME("(%p,0x%8lx,0x%8x,%s,%s,%p)-stub!\n", This
, cfFlags
, assocenum
,
690 debugstr_w(pszExtra
), debugstr_guid(riid
), ppvOut
);
694 static struct IQueryAssociationsVtbl IQueryAssociations_vtbl
=
696 IQueryAssociations_fnQueryInterface
,
697 IQueryAssociations_fnAddRef
,
698 IQueryAssociations_fnRelease
,
699 IQueryAssociations_fnInit
,
700 IQueryAssociations_fnGetString
,
701 IQueryAssociations_fnGetKey
,
702 IQueryAssociations_fnGetData
,
703 IQueryAssociations_fnGetEnum