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
);
92 /*************************************************************************
93 * AssocGetPerceivedType [SHLWAPI.@]
95 * Detect the type of a file by inspecting its extension
98 * lpszExt [I] File extension to evaluate.
99 * lpType [O] Pointer to perceived type
100 * lpFlag [O] Pointer to perceived type flag
101 * lppszType [O] Address to pointer for perceived type text
104 * Success: S_OK. lpType and lpFlag contain the perceived type and
105 * its information. If lppszType is not NULL, it will point
106 * to a string with perceived type text.
107 * Failure: An HRESULT error code indicating the error.
110 * lppszType is optional and it can be NULL.
111 * if lpType or lpFlag are NULL, the function will crash.
112 * if lpszExt is NULL, an error is returned.
117 HRESULT WINAPI
AssocGetPerceivedType(LPCWSTR lpszExt
, PERCEIVED
*lpType
,
118 INT
*lpFlag
, LPWSTR
*lppszType
)
120 FIXME("(%s, %p, %p, %p) not supported\n", debugstr_w(lpszExt
), lpType
, lpFlag
, lppszType
);
123 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
128 /*************************************************************************
129 * AssocQueryKeyW [SHLWAPI.@]
131 * See AssocQueryKeyA.
133 HRESULT WINAPI
AssocQueryKeyW(ASSOCF cfFlags
, ASSOCKEY assockey
, LPCWSTR pszAssoc
,
134 LPCWSTR pszExtra
, HKEY
*phkeyOut
)
137 IQueryAssociations
* lpAssoc
;
139 TRACE("(0x%x,%d,%s,%s,%p)\n", cfFlags
, assockey
, debugstr_w(pszAssoc
),
140 debugstr_w(pszExtra
), phkeyOut
);
142 hRet
= AssocCreate( CLSID_QueryAssociations
, &IID_IQueryAssociations
, (void **)&lpAssoc
);
143 if (FAILED(hRet
)) return hRet
;
145 cfFlags
&= SHLWAPI_DEF_ASSOCF
;
146 hRet
= IQueryAssociations_Init(lpAssoc
, cfFlags
, pszAssoc
, NULL
, NULL
);
149 hRet
= IQueryAssociations_GetKey(lpAssoc
, cfFlags
, assockey
, pszExtra
, phkeyOut
);
151 IQueryAssociations_Release(lpAssoc
);
155 /*************************************************************************
156 * AssocQueryKeyA [SHLWAPI.@]
158 * Get a file association key from the registry.
161 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
162 * assockey [I] Type of key to get
163 * pszAssoc [I] Key name to search below
164 * pszExtra [I] Extra information about the key location
165 * phkeyOut [O] Destination for the association key
168 * Success: S_OK. phkeyOut contains the key.
169 * Failure: An HRESULT error code indicating the error.
171 HRESULT WINAPI
AssocQueryKeyA(ASSOCF cfFlags
, ASSOCKEY assockey
, LPCSTR pszAssoc
,
172 LPCSTR pszExtra
, HKEY
*phkeyOut
)
174 WCHAR szAssocW
[MAX_PATH
], *lpszAssocW
= NULL
;
175 WCHAR szExtraW
[MAX_PATH
], *lpszExtraW
= NULL
;
176 HRESULT hRet
= E_OUTOFMEMORY
;
178 TRACE("(0x%x,%d,%s,%s,%p)\n", cfFlags
, assockey
, debugstr_a(pszAssoc
),
179 debugstr_a(pszExtra
), phkeyOut
);
181 if (SHLWAPI_ParamAToW(pszAssoc
, szAssocW
, MAX_PATH
, &lpszAssocW
) &&
182 SHLWAPI_ParamAToW(pszExtra
, szExtraW
, MAX_PATH
, &lpszExtraW
))
184 hRet
= AssocQueryKeyW(cfFlags
, assockey
, lpszAssocW
, lpszExtraW
, phkeyOut
);
187 if (lpszAssocW
!= szAssocW
)
188 HeapFree(GetProcessHeap(), 0, lpszAssocW
);
190 if (lpszExtraW
!= szExtraW
)
191 HeapFree(GetProcessHeap(), 0, lpszExtraW
);
196 /*************************************************************************
197 * AssocQueryStringW [SHLWAPI.@]
199 * See AssocQueryStringA.
201 HRESULT WINAPI
AssocQueryStringW(ASSOCF cfFlags
, ASSOCSTR str
, LPCWSTR pszAssoc
,
202 LPCWSTR pszExtra
, LPWSTR pszOut
, DWORD
*pcchOut
)
205 IQueryAssociations
* lpAssoc
;
207 TRACE("(0x%x,%d,%s,%s,%p,%p)\n", cfFlags
, str
, debugstr_w(pszAssoc
),
208 debugstr_w(pszExtra
), pszOut
, pcchOut
);
213 hRet
= AssocCreate( CLSID_QueryAssociations
, &IID_IQueryAssociations
, (void **)&lpAssoc
);
214 if (FAILED(hRet
)) return hRet
;
216 hRet
= IQueryAssociations_Init(lpAssoc
, cfFlags
& SHLWAPI_DEF_ASSOCF
,
217 pszAssoc
, NULL
, NULL
);
220 hRet
= IQueryAssociations_GetString(lpAssoc
, cfFlags
, str
, pszExtra
,
223 IQueryAssociations_Release(lpAssoc
);
227 /*************************************************************************
228 * AssocQueryStringA [SHLWAPI.@]
230 * Get a file association string from the registry.
233 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
234 * str [I] Type of string to get (ASSOCSTR enum from "shlwapi.h")
235 * pszAssoc [I] Key name to search below
236 * pszExtra [I] Extra information about the string location
237 * pszOut [O] Destination for the association string
238 * pcchOut [O] Length of pszOut
241 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
242 * Failure: An HRESULT error code indicating the error.
244 HRESULT WINAPI
AssocQueryStringA(ASSOCF cfFlags
, ASSOCSTR str
, LPCSTR pszAssoc
,
245 LPCSTR pszExtra
, LPSTR pszOut
, DWORD
*pcchOut
)
247 WCHAR szAssocW
[MAX_PATH
], *lpszAssocW
= NULL
;
248 WCHAR szExtraW
[MAX_PATH
], *lpszExtraW
= NULL
;
249 HRESULT hRet
= E_OUTOFMEMORY
;
251 TRACE("(0x%x,0x%d,%s,%s,%p,%p)\n", cfFlags
, str
, debugstr_a(pszAssoc
),
252 debugstr_a(pszExtra
), pszOut
, pcchOut
);
256 else if (SHLWAPI_ParamAToW(pszAssoc
, szAssocW
, MAX_PATH
, &lpszAssocW
) &&
257 SHLWAPI_ParamAToW(pszExtra
, szExtraW
, MAX_PATH
, &lpszExtraW
))
259 WCHAR szReturnW
[MAX_PATH
], *lpszReturnW
= szReturnW
;
260 DWORD dwLenOut
= *pcchOut
;
262 if (dwLenOut
>= MAX_PATH
)
263 lpszReturnW
= HeapAlloc(GetProcessHeap(), 0,
264 (dwLenOut
+ 1) * sizeof(WCHAR
));
266 dwLenOut
= sizeof(szReturnW
) / sizeof(szReturnW
[0]);
269 hRet
= E_OUTOFMEMORY
;
272 hRet
= AssocQueryStringW(cfFlags
, str
, lpszAssocW
, lpszExtraW
,
273 lpszReturnW
, &dwLenOut
);
276 dwLenOut
= WideCharToMultiByte(CP_ACP
, 0, lpszReturnW
, -1,
277 pszOut
, *pcchOut
, NULL
, NULL
);
280 if (lpszReturnW
!= szReturnW
)
281 HeapFree(GetProcessHeap(), 0, lpszReturnW
);
285 if (lpszAssocW
!= szAssocW
)
286 HeapFree(GetProcessHeap(), 0, lpszAssocW
);
287 if (lpszExtraW
!= szExtraW
)
288 HeapFree(GetProcessHeap(), 0, lpszExtraW
);
292 /*************************************************************************
293 * AssocQueryStringByKeyW [SHLWAPI.@]
295 * See AssocQueryStringByKeyA.
297 HRESULT WINAPI
AssocQueryStringByKeyW(ASSOCF cfFlags
, ASSOCSTR str
, HKEY hkAssoc
,
298 LPCWSTR pszExtra
, LPWSTR pszOut
,
302 IQueryAssociations
* lpAssoc
;
304 TRACE("(0x%x,0x%d,%p,%s,%p,%p)\n", cfFlags
, str
, hkAssoc
,
305 debugstr_w(pszExtra
), pszOut
, pcchOut
);
307 hRet
= AssocCreate( CLSID_QueryAssociations
, &IID_IQueryAssociations
, (void **)&lpAssoc
);
308 if (FAILED(hRet
)) return hRet
;
310 cfFlags
&= SHLWAPI_DEF_ASSOCF
;
311 hRet
= IQueryAssociations_Init(lpAssoc
, cfFlags
, 0, hkAssoc
, NULL
);
314 hRet
= IQueryAssociations_GetString(lpAssoc
, cfFlags
, str
, pszExtra
,
317 IQueryAssociations_Release(lpAssoc
);
321 /*************************************************************************
322 * AssocQueryStringByKeyA [SHLWAPI.@]
324 * Get a file association string from the registry, given a starting key.
327 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
328 * str [I] Type of string to get
329 * hkAssoc [I] Key to search below
330 * pszExtra [I] Extra information about the string location
331 * pszOut [O] Destination for the association string
332 * pcchOut [O] Length of pszOut
335 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
336 * Failure: An HRESULT error code indicating the error.
338 HRESULT WINAPI
AssocQueryStringByKeyA(ASSOCF cfFlags
, ASSOCSTR str
, HKEY hkAssoc
,
339 LPCSTR pszExtra
, LPSTR pszOut
,
342 WCHAR szExtraW
[MAX_PATH
], *lpszExtraW
= szExtraW
;
343 WCHAR szReturnW
[MAX_PATH
], *lpszReturnW
= szReturnW
;
344 HRESULT hRet
= E_OUTOFMEMORY
;
346 TRACE("(0x%x,0x%d,%p,%s,%p,%p)\n", cfFlags
, str
, hkAssoc
,
347 debugstr_a(pszExtra
), pszOut
, pcchOut
);
351 else if (SHLWAPI_ParamAToW(pszExtra
, szExtraW
, MAX_PATH
, &lpszExtraW
))
353 DWORD dwLenOut
= *pcchOut
;
354 if (dwLenOut
>= MAX_PATH
)
355 lpszReturnW
= HeapAlloc(GetProcessHeap(), 0,
356 (dwLenOut
+ 1) * sizeof(WCHAR
));
360 hRet
= AssocQueryStringByKeyW(cfFlags
, str
, hkAssoc
, lpszExtraW
,
361 lpszReturnW
, &dwLenOut
);
364 WideCharToMultiByte(CP_ACP
,0,szReturnW
,-1,pszOut
,dwLenOut
,0,0);
367 if (lpszReturnW
!= szReturnW
)
368 HeapFree(GetProcessHeap(), 0, lpszReturnW
);
372 if (lpszExtraW
!= szExtraW
)
373 HeapFree(GetProcessHeap(), 0, lpszExtraW
);
378 /**************************************************************************
379 * AssocIsDangerous (SHLWAPI.@)
381 * Determine if a file association is dangerous (potentially malware).
384 * lpszAssoc [I] Name of file or file extension to check.
387 * TRUE, if lpszAssoc may potentially be malware (executable),
390 BOOL WINAPI
AssocIsDangerous(LPCWSTR lpszAssoc
)
392 FIXME("%s\n", debugstr_w(lpszAssoc
));