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
31 #include "wine/unicode.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(shell);
36 /* Default IQueryAssociations::Init() flags */
37 #define SHLWAPI_DEF_ASSOCF (ASSOCF_INIT_BYEXENAME|ASSOCF_INIT_DEFAULTTOSTAR| \
38 ASSOCF_INIT_DEFAULTTOFOLDER)
40 /*************************************************************************
43 * Internal helper function: Convert ASCII parameter to Unicode.
45 static BOOL SHLWAPI_ParamAToW(LPCSTR lpszParam, LPWSTR lpszBuff, DWORD dwLen,
50 DWORD dwStrLen = MultiByteToWideChar(CP_ACP, 0, lpszParam, -1, NULL, 0);
54 *lpszOut = lpszBuff; /* Use Buffer, it is big enough */
58 /* Create a new buffer big enough for the string */
59 *lpszOut = HeapAlloc(GetProcessHeap(), 0,
60 dwStrLen * sizeof(WCHAR));
64 MultiByteToWideChar(CP_ACP, 0, lpszParam, -1, *lpszOut, dwStrLen);
71 /*************************************************************************
72 * AssocCreate [SHLWAPI.@]
74 * Create a new IQueryAssociations object.
77 * clsid [I] CLSID of object
78 * refiid [I] REFIID of interface
79 * lpInterface [O] Destination for the created IQueryAssociations object
82 * Success: S_OK. lpInterface contains the new object.
83 * Failure: An HRESULT error code indicating the error.
86 * clsid must be equal to CLSID_QueryAssociations and
87 * refiid must be equal to IID_IQueryAssociations, IID_IUnknown or this function will fail
89 HRESULT WINAPI AssocCreate(CLSID clsid, REFIID refiid, void **lpInterface)
91 TRACE("(%s,%s,%p)\n", debugstr_guid(&clsid), debugstr_guid(refiid),
97 *(DWORD*)lpInterface = 0;
99 if (!IsEqualGUID(&clsid, &CLSID_QueryAssociations))
100 return CLASS_E_CLASSNOTAVAILABLE;
102 return SHCoCreateInstance( NULL, &clsid, NULL, refiid, lpInterface );
105 /*************************************************************************
106 * AssocGetPerceivedType [SHLWAPI.@]
108 * Detect the type of a file by inspecting its extension
111 * lpszExt [I] File extension to evaluate.
112 * lpType [O] Pointer to perceived type
113 * lpFlag [O] Pointer to perceived type flag
114 * lppszType [O] Address to pointer for perceived type text
117 * Success: S_OK. lpType and lpFlag contain the perceived type and
118 * its information. If lppszType is not NULL, it will point
119 * to a string with perceived type text.
120 * Failure: An HRESULT error code indicating the error.
123 * lppszType is optional and it can be NULL.
124 * if lpType or lpFlag are NULL, the function will crash.
125 * if lpszExt is NULL, an error is returned.
130 HRESULT WINAPI AssocGetPerceivedType(LPCWSTR lpszExt, PERCEIVED *lpType,
131 INT *lpFlag, LPWSTR *lppszType)
133 FIXME("(%s, %p, %p, %p) not supported\n", debugstr_w(lpszExt), lpType, lpFlag, lppszType);
136 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
141 /*************************************************************************
142 * AssocQueryKeyW [SHLWAPI.@]
144 * See AssocQueryKeyA.
146 HRESULT WINAPI AssocQueryKeyW(ASSOCF cfFlags, ASSOCKEY assockey, LPCWSTR pszAssoc,
147 LPCWSTR pszExtra, HKEY *phkeyOut)
150 IQueryAssociations* lpAssoc;
152 TRACE("(0x%x,%d,%s,%s,%p)\n", cfFlags, assockey, debugstr_w(pszAssoc),
153 debugstr_w(pszExtra), phkeyOut);
155 hRet = AssocCreate( CLSID_QueryAssociations, &IID_IQueryAssociations, (void **)&lpAssoc );
156 if (FAILED(hRet)) return hRet;
158 cfFlags &= SHLWAPI_DEF_ASSOCF;
159 hRet = IQueryAssociations_Init(lpAssoc, cfFlags, pszAssoc, NULL, NULL);
162 hRet = IQueryAssociations_GetKey(lpAssoc, cfFlags, assockey, pszExtra, phkeyOut);
164 IQueryAssociations_Release(lpAssoc);
168 /*************************************************************************
169 * AssocQueryKeyA [SHLWAPI.@]
171 * Get a file association key from the registry.
174 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
175 * assockey [I] Type of key to get
176 * pszAssoc [I] Key name to search below
177 * pszExtra [I] Extra information about the key location
178 * phkeyOut [O] Destination for the association key
181 * Success: S_OK. phkeyOut contains the key.
182 * Failure: An HRESULT error code indicating the error.
184 HRESULT WINAPI AssocQueryKeyA(ASSOCF cfFlags, ASSOCKEY assockey, LPCSTR pszAssoc,
185 LPCSTR pszExtra, HKEY *phkeyOut)
187 WCHAR szAssocW[MAX_PATH], *lpszAssocW = NULL;
188 WCHAR szExtraW[MAX_PATH], *lpszExtraW = NULL;
189 HRESULT hRet = E_OUTOFMEMORY;
191 TRACE("(0x%x,%d,%s,%s,%p)\n", cfFlags, assockey, debugstr_a(pszAssoc),
192 debugstr_a(pszExtra), phkeyOut);
194 if (SHLWAPI_ParamAToW(pszAssoc, szAssocW, MAX_PATH, &lpszAssocW) &&
195 SHLWAPI_ParamAToW(pszExtra, szExtraW, MAX_PATH, &lpszExtraW))
197 hRet = AssocQueryKeyW(cfFlags, assockey, lpszAssocW, lpszExtraW, phkeyOut);
200 if (lpszAssocW != szAssocW)
201 HeapFree(GetProcessHeap(), 0, lpszAssocW);
203 if (lpszExtraW != szExtraW)
204 HeapFree(GetProcessHeap(), 0, lpszExtraW);
209 /*************************************************************************
210 * AssocQueryStringW [SHLWAPI.@]
212 * See AssocQueryStringA.
214 HRESULT WINAPI AssocQueryStringW(ASSOCF cfFlags, ASSOCSTR str, LPCWSTR pszAssoc,
215 LPCWSTR pszExtra, LPWSTR pszOut, DWORD *pcchOut)
218 IQueryAssociations* lpAssoc;
220 TRACE("(0x%x,%d,%s,%s,%p,%p)\n", cfFlags, str, debugstr_w(pszAssoc),
221 debugstr_w(pszExtra), pszOut, pcchOut);
226 hRet = AssocCreate( CLSID_QueryAssociations, &IID_IQueryAssociations, (void **)&lpAssoc );
227 if (FAILED(hRet)) return hRet;
229 hRet = IQueryAssociations_Init(lpAssoc, cfFlags & SHLWAPI_DEF_ASSOCF,
230 pszAssoc, NULL, NULL);
233 hRet = IQueryAssociations_GetString(lpAssoc, cfFlags, str, pszExtra,
236 IQueryAssociations_Release(lpAssoc);
240 /*************************************************************************
241 * AssocQueryStringA [SHLWAPI.@]
243 * Get a file association string from the registry.
246 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
247 * str [I] Type of string to get (ASSOCSTR enum from "shlwapi.h")
248 * pszAssoc [I] Key name to search below
249 * pszExtra [I] Extra information about the string location
250 * pszOut [O] Destination for the association string
251 * pcchOut [O] Length of pszOut
254 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
255 * Failure: An HRESULT error code indicating the error.
257 HRESULT WINAPI AssocQueryStringA(ASSOCF cfFlags, ASSOCSTR str, LPCSTR pszAssoc,
258 LPCSTR pszExtra, LPSTR pszOut, DWORD *pcchOut)
260 WCHAR szAssocW[MAX_PATH], *lpszAssocW = NULL;
261 WCHAR szExtraW[MAX_PATH], *lpszExtraW = NULL;
262 HRESULT hRet = E_OUTOFMEMORY;
264 TRACE("(0x%x,0x%d,%s,%s,%p,%p)\n", cfFlags, str, debugstr_a(pszAssoc),
265 debugstr_a(pszExtra), pszOut, pcchOut);
269 else if (SHLWAPI_ParamAToW(pszAssoc, szAssocW, MAX_PATH, &lpszAssocW) &&
270 SHLWAPI_ParamAToW(pszExtra, szExtraW, MAX_PATH, &lpszExtraW))
272 WCHAR szReturnW[MAX_PATH], *lpszReturnW = szReturnW;
273 DWORD dwLenOut = *pcchOut;
275 if (dwLenOut >= MAX_PATH)
276 lpszReturnW = HeapAlloc(GetProcessHeap(), 0,
277 (dwLenOut + 1) * sizeof(WCHAR));
279 dwLenOut = sizeof(szReturnW) / sizeof(szReturnW[0]);
282 hRet = E_OUTOFMEMORY;
285 hRet = AssocQueryStringW(cfFlags, str, lpszAssocW, lpszExtraW,
286 lpszReturnW, &dwLenOut);
289 dwLenOut = WideCharToMultiByte(CP_ACP, 0, lpszReturnW, -1,
290 pszOut, *pcchOut, NULL, NULL);
293 if (lpszReturnW != szReturnW)
294 HeapFree(GetProcessHeap(), 0, lpszReturnW);
298 if (lpszAssocW != szAssocW)
299 HeapFree(GetProcessHeap(), 0, lpszAssocW);
300 if (lpszExtraW != szExtraW)
301 HeapFree(GetProcessHeap(), 0, lpszExtraW);
305 /*************************************************************************
306 * AssocQueryStringByKeyW [SHLWAPI.@]
308 * See AssocQueryStringByKeyA.
310 HRESULT WINAPI AssocQueryStringByKeyW(ASSOCF cfFlags, ASSOCSTR str, HKEY hkAssoc,
311 LPCWSTR pszExtra, LPWSTR pszOut,
315 IQueryAssociations* lpAssoc;
317 TRACE("(0x%x,0x%d,%p,%s,%p,%p)\n", cfFlags, str, hkAssoc,
318 debugstr_w(pszExtra), pszOut, pcchOut);
320 hRet = AssocCreate( CLSID_QueryAssociations, &IID_IQueryAssociations, (void **)&lpAssoc );
321 if (FAILED(hRet)) return hRet;
323 cfFlags &= SHLWAPI_DEF_ASSOCF;
324 hRet = IQueryAssociations_Init(lpAssoc, cfFlags, 0, hkAssoc, NULL);
327 hRet = IQueryAssociations_GetString(lpAssoc, cfFlags, str, pszExtra,
330 IQueryAssociations_Release(lpAssoc);
334 /*************************************************************************
335 * AssocQueryStringByKeyA [SHLWAPI.@]
337 * Get a file association string from the registry, given a starting key.
340 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
341 * str [I] Type of string to get
342 * hkAssoc [I] Key to search below
343 * pszExtra [I] Extra information about the string location
344 * pszOut [O] Destination for the association string
345 * pcchOut [O] Length of pszOut
348 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
349 * Failure: An HRESULT error code indicating the error.
351 HRESULT WINAPI AssocQueryStringByKeyA(ASSOCF cfFlags, ASSOCSTR str, HKEY hkAssoc,
352 LPCSTR pszExtra, LPSTR pszOut,
355 WCHAR szExtraW[MAX_PATH], *lpszExtraW = szExtraW;
356 WCHAR szReturnW[MAX_PATH], *lpszReturnW = szReturnW;
357 HRESULT hRet = E_OUTOFMEMORY;
359 TRACE("(0x%x,0x%d,%p,%s,%p,%p)\n", cfFlags, str, hkAssoc,
360 debugstr_a(pszExtra), pszOut, pcchOut);
364 else if (SHLWAPI_ParamAToW(pszExtra, szExtraW, MAX_PATH, &lpszExtraW))
366 DWORD dwLenOut = *pcchOut;
367 if (dwLenOut >= MAX_PATH)
368 lpszReturnW = HeapAlloc(GetProcessHeap(), 0,
369 (dwLenOut + 1) * sizeof(WCHAR));
373 hRet = AssocQueryStringByKeyW(cfFlags, str, hkAssoc, lpszExtraW,
374 lpszReturnW, &dwLenOut);
377 WideCharToMultiByte(CP_ACP,0,szReturnW,-1,pszOut,dwLenOut,0,0);
380 if (lpszReturnW != szReturnW)
381 HeapFree(GetProcessHeap(), 0, lpszReturnW);
385 if (lpszExtraW != szExtraW)
386 HeapFree(GetProcessHeap(), 0, lpszExtraW);
391 /**************************************************************************
392 * AssocIsDangerous (SHLWAPI.@)
394 * Determine if a file association is dangerous (potentially malware).
397 * lpszAssoc [I] Name of file or file extension to check.
400 * TRUE, if lpszAssoc may potentially be malware (executable),
403 BOOL WINAPI AssocIsDangerous(LPCWSTR lpszAssoc)
405 FIXME("%s\n", debugstr_w(lpszAssoc));