2 * IQueryAssociations 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
23 /* Default IQueryAssociations::Init() flags */
24 #define SHLWAPI_DEF_ASSOCF (ASSOCF_INIT_BYEXENAME|ASSOCF_INIT_DEFAULTTOSTAR| \
25 ASSOCF_INIT_DEFAULTTOFOLDER)
27 /*************************************************************************
30 * Internal helper function: Convert ASCII parameter to Unicode.
32 static BOOL
SHLWAPI_ParamAToW(LPCSTR lpszParam
, LPWSTR lpszBuff
, DWORD dwLen
,
37 DWORD dwStrLen
= MultiByteToWideChar(CP_ACP
, 0, lpszParam
, -1, NULL
, 0);
41 *lpszOut
= lpszBuff
; /* Use Buffer, it is big enough */
45 /* Create a new buffer big enough for the string */
46 *lpszOut
= HeapAlloc(GetProcessHeap(), 0,
47 dwStrLen
* sizeof(WCHAR
));
51 MultiByteToWideChar(CP_ACP
, 0, lpszParam
, -1, *lpszOut
, dwStrLen
);
58 /*************************************************************************
59 * AssocCreate [SHLWAPI.@]
61 * Create a new IQueryAssociations object.
64 * clsid [I] CLSID of object
65 * refiid [I] REFIID of interface
66 * lpInterface [O] Destination for the created IQueryAssociations object
69 * Success: S_OK. lpInterface contains the new object.
70 * Failure: An HRESULT error code indicating the error.
73 * clsid must be equal to CLSID_QueryAssociations and
74 * refiid must be equal to IID_IQueryAssociations, IID_IUnknown or this function will fail
76 HRESULT WINAPI
AssocCreate(CLSID clsid
, REFIID refiid
, void **lpInterface
)
78 TRACE("(%s,%s,%p)\n", debugstr_guid(&clsid
), debugstr_guid(refiid
),
84 *(DWORD
*)lpInterface
= 0;
86 if (!IsEqualGUID(&clsid
, &CLSID_QueryAssociations
))
87 return CLASS_E_CLASSNOTAVAILABLE
;
89 return SHCoCreateInstance( NULL
, &clsid
, NULL
, refiid
, lpInterface
);
93 struct AssocPerceivedInfo
102 static const WCHAR unspecified_exts
[] = {
104 '.','s','e','a','r','c','h','-','m','s',0,
108 static const WCHAR image_exts
[] = {
114 '.','j','f','i','f',0,
116 '.','j','p','e','g',0,
121 '.','t','i','f','f',0,
126 static const WCHAR audio_exts
[] = {
128 '.','a','i','f','c',0,
129 '.','a','i','f','f',0,
133 '.','m','i','d','i',0,
134 #if _WIN32_WINNT > 0x602
146 static const WCHAR video_exts
[] = {
150 '.','d','v','r','-','m','s',0,
153 #if _WIN32_WINNT <= 0x602
156 '.','m','p','2','v',0,
159 '.','m','p','e','g',0,
161 '.','m','p','v','2',0,
169 static const WCHAR compressed_exts
[] = {
174 static const WCHAR document_exts
[] = {
175 #if _WIN32_WINNT >= 0x600
177 '.','h','t','m','l',0,
183 static const WCHAR system_exts
[] = {
188 static const WCHAR application_exts
[] = {
203 const WCHAR type_text
[] = {'t','e','x','t',0};
204 const WCHAR type_image
[] = {'i','m','a','g','e',0};
205 const WCHAR type_audio
[] = {'a','u','d','i','o',0};
206 const WCHAR type_video
[] = {'v','i','d','e','o',0};
207 const WCHAR type_compressed
[] = {'c','o','m','p','r','e','s','s','e','d',0};
208 const WCHAR type_document
[] = {'d','o','c','u','m','e','n','t',0};
209 const WCHAR type_system
[] = {'s','y','s','t','e','m',0};
210 const WCHAR type_application
[] = {'a','p','p','l','i','c','a','t','i','o','n',0};
212 #define HARDCODED_NATIVE_WMSDK (PERCEIVEDFLAG_HARDCODED | PERCEIVEDFLAG_NATIVESUPPORT | PERCEIVEDFLAG_WMSDK)
213 #define HARDCODED_NATIVE_GDIPLUS (PERCEIVEDFLAG_HARDCODED | PERCEIVEDFLAG_NATIVESUPPORT | PERCEIVEDFLAG_GDIPLUS)
214 #define HARDCODED_NATIVE_ZIPFLDR (PERCEIVEDFLAG_HARDCODED | PERCEIVEDFLAG_NATIVESUPPORT | PERCEIVEDFLAG_ZIPFOLDER)
215 #define SOFTCODED_NATIVESUPPORT (PERCEIVEDFLAG_SOFTCODED | PERCEIVEDFLAG_NATIVESUPPORT)
217 static const struct AssocPerceivedInfo known_types
[] = {
218 { NULL
, PERCEIVED_TYPE_UNSPECIFIED
, PERCEIVEDFLAG_HARDCODED
, PERCEIVEDFLAG_SOFTCODED
, unspecified_exts
},
219 { type_text
, PERCEIVED_TYPE_TEXT
, PERCEIVEDFLAG_HARDCODED
, SOFTCODED_NATIVESUPPORT
, NULL
},
220 { type_image
, PERCEIVED_TYPE_IMAGE
, HARDCODED_NATIVE_GDIPLUS
, PERCEIVEDFLAG_SOFTCODED
, image_exts
},
221 { type_audio
, PERCEIVED_TYPE_AUDIO
, HARDCODED_NATIVE_WMSDK
, PERCEIVEDFLAG_SOFTCODED
, audio_exts
},
222 { type_video
, PERCEIVED_TYPE_VIDEO
, HARDCODED_NATIVE_WMSDK
, PERCEIVEDFLAG_SOFTCODED
, video_exts
},
223 { type_compressed
, PERCEIVED_TYPE_COMPRESSED
, HARDCODED_NATIVE_ZIPFLDR
, PERCEIVEDFLAG_SOFTCODED
, compressed_exts
},
224 { type_document
, PERCEIVED_TYPE_DOCUMENT
, PERCEIVEDFLAG_HARDCODED
, PERCEIVEDFLAG_SOFTCODED
, document_exts
},
225 { type_system
, PERCEIVED_TYPE_SYSTEM
, PERCEIVEDFLAG_HARDCODED
, PERCEIVEDFLAG_SOFTCODED
, system_exts
},
226 { type_application
, PERCEIVED_TYPE_APPLICATION
, PERCEIVEDFLAG_HARDCODED
, PERCEIVEDFLAG_SOFTCODED
, application_exts
},
229 static const struct AssocPerceivedInfo
* AssocFindByBuiltinExtension(LPCWSTR pszExt
)
232 for (n
= 0; n
< sizeof(known_types
) / sizeof(known_types
[0]); ++n
)
234 PCWSTR Ext
= known_types
[n
].Extensions
;
237 if (!StrCmpIW(Ext
, pszExt
))
238 return &known_types
[n
];
239 Ext
+= (strlenW(Ext
) + 1);
245 static const struct AssocPerceivedInfo
* AssocFindByType(LPCWSTR pszType
)
248 for (n
= 0; n
< sizeof(known_types
) / sizeof(known_types
[0]); ++n
)
250 if (known_types
[n
].Type
)
252 if (!StrCmpIW(known_types
[n
].Type
, pszType
))
253 return &known_types
[n
];
260 /*************************************************************************
261 * AssocGetPerceivedType [SHLWAPI.@]
263 * Detect the type of a file by inspecting its extension
266 * lpszExt [I] File extension to evaluate.
267 * lpType [O] Pointer to perceived type
268 * lpFlag [O] Pointer to perceived type flag
269 * lppszType [O] Address to pointer for perceived type text
272 * Success: S_OK. lpType and lpFlag contain the perceived type and
273 * its information. If lppszType is not NULL, it will point
274 * to a string with perceived type text.
275 * Failure: An HRESULT error code indicating the error.
278 * lppszType is optional and it can be NULL.
279 * if lpType or lpFlag are NULL, the function will crash.
280 * if lpszExt is NULL, an error is returned.
282 HRESULT WINAPI
AssocGetPerceivedType(LPCWSTR lpszExt
, PERCEIVED
*lpType
,
283 INT
*lpFlag
, LPWSTR
*lppszType
)
285 static const WCHAR PerceivedTypeKey
[] = {'P','e','r','c','e','i','v','e','d','T','y','p','e',0};
286 static const WCHAR SystemFileAssociationsKey
[] = {'S','y','s','t','e','m','F','i','l','e',
287 'A','s','s','o','c','i','a','t','i','o','n','s','\\','%','s',0};
288 const struct AssocPerceivedInfo
*Info
;
290 TRACE("(%s,%p,%p,%p)\n", debugstr_w(lpszExt
), lpType
, lpFlag
, lppszType
);
292 Info
= AssocFindByBuiltinExtension(lpszExt
);
295 *lpType
= Info
->Perceived
;
296 *lpFlag
= Info
->FlagHardcoded
;
300 WCHAR Buffer
[100] = { 0 };
301 DWORD Size
= sizeof(Buffer
);
302 if (RegGetValueW(HKEY_CLASSES_ROOT
, lpszExt
, PerceivedTypeKey
,
303 RRF_RT_REG_SZ
, NULL
, Buffer
, &Size
) == ERROR_SUCCESS
)
305 Info
= AssocFindByType(Buffer
);
309 WCHAR KeyName
[MAX_PATH
] = { 0 };
310 snprintfW(KeyName
, MAX_PATH
, SystemFileAssociationsKey
, lpszExt
);
311 Size
= sizeof(Buffer
);
312 if (RegGetValueW(HKEY_CLASSES_ROOT
, KeyName
, PerceivedTypeKey
,
313 RRF_RT_REG_SZ
, NULL
, Buffer
, &Size
) == ERROR_SUCCESS
)
315 Info
= AssocFindByType(Buffer
);
320 *lpType
= Info
->Perceived
;
321 *lpFlag
= Info
->FlagSoftcoded
;
327 if (lppszType
&& Info
->Type
)
329 return SHStrDupW(Info
->Type
, lppszType
);
331 return Info
->Type
? S_OK
: E_FAIL
;
335 *lpType
= PERCEIVED_TYPE_UNSPECIFIED
;
338 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
341 /*************************************************************************
342 * AssocQueryKeyW [SHLWAPI.@]
344 * See AssocQueryKeyA.
346 HRESULT WINAPI
AssocQueryKeyW(ASSOCF cfFlags
, ASSOCKEY assockey
, LPCWSTR pszAssoc
,
347 LPCWSTR pszExtra
, HKEY
*phkeyOut
)
350 IQueryAssociations
* lpAssoc
;
352 TRACE("(0x%x,%d,%s,%s,%p)\n", cfFlags
, assockey
, debugstr_w(pszAssoc
),
353 debugstr_w(pszExtra
), phkeyOut
);
355 hRet
= AssocCreate( CLSID_QueryAssociations
, &IID_IQueryAssociations
, (void **)&lpAssoc
);
356 if (FAILED(hRet
)) return hRet
;
358 cfFlags
&= SHLWAPI_DEF_ASSOCF
;
359 hRet
= IQueryAssociations_Init(lpAssoc
, cfFlags
, pszAssoc
, NULL
, NULL
);
362 hRet
= IQueryAssociations_GetKey(lpAssoc
, cfFlags
, assockey
, pszExtra
, phkeyOut
);
364 IQueryAssociations_Release(lpAssoc
);
368 /*************************************************************************
369 * AssocQueryKeyA [SHLWAPI.@]
371 * Get a file association key from the registry.
374 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
375 * assockey [I] Type of key to get
376 * pszAssoc [I] Key name to search below
377 * pszExtra [I] Extra information about the key location
378 * phkeyOut [O] Destination for the association key
381 * Success: S_OK. phkeyOut contains the key.
382 * Failure: An HRESULT error code indicating the error.
384 HRESULT WINAPI
AssocQueryKeyA(ASSOCF cfFlags
, ASSOCKEY assockey
, LPCSTR pszAssoc
,
385 LPCSTR pszExtra
, HKEY
*phkeyOut
)
387 WCHAR szAssocW
[MAX_PATH
], *lpszAssocW
= NULL
;
388 WCHAR szExtraW
[MAX_PATH
], *lpszExtraW
= NULL
;
389 HRESULT hRet
= E_OUTOFMEMORY
;
391 TRACE("(0x%x,%d,%s,%s,%p)\n", cfFlags
, assockey
, debugstr_a(pszAssoc
),
392 debugstr_a(pszExtra
), phkeyOut
);
394 if (SHLWAPI_ParamAToW(pszAssoc
, szAssocW
, MAX_PATH
, &lpszAssocW
) &&
395 SHLWAPI_ParamAToW(pszExtra
, szExtraW
, MAX_PATH
, &lpszExtraW
))
397 hRet
= AssocQueryKeyW(cfFlags
, assockey
, lpszAssocW
, lpszExtraW
, phkeyOut
);
400 if (lpszAssocW
!= szAssocW
)
401 HeapFree(GetProcessHeap(), 0, lpszAssocW
);
403 if (lpszExtraW
!= szExtraW
)
404 HeapFree(GetProcessHeap(), 0, lpszExtraW
);
409 /*************************************************************************
410 * AssocQueryStringW [SHLWAPI.@]
412 * See AssocQueryStringA.
414 HRESULT WINAPI
AssocQueryStringW(ASSOCF cfFlags
, ASSOCSTR str
, LPCWSTR pszAssoc
,
415 LPCWSTR pszExtra
, LPWSTR pszOut
, DWORD
*pcchOut
)
418 IQueryAssociations
* lpAssoc
;
420 TRACE("(0x%x,%d,%s,%s,%p,%p)\n", cfFlags
, str
, debugstr_w(pszAssoc
),
421 debugstr_w(pszExtra
), pszOut
, pcchOut
);
426 hRet
= AssocCreate( CLSID_QueryAssociations
, &IID_IQueryAssociations
, (void **)&lpAssoc
);
427 if (FAILED(hRet
)) return hRet
;
429 hRet
= IQueryAssociations_Init(lpAssoc
, cfFlags
& SHLWAPI_DEF_ASSOCF
,
430 pszAssoc
, NULL
, NULL
);
433 hRet
= IQueryAssociations_GetString(lpAssoc
, cfFlags
, str
, pszExtra
,
436 IQueryAssociations_Release(lpAssoc
);
440 /*************************************************************************
441 * AssocQueryStringA [SHLWAPI.@]
443 * Get a file association string from the registry.
446 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
447 * str [I] Type of string to get (ASSOCSTR enum from "shlwapi.h")
448 * pszAssoc [I] Key name to search below
449 * pszExtra [I] Extra information about the string location
450 * pszOut [O] Destination for the association string
451 * pcchOut [O] Length of pszOut
454 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
455 * Failure: An HRESULT error code indicating the error.
457 HRESULT WINAPI
AssocQueryStringA(ASSOCF cfFlags
, ASSOCSTR str
, LPCSTR pszAssoc
,
458 LPCSTR pszExtra
, LPSTR pszOut
, DWORD
*pcchOut
)
460 WCHAR szAssocW
[MAX_PATH
], *lpszAssocW
= NULL
;
461 WCHAR szExtraW
[MAX_PATH
], *lpszExtraW
= NULL
;
462 HRESULT hRet
= E_OUTOFMEMORY
;
464 TRACE("(0x%x,0x%d,%s,%s,%p,%p)\n", cfFlags
, str
, debugstr_a(pszAssoc
),
465 debugstr_a(pszExtra
), pszOut
, pcchOut
);
469 else if (SHLWAPI_ParamAToW(pszAssoc
, szAssocW
, MAX_PATH
, &lpszAssocW
) &&
470 SHLWAPI_ParamAToW(pszExtra
, szExtraW
, MAX_PATH
, &lpszExtraW
))
472 WCHAR szReturnW
[MAX_PATH
], *lpszReturnW
= szReturnW
;
473 DWORD dwLenOut
= *pcchOut
;
475 if (dwLenOut
>= MAX_PATH
)
476 lpszReturnW
= HeapAlloc(GetProcessHeap(), 0,
477 (dwLenOut
+ 1) * sizeof(WCHAR
));
479 dwLenOut
= sizeof(szReturnW
) / sizeof(szReturnW
[0]);
482 hRet
= E_OUTOFMEMORY
;
485 hRet
= AssocQueryStringW(cfFlags
, str
, lpszAssocW
, lpszExtraW
,
486 lpszReturnW
, &dwLenOut
);
489 dwLenOut
= WideCharToMultiByte(CP_ACP
, 0, lpszReturnW
, -1,
490 pszOut
, *pcchOut
, NULL
, NULL
);
493 if (lpszReturnW
!= szReturnW
)
494 HeapFree(GetProcessHeap(), 0, lpszReturnW
);
498 if (lpszAssocW
!= szAssocW
)
499 HeapFree(GetProcessHeap(), 0, lpszAssocW
);
500 if (lpszExtraW
!= szExtraW
)
501 HeapFree(GetProcessHeap(), 0, lpszExtraW
);
505 /*************************************************************************
506 * AssocQueryStringByKeyW [SHLWAPI.@]
508 * See AssocQueryStringByKeyA.
510 HRESULT WINAPI
AssocQueryStringByKeyW(ASSOCF cfFlags
, ASSOCSTR str
, HKEY hkAssoc
,
511 LPCWSTR pszExtra
, LPWSTR pszOut
,
515 IQueryAssociations
* lpAssoc
;
517 TRACE("(0x%x,0x%d,%p,%s,%p,%p)\n", cfFlags
, str
, hkAssoc
,
518 debugstr_w(pszExtra
), pszOut
, pcchOut
);
520 hRet
= AssocCreate( CLSID_QueryAssociations
, &IID_IQueryAssociations
, (void **)&lpAssoc
);
521 if (FAILED(hRet
)) return hRet
;
523 cfFlags
&= SHLWAPI_DEF_ASSOCF
;
524 hRet
= IQueryAssociations_Init(lpAssoc
, cfFlags
, 0, hkAssoc
, NULL
);
527 hRet
= IQueryAssociations_GetString(lpAssoc
, cfFlags
, str
, pszExtra
,
530 IQueryAssociations_Release(lpAssoc
);
534 /*************************************************************************
535 * AssocQueryStringByKeyA [SHLWAPI.@]
537 * Get a file association string from the registry, given a starting key.
540 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
541 * str [I] Type of string to get
542 * hkAssoc [I] Key to search below
543 * pszExtra [I] Extra information about the string location
544 * pszOut [O] Destination for the association string
545 * pcchOut [O] Length of pszOut
548 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
549 * Failure: An HRESULT error code indicating the error.
551 HRESULT WINAPI
AssocQueryStringByKeyA(ASSOCF cfFlags
, ASSOCSTR str
, HKEY hkAssoc
,
552 LPCSTR pszExtra
, LPSTR pszOut
,
555 WCHAR szExtraW
[MAX_PATH
], *lpszExtraW
= szExtraW
;
556 WCHAR szReturnW
[MAX_PATH
], *lpszReturnW
= szReturnW
;
557 HRESULT hRet
= E_OUTOFMEMORY
;
559 TRACE("(0x%x,0x%d,%p,%s,%p,%p)\n", cfFlags
, str
, hkAssoc
,
560 debugstr_a(pszExtra
), pszOut
, pcchOut
);
564 else if (SHLWAPI_ParamAToW(pszExtra
, szExtraW
, MAX_PATH
, &lpszExtraW
))
566 DWORD dwLenOut
= *pcchOut
;
567 if (dwLenOut
>= MAX_PATH
)
568 lpszReturnW
= HeapAlloc(GetProcessHeap(), 0,
569 (dwLenOut
+ 1) * sizeof(WCHAR
));
573 hRet
= AssocQueryStringByKeyW(cfFlags
, str
, hkAssoc
, lpszExtraW
,
574 lpszReturnW
, &dwLenOut
);
577 WideCharToMultiByte(CP_ACP
,0,szReturnW
,-1,pszOut
,dwLenOut
,0,0);
580 if (lpszReturnW
!= szReturnW
)
581 HeapFree(GetProcessHeap(), 0, lpszReturnW
);
585 if (lpszExtraW
!= szExtraW
)
586 HeapFree(GetProcessHeap(), 0, lpszExtraW
);
591 /**************************************************************************
592 * AssocIsDangerous (SHLWAPI.@)
594 * Determine if a file association is dangerous (potentially malware).
597 * lpszAssoc [I] Name of file or file extension to check.
600 * TRUE, if lpszAssoc may potentially be malware (executable),
603 BOOL WINAPI
AssocIsDangerous(LPCWSTR lpszAssoc
)
605 FIXME("%s\n", debugstr_w(lpszAssoc
));