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
);
94 struct AssocPerceivedInfo
103 static const WCHAR unspecified_exts
[] = {
105 '.','s','e','a','r','c','h','-','m','s',0,
109 static const WCHAR image_exts
[] = {
115 '.','j','f','i','f',0,
117 '.','j','p','e','g',0,
122 '.','t','i','f','f',0,
127 static const WCHAR audio_exts
[] = {
129 '.','a','i','f','c',0,
130 '.','a','i','f','f',0,
134 '.','m','i','d','i',0,
135 #if _WIN32_WINNT > 0x602
147 static const WCHAR video_exts
[] = {
151 '.','d','v','r','-','m','s',0,
154 #if _WIN32_WINNT <= 0x602
157 '.','m','p','2','v',0,
160 '.','m','p','e','g',0,
162 '.','m','p','v','2',0,
170 static const WCHAR compressed_exts
[] = {
175 static const WCHAR document_exts
[] = {
176 #if _WIN32_WINNT >= 0x600
178 '.','h','t','m','l',0,
184 static const WCHAR system_exts
[] = {
189 static const WCHAR application_exts
[] = {
204 const WCHAR type_text
[] = {'t','e','x','t',0};
205 const WCHAR type_image
[] = {'i','m','a','g','e',0};
206 const WCHAR type_audio
[] = {'a','u','d','i','o',0};
207 const WCHAR type_video
[] = {'v','i','d','e','o',0};
208 const WCHAR type_compressed
[] = {'c','o','m','p','r','e','s','s','e','d',0};
209 const WCHAR type_document
[] = {'d','o','c','u','m','e','n','t',0};
210 const WCHAR type_system
[] = {'s','y','s','t','e','m',0};
211 const WCHAR type_application
[] = {'a','p','p','l','i','c','a','t','i','o','n',0};
213 #define HARDCODED_NATIVE_WMSDK (PERCEIVEDFLAG_HARDCODED | PERCEIVEDFLAG_NATIVESUPPORT | PERCEIVEDFLAG_WMSDK)
214 #define HARDCODED_NATIVE_GDIPLUS (PERCEIVEDFLAG_HARDCODED | PERCEIVEDFLAG_NATIVESUPPORT | PERCEIVEDFLAG_GDIPLUS)
215 #define HARDCODED_NATIVE_ZIPFLDR (PERCEIVEDFLAG_HARDCODED | PERCEIVEDFLAG_NATIVESUPPORT | PERCEIVEDFLAG_ZIPFOLDER)
216 #define SOFTCODED_NATIVESUPPORT (PERCEIVEDFLAG_SOFTCODED | PERCEIVEDFLAG_NATIVESUPPORT)
218 static const struct AssocPerceivedInfo known_types
[] = {
219 { NULL
, PERCEIVED_TYPE_UNSPECIFIED
, PERCEIVEDFLAG_HARDCODED
, PERCEIVEDFLAG_SOFTCODED
, unspecified_exts
},
220 { type_text
, PERCEIVED_TYPE_TEXT
, PERCEIVEDFLAG_HARDCODED
, SOFTCODED_NATIVESUPPORT
, NULL
},
221 { type_image
, PERCEIVED_TYPE_IMAGE
, HARDCODED_NATIVE_GDIPLUS
, PERCEIVEDFLAG_SOFTCODED
, image_exts
},
222 { type_audio
, PERCEIVED_TYPE_AUDIO
, HARDCODED_NATIVE_WMSDK
, PERCEIVEDFLAG_SOFTCODED
, audio_exts
},
223 { type_video
, PERCEIVED_TYPE_VIDEO
, HARDCODED_NATIVE_WMSDK
, PERCEIVEDFLAG_SOFTCODED
, video_exts
},
224 { type_compressed
, PERCEIVED_TYPE_COMPRESSED
, HARDCODED_NATIVE_ZIPFLDR
, PERCEIVEDFLAG_SOFTCODED
, compressed_exts
},
225 { type_document
, PERCEIVED_TYPE_DOCUMENT
, PERCEIVEDFLAG_HARDCODED
, PERCEIVEDFLAG_SOFTCODED
, document_exts
},
226 { type_system
, PERCEIVED_TYPE_SYSTEM
, PERCEIVEDFLAG_HARDCODED
, PERCEIVEDFLAG_SOFTCODED
, system_exts
},
227 { type_application
, PERCEIVED_TYPE_APPLICATION
, PERCEIVEDFLAG_HARDCODED
, PERCEIVEDFLAG_SOFTCODED
, application_exts
},
230 static const struct AssocPerceivedInfo
* AssocFindByBuiltinExtension(LPCWSTR pszExt
)
233 for (n
= 0; n
< sizeof(known_types
) / sizeof(known_types
[0]); ++n
)
235 PCWSTR Ext
= known_types
[n
].Extensions
;
238 if (!StrCmpIW(Ext
, pszExt
))
239 return &known_types
[n
];
240 Ext
+= (strlenW(Ext
) + 1);
246 static const struct AssocPerceivedInfo
* AssocFindByType(LPCWSTR pszType
)
249 for (n
= 0; n
< sizeof(known_types
) / sizeof(known_types
[0]); ++n
)
251 if (known_types
[n
].Type
)
253 if (!StrCmpIW(known_types
[n
].Type
, pszType
))
254 return &known_types
[n
];
263 /*************************************************************************
264 * AssocGetPerceivedType [SHLWAPI.@]
266 * Detect the type of a file by inspecting its extension
269 * lpszExt [I] File extension to evaluate.
270 * lpType [O] Pointer to perceived type
271 * lpFlag [O] Pointer to perceived type flag
272 * lppszType [O] Address to pointer for perceived type text
275 * Success: S_OK. lpType and lpFlag contain the perceived type and
276 * its information. If lppszType is not NULL, it will point
277 * to a string with perceived type text.
278 * Failure: An HRESULT error code indicating the error.
281 * lppszType is optional and it can be NULL.
282 * if lpType or lpFlag are NULL, the function will crash.
283 * if lpszExt is NULL, an error is returned.
290 HRESULT WINAPI
AssocGetPerceivedType(LPCWSTR lpszExt
, PERCEIVED
*lpType
,
291 INT
*lpFlag
, LPWSTR
*lppszType
)
295 FIXME("(%s, %p, %p, %p) not supported\n", debugstr_w(lpszExt
), lpType
, lpFlag
, lppszType
);
298 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
304 static const WCHAR PerceivedTypeKey
[] = {'P','e','r','c','e','i','v','e','d','T','y','p','e',0};
305 static const WCHAR SystemFileAssociationsKey
[] = {'S','y','s','t','e','m','F','i','l','e',
306 'A','s','s','o','c','i','a','t','i','o','n','s','\\','%','s',0};
307 const struct AssocPerceivedInfo
*Info
;
309 TRACE("(%s,%p,%p,%p)\n", debugstr_w(lpszExt
), lpType
, lpFlag
, lppszType
);
311 Info
= AssocFindByBuiltinExtension(lpszExt
);
314 *lpType
= Info
->Perceived
;
315 *lpFlag
= Info
->FlagHardcoded
;
319 WCHAR Buffer
[100] = { 0 };
320 DWORD Size
= sizeof(Buffer
);
321 if (RegGetValueW(HKEY_CLASSES_ROOT
, lpszExt
, PerceivedTypeKey
,
322 RRF_RT_REG_SZ
, NULL
, Buffer
, &Size
) == ERROR_SUCCESS
)
324 Info
= AssocFindByType(Buffer
);
328 WCHAR KeyName
[MAX_PATH
] = { 0 };
329 snprintfW(KeyName
, MAX_PATH
, SystemFileAssociationsKey
, lpszExt
);
330 Size
= sizeof(Buffer
);
331 if (RegGetValueW(HKEY_CLASSES_ROOT
, KeyName
, PerceivedTypeKey
,
332 RRF_RT_REG_SZ
, NULL
, Buffer
, &Size
) == ERROR_SUCCESS
)
334 Info
= AssocFindByType(Buffer
);
339 *lpType
= Info
->Perceived
;
340 *lpFlag
= Info
->FlagSoftcoded
;
346 if (lppszType
&& Info
->Type
)
348 return SHStrDupW(Info
->Type
, lppszType
);
350 return Info
->Type
? S_OK
: E_FAIL
;
354 *lpType
= PERCEIVED_TYPE_UNSPECIFIED
;
357 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
362 /*************************************************************************
363 * AssocQueryKeyW [SHLWAPI.@]
365 * See AssocQueryKeyA.
367 HRESULT WINAPI
AssocQueryKeyW(ASSOCF cfFlags
, ASSOCKEY assockey
, LPCWSTR pszAssoc
,
368 LPCWSTR pszExtra
, HKEY
*phkeyOut
)
371 IQueryAssociations
* lpAssoc
;
373 TRACE("(0x%x,%d,%s,%s,%p)\n", cfFlags
, assockey
, debugstr_w(pszAssoc
),
374 debugstr_w(pszExtra
), phkeyOut
);
376 hRet
= AssocCreate( CLSID_QueryAssociations
, &IID_IQueryAssociations
, (void **)&lpAssoc
);
377 if (FAILED(hRet
)) return hRet
;
379 cfFlags
&= SHLWAPI_DEF_ASSOCF
;
380 hRet
= IQueryAssociations_Init(lpAssoc
, cfFlags
, pszAssoc
, NULL
, NULL
);
383 hRet
= IQueryAssociations_GetKey(lpAssoc
, cfFlags
, assockey
, pszExtra
, phkeyOut
);
385 IQueryAssociations_Release(lpAssoc
);
389 /*************************************************************************
390 * AssocQueryKeyA [SHLWAPI.@]
392 * Get a file association key from the registry.
395 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
396 * assockey [I] Type of key to get
397 * pszAssoc [I] Key name to search below
398 * pszExtra [I] Extra information about the key location
399 * phkeyOut [O] Destination for the association key
402 * Success: S_OK. phkeyOut contains the key.
403 * Failure: An HRESULT error code indicating the error.
405 HRESULT WINAPI
AssocQueryKeyA(ASSOCF cfFlags
, ASSOCKEY assockey
, LPCSTR pszAssoc
,
406 LPCSTR pszExtra
, HKEY
*phkeyOut
)
408 WCHAR szAssocW
[MAX_PATH
], *lpszAssocW
= NULL
;
409 WCHAR szExtraW
[MAX_PATH
], *lpszExtraW
= NULL
;
410 HRESULT hRet
= E_OUTOFMEMORY
;
412 TRACE("(0x%x,%d,%s,%s,%p)\n", cfFlags
, assockey
, debugstr_a(pszAssoc
),
413 debugstr_a(pszExtra
), phkeyOut
);
415 if (SHLWAPI_ParamAToW(pszAssoc
, szAssocW
, MAX_PATH
, &lpszAssocW
) &&
416 SHLWAPI_ParamAToW(pszExtra
, szExtraW
, MAX_PATH
, &lpszExtraW
))
418 hRet
= AssocQueryKeyW(cfFlags
, assockey
, lpszAssocW
, lpszExtraW
, phkeyOut
);
421 if (lpszAssocW
!= szAssocW
)
422 HeapFree(GetProcessHeap(), 0, lpszAssocW
);
424 if (lpszExtraW
!= szExtraW
)
425 HeapFree(GetProcessHeap(), 0, lpszExtraW
);
430 /*************************************************************************
431 * AssocQueryStringW [SHLWAPI.@]
433 * See AssocQueryStringA.
435 HRESULT WINAPI
AssocQueryStringW(ASSOCF cfFlags
, ASSOCSTR str
, LPCWSTR pszAssoc
,
436 LPCWSTR pszExtra
, LPWSTR pszOut
, DWORD
*pcchOut
)
439 IQueryAssociations
* lpAssoc
;
441 TRACE("(0x%x,%d,%s,%s,%p,%p)\n", cfFlags
, str
, debugstr_w(pszAssoc
),
442 debugstr_w(pszExtra
), pszOut
, pcchOut
);
447 hRet
= AssocCreate( CLSID_QueryAssociations
, &IID_IQueryAssociations
, (void **)&lpAssoc
);
448 if (FAILED(hRet
)) return hRet
;
450 hRet
= IQueryAssociations_Init(lpAssoc
, cfFlags
& SHLWAPI_DEF_ASSOCF
,
451 pszAssoc
, NULL
, NULL
);
454 hRet
= IQueryAssociations_GetString(lpAssoc
, cfFlags
, str
, pszExtra
,
457 IQueryAssociations_Release(lpAssoc
);
461 /*************************************************************************
462 * AssocQueryStringA [SHLWAPI.@]
464 * Get a file association string from the registry.
467 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
468 * str [I] Type of string to get (ASSOCSTR enum from "shlwapi.h")
469 * pszAssoc [I] Key name to search below
470 * pszExtra [I] Extra information about the string location
471 * pszOut [O] Destination for the association string
472 * pcchOut [O] Length of pszOut
475 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
476 * Failure: An HRESULT error code indicating the error.
478 HRESULT WINAPI
AssocQueryStringA(ASSOCF cfFlags
, ASSOCSTR str
, LPCSTR pszAssoc
,
479 LPCSTR pszExtra
, LPSTR pszOut
, DWORD
*pcchOut
)
481 WCHAR szAssocW
[MAX_PATH
], *lpszAssocW
= NULL
;
482 WCHAR szExtraW
[MAX_PATH
], *lpszExtraW
= NULL
;
483 HRESULT hRet
= E_OUTOFMEMORY
;
485 TRACE("(0x%x,0x%d,%s,%s,%p,%p)\n", cfFlags
, str
, debugstr_a(pszAssoc
),
486 debugstr_a(pszExtra
), pszOut
, pcchOut
);
490 else if (SHLWAPI_ParamAToW(pszAssoc
, szAssocW
, MAX_PATH
, &lpszAssocW
) &&
491 SHLWAPI_ParamAToW(pszExtra
, szExtraW
, MAX_PATH
, &lpszExtraW
))
493 WCHAR szReturnW
[MAX_PATH
], *lpszReturnW
= szReturnW
;
494 DWORD dwLenOut
= *pcchOut
;
496 if (dwLenOut
>= MAX_PATH
)
497 lpszReturnW
= HeapAlloc(GetProcessHeap(), 0,
498 (dwLenOut
+ 1) * sizeof(WCHAR
));
500 dwLenOut
= sizeof(szReturnW
) / sizeof(szReturnW
[0]);
503 hRet
= E_OUTOFMEMORY
;
506 hRet
= AssocQueryStringW(cfFlags
, str
, lpszAssocW
, lpszExtraW
,
507 lpszReturnW
, &dwLenOut
);
510 dwLenOut
= WideCharToMultiByte(CP_ACP
, 0, lpszReturnW
, -1,
511 pszOut
, *pcchOut
, NULL
, NULL
);
514 if (lpszReturnW
!= szReturnW
)
515 HeapFree(GetProcessHeap(), 0, lpszReturnW
);
519 if (lpszAssocW
!= szAssocW
)
520 HeapFree(GetProcessHeap(), 0, lpszAssocW
);
521 if (lpszExtraW
!= szExtraW
)
522 HeapFree(GetProcessHeap(), 0, lpszExtraW
);
526 /*************************************************************************
527 * AssocQueryStringByKeyW [SHLWAPI.@]
529 * See AssocQueryStringByKeyA.
531 HRESULT WINAPI
AssocQueryStringByKeyW(ASSOCF cfFlags
, ASSOCSTR str
, HKEY hkAssoc
,
532 LPCWSTR pszExtra
, LPWSTR pszOut
,
536 IQueryAssociations
* lpAssoc
;
538 TRACE("(0x%x,0x%d,%p,%s,%p,%p)\n", cfFlags
, str
, hkAssoc
,
539 debugstr_w(pszExtra
), pszOut
, pcchOut
);
541 hRet
= AssocCreate( CLSID_QueryAssociations
, &IID_IQueryAssociations
, (void **)&lpAssoc
);
542 if (FAILED(hRet
)) return hRet
;
544 cfFlags
&= SHLWAPI_DEF_ASSOCF
;
545 hRet
= IQueryAssociations_Init(lpAssoc
, cfFlags
, 0, hkAssoc
, NULL
);
548 hRet
= IQueryAssociations_GetString(lpAssoc
, cfFlags
, str
, pszExtra
,
551 IQueryAssociations_Release(lpAssoc
);
555 /*************************************************************************
556 * AssocQueryStringByKeyA [SHLWAPI.@]
558 * Get a file association string from the registry, given a starting key.
561 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
562 * str [I] Type of string to get
563 * hkAssoc [I] Key to search below
564 * pszExtra [I] Extra information about the string location
565 * pszOut [O] Destination for the association string
566 * pcchOut [O] Length of pszOut
569 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
570 * Failure: An HRESULT error code indicating the error.
572 HRESULT WINAPI
AssocQueryStringByKeyA(ASSOCF cfFlags
, ASSOCSTR str
, HKEY hkAssoc
,
573 LPCSTR pszExtra
, LPSTR pszOut
,
576 WCHAR szExtraW
[MAX_PATH
], *lpszExtraW
= szExtraW
;
577 WCHAR szReturnW
[MAX_PATH
], *lpszReturnW
= szReturnW
;
578 HRESULT hRet
= E_OUTOFMEMORY
;
580 TRACE("(0x%x,0x%d,%p,%s,%p,%p)\n", cfFlags
, str
, hkAssoc
,
581 debugstr_a(pszExtra
), pszOut
, pcchOut
);
585 else if (SHLWAPI_ParamAToW(pszExtra
, szExtraW
, MAX_PATH
, &lpszExtraW
))
587 DWORD dwLenOut
= *pcchOut
;
588 if (dwLenOut
>= MAX_PATH
)
589 lpszReturnW
= HeapAlloc(GetProcessHeap(), 0,
590 (dwLenOut
+ 1) * sizeof(WCHAR
));
594 hRet
= AssocQueryStringByKeyW(cfFlags
, str
, hkAssoc
, lpszExtraW
,
595 lpszReturnW
, &dwLenOut
);
598 WideCharToMultiByte(CP_ACP
,0,szReturnW
,-1,pszOut
,dwLenOut
,0,0);
601 if (lpszReturnW
!= szReturnW
)
602 HeapFree(GetProcessHeap(), 0, lpszReturnW
);
606 if (lpszExtraW
!= szExtraW
)
607 HeapFree(GetProcessHeap(), 0, lpszExtraW
);
612 /**************************************************************************
613 * AssocIsDangerous (SHLWAPI.@)
615 * Determine if a file association is dangerous (potentially malware).
618 * lpszAssoc [I] Name of file or file extension to check.
621 * TRUE, if lpszAssoc may potentially be malware (executable),
624 BOOL WINAPI
AssocIsDangerous(LPCWSTR lpszAssoc
)
626 FIXME("%s\n", debugstr_w(lpszAssoc
));