Release 9.12.
[wine.git] / dlls / shlwapi / assoc.c
blobb2afe52ec0e7050fe12af892d81101fdc9f82f0d
1 /*
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
20 #include <stdarg.h>
21 #include <assert.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winnls.h"
26 #include "winreg.h"
27 #include "objbase.h"
28 #include "shlguid.h"
29 #include "shlobj.h"
30 #include "shlwapi.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(shell);
35 /* Default IQueryAssociations::Init() flags */
36 #define SHLWAPI_DEF_ASSOCF (ASSOCF_INIT_BYEXENAME|ASSOCF_INIT_DEFAULTTOSTAR| \
37 ASSOCF_INIT_DEFAULTTOFOLDER)
39 /*************************************************************************
40 * SHLWAPI_ParamAToW
42 * Internal helper function: Convert ANSI parameter to Unicode.
44 static BOOL SHLWAPI_ParamAToW(LPCSTR lpszParam, LPWSTR lpszBuff, DWORD dwLen,
45 LPWSTR* lpszOut)
47 if (lpszParam)
49 DWORD dwStrLen = MultiByteToWideChar(CP_ACP, 0, lpszParam, -1, NULL, 0);
51 if (dwStrLen < dwLen)
53 *lpszOut = lpszBuff; /* Use Buffer, it is big enough */
55 else
57 /* Create a new buffer big enough for the string */
58 *lpszOut = malloc(dwStrLen * sizeof(WCHAR));
59 if (!*lpszOut)
60 return FALSE;
62 MultiByteToWideChar(CP_ACP, 0, lpszParam, -1, *lpszOut, dwStrLen);
64 else
65 *lpszOut = NULL;
66 return TRUE;
69 /*************************************************************************
70 * AssocCreate [SHLWAPI.@]
72 * Create a new IQueryAssociations object.
74 * PARAMS
75 * clsid [I] CLSID of object
76 * refiid [I] REFIID of interface
77 * lpInterface [O] Destination for the created IQueryAssociations object
79 * RETURNS
80 * Success: S_OK. lpInterface contains the new object.
81 * Failure: An HRESULT error code indicating the error.
83 * NOTES
84 * clsid must be equal to CLSID_QueryAssociations and
85 * refiid must be equal to IID_IQueryAssociations, IID_IUnknown or this function will fail
87 HRESULT WINAPI AssocCreate(CLSID clsid, REFIID refiid, void **lpInterface)
89 TRACE("(%s,%s,%p)\n", debugstr_guid(&clsid), debugstr_guid(refiid),
90 lpInterface);
92 if (!lpInterface)
93 return E_INVALIDARG;
95 *(DWORD*)lpInterface = 0;
97 if (!IsEqualGUID(&clsid, &CLSID_QueryAssociations))
98 return CLASS_E_CLASSNOTAVAILABLE;
100 return SHCoCreateInstance( NULL, &clsid, NULL, refiid, lpInterface );
103 /*************************************************************************
104 * AssocGetPerceivedType [SHLWAPI.@]
106 * Detect the type of a file by inspecting its extension
108 * PARAMS
109 * lpszExt [I] File extension to evaluate.
110 * lpType [O] Pointer to perceived type
111 * lpFlag [O] Pointer to perceived type flag
112 * lppszType [O] Address to pointer for perceived type text
114 * RETURNS
115 * Success: S_OK. lpType and lpFlag contain the perceived type and
116 * its information. If lppszType is not NULL, it will point
117 * to a string with perceived type text.
118 * Failure: An HRESULT error code indicating the error.
120 * NOTES
121 * lppszType is optional and it can be NULL.
122 * if lpType or lpFlag are NULL, the function will crash.
123 * if lpszExt is NULL, an error is returned.
125 * BUGS
126 * Unimplemented.
128 HRESULT WINAPI AssocGetPerceivedType(LPCWSTR lpszExt, PERCEIVED *lpType,
129 INT *lpFlag, LPWSTR *lppszType)
131 FIXME("(%s, %p, %p, %p) not supported\n", debugstr_w(lpszExt), lpType, lpFlag, lppszType);
133 if (lpszExt == NULL)
134 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
136 return E_NOTIMPL;
139 /*************************************************************************
140 * AssocQueryKeyW [SHLWAPI.@]
142 * See AssocQueryKeyA.
144 HRESULT WINAPI AssocQueryKeyW(ASSOCF cfFlags, ASSOCKEY assockey, LPCWSTR pszAssoc,
145 LPCWSTR pszExtra, HKEY *phkeyOut)
147 HRESULT hRet;
148 IQueryAssociations* lpAssoc;
150 TRACE("(0x%lx,%d,%s,%s,%p)\n", cfFlags, assockey, debugstr_w(pszAssoc),
151 debugstr_w(pszExtra), phkeyOut);
153 hRet = AssocCreate( CLSID_QueryAssociations, &IID_IQueryAssociations, (void **)&lpAssoc );
154 if (FAILED(hRet)) return hRet;
156 cfFlags &= SHLWAPI_DEF_ASSOCF;
157 hRet = IQueryAssociations_Init(lpAssoc, cfFlags, pszAssoc, NULL, NULL);
159 if (SUCCEEDED(hRet))
160 hRet = IQueryAssociations_GetKey(lpAssoc, cfFlags, assockey, pszExtra, phkeyOut);
162 IQueryAssociations_Release(lpAssoc);
163 return hRet;
166 /*************************************************************************
167 * AssocQueryKeyA [SHLWAPI.@]
169 * Get a file association key from the registry.
171 * PARAMS
172 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
173 * assockey [I] Type of key to get
174 * pszAssoc [I] Key name to search below
175 * pszExtra [I] Extra information about the key location
176 * phkeyOut [O] Destination for the association key
178 * RETURNS
179 * Success: S_OK. phkeyOut contains the key.
180 * Failure: An HRESULT error code indicating the error.
182 HRESULT WINAPI AssocQueryKeyA(ASSOCF cfFlags, ASSOCKEY assockey, LPCSTR pszAssoc,
183 LPCSTR pszExtra, HKEY *phkeyOut)
185 WCHAR szAssocW[MAX_PATH], *lpszAssocW = NULL;
186 WCHAR szExtraW[MAX_PATH], *lpszExtraW = NULL;
187 HRESULT hRet = E_OUTOFMEMORY;
189 TRACE("(0x%lx,%d,%s,%s,%p)\n", cfFlags, assockey, debugstr_a(pszAssoc),
190 debugstr_a(pszExtra), phkeyOut);
192 if (SHLWAPI_ParamAToW(pszAssoc, szAssocW, MAX_PATH, &lpszAssocW) &&
193 SHLWAPI_ParamAToW(pszExtra, szExtraW, MAX_PATH, &lpszExtraW))
195 hRet = AssocQueryKeyW(cfFlags, assockey, lpszAssocW, lpszExtraW, phkeyOut);
198 if (lpszAssocW != szAssocW)
199 free(lpszAssocW);
201 if (lpszExtraW != szExtraW)
202 free(lpszExtraW);
204 return hRet;
207 /*************************************************************************
208 * AssocQueryStringW [SHLWAPI.@]
210 * See AssocQueryStringA.
212 HRESULT WINAPI AssocQueryStringW(ASSOCF cfFlags, ASSOCSTR str, LPCWSTR pszAssoc,
213 LPCWSTR pszExtra, LPWSTR pszOut, DWORD *pcchOut)
215 HRESULT hRet;
216 IQueryAssociations* lpAssoc;
218 TRACE("(0x%lx,%d,%s,%s,%p,%p)\n", cfFlags, str, debugstr_w(pszAssoc),
219 debugstr_w(pszExtra), pszOut, pcchOut);
221 if (!pcchOut)
222 return E_UNEXPECTED;
224 hRet = AssocCreate( CLSID_QueryAssociations, &IID_IQueryAssociations, (void **)&lpAssoc );
225 if (FAILED(hRet)) return hRet;
227 hRet = IQueryAssociations_Init(lpAssoc, cfFlags & SHLWAPI_DEF_ASSOCF,
228 pszAssoc, NULL, NULL);
230 if (SUCCEEDED(hRet))
231 hRet = IQueryAssociations_GetString(lpAssoc, cfFlags, str, pszExtra,
232 pszOut, pcchOut);
234 IQueryAssociations_Release(lpAssoc);
235 return hRet;
238 /*************************************************************************
239 * AssocQueryStringA [SHLWAPI.@]
241 * Get a file association string from the registry.
243 * PARAMS
244 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
245 * str [I] Type of string to get (ASSOCSTR enum from "shlwapi.h")
246 * pszAssoc [I] Key name to search below
247 * pszExtra [I] Extra information about the string location
248 * pszOut [O] Destination for the association string
249 * pcchOut [O] Length of pszOut
251 * RETURNS
252 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
253 * Failure: An HRESULT error code indicating the error.
255 HRESULT WINAPI AssocQueryStringA(ASSOCF cfFlags, ASSOCSTR str, LPCSTR pszAssoc,
256 LPCSTR pszExtra, LPSTR pszOut, DWORD *pcchOut)
258 WCHAR szAssocW[MAX_PATH], *lpszAssocW = NULL;
259 WCHAR szExtraW[MAX_PATH], *lpszExtraW = NULL;
260 HRESULT hRet = E_OUTOFMEMORY;
262 TRACE("(0x%lx,0x%d,%s,%s,%p,%p)\n", cfFlags, str, debugstr_a(pszAssoc),
263 debugstr_a(pszExtra), pszOut, pcchOut);
265 if (!pcchOut)
266 hRet = E_UNEXPECTED;
267 else if (SHLWAPI_ParamAToW(pszAssoc, szAssocW, MAX_PATH, &lpszAssocW) &&
268 SHLWAPI_ParamAToW(pszExtra, szExtraW, MAX_PATH, &lpszExtraW))
270 WCHAR szReturnW[MAX_PATH], *lpszReturnW = szReturnW;
271 DWORD dwLenOut = *pcchOut;
273 if (dwLenOut >= MAX_PATH)
274 lpszReturnW = malloc((dwLenOut + 1) * sizeof(WCHAR));
275 else
276 dwLenOut = ARRAY_SIZE(szReturnW);
278 if (!lpszReturnW)
279 hRet = E_OUTOFMEMORY;
280 else
282 hRet = AssocQueryStringW(cfFlags, str, lpszAssocW, lpszExtraW,
283 lpszReturnW, &dwLenOut);
285 if (SUCCEEDED(hRet))
286 dwLenOut = WideCharToMultiByte(CP_ACP, 0, lpszReturnW, -1,
287 pszOut, *pcchOut, NULL, NULL);
289 *pcchOut = dwLenOut;
290 if (lpszReturnW != szReturnW)
291 free(lpszReturnW);
295 if (lpszAssocW != szAssocW)
296 free(lpszAssocW);
297 if (lpszExtraW != szExtraW)
298 free(lpszExtraW);
299 return hRet;
302 /*************************************************************************
303 * AssocQueryStringByKeyW [SHLWAPI.@]
305 * See AssocQueryStringByKeyA.
307 HRESULT WINAPI AssocQueryStringByKeyW(ASSOCF cfFlags, ASSOCSTR str, HKEY hkAssoc,
308 LPCWSTR pszExtra, LPWSTR pszOut,
309 DWORD *pcchOut)
311 HRESULT hRet;
312 IQueryAssociations* lpAssoc;
314 TRACE("(0x%lx,0x%d,%p,%s,%p,%p)\n", cfFlags, str, hkAssoc,
315 debugstr_w(pszExtra), pszOut, pcchOut);
317 hRet = AssocCreate( CLSID_QueryAssociations, &IID_IQueryAssociations, (void **)&lpAssoc );
318 if (FAILED(hRet)) return hRet;
320 cfFlags &= SHLWAPI_DEF_ASSOCF;
321 hRet = IQueryAssociations_Init(lpAssoc, cfFlags, 0, hkAssoc, NULL);
323 if (SUCCEEDED(hRet))
324 hRet = IQueryAssociations_GetString(lpAssoc, cfFlags, str, pszExtra,
325 pszOut, pcchOut);
327 IQueryAssociations_Release(lpAssoc);
328 return hRet;
331 /*************************************************************************
332 * AssocQueryStringByKeyA [SHLWAPI.@]
334 * Get a file association string from the registry, given a starting key.
336 * PARAMS
337 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
338 * str [I] Type of string to get
339 * hkAssoc [I] Key to search below
340 * pszExtra [I] Extra information about the string location
341 * pszOut [O] Destination for the association string
342 * pcchOut [O] Length of pszOut
344 * RETURNS
345 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
346 * Failure: An HRESULT error code indicating the error.
348 HRESULT WINAPI AssocQueryStringByKeyA(ASSOCF cfFlags, ASSOCSTR str, HKEY hkAssoc,
349 LPCSTR pszExtra, LPSTR pszOut,
350 DWORD *pcchOut)
352 WCHAR szExtraW[MAX_PATH], *lpszExtraW = szExtraW;
353 WCHAR szReturnW[MAX_PATH], *lpszReturnW = szReturnW;
354 HRESULT hRet = E_OUTOFMEMORY;
356 TRACE("(0x%lx,0x%d,%p,%s,%p,%p)\n", cfFlags, str, hkAssoc,
357 debugstr_a(pszExtra), pszOut, pcchOut);
359 if (!pcchOut)
360 hRet = E_INVALIDARG;
361 else if (SHLWAPI_ParamAToW(pszExtra, szExtraW, MAX_PATH, &lpszExtraW))
363 DWORD dwLenOut = *pcchOut;
364 if (dwLenOut >= MAX_PATH)
365 lpszReturnW = malloc((dwLenOut + 1) * sizeof(WCHAR));
367 if (lpszReturnW)
369 hRet = AssocQueryStringByKeyW(cfFlags, str, hkAssoc, lpszExtraW,
370 lpszReturnW, &dwLenOut);
372 if (SUCCEEDED(hRet))
373 WideCharToMultiByte(CP_ACP,0,szReturnW,-1,pszOut,dwLenOut,0,0);
374 *pcchOut = dwLenOut;
376 if (lpszReturnW != szReturnW)
377 free(lpszReturnW);
381 if (lpszExtraW != szExtraW)
382 free(lpszExtraW);
383 return hRet;
387 /**************************************************************************
388 * AssocIsDangerous (SHLWAPI.@)
390 * Determine if a file association is dangerous (potentially malware).
392 * PARAMS
393 * lpszAssoc [I] Name of file or file extension to check.
395 * RETURNS
396 * TRUE, if lpszAssoc may potentially be malware (executable),
397 * FALSE, Otherwise.
399 BOOL WINAPI AssocIsDangerous(LPCWSTR lpszAssoc)
401 FIXME("%s\n", debugstr_w(lpszAssoc));
402 return FALSE;