winepulse: Use GetModuleFileName() instead of hardcoded module filename for registry...
[wine.git] / dlls / shlwapi / assoc.c
blobb4f5b0cf8ce4dbf009efa5247c5583cf484cdd02
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 = HeapAlloc(GetProcessHeap(), 0,
59 dwStrLen * sizeof(WCHAR));
60 if (!*lpszOut)
61 return FALSE;
63 MultiByteToWideChar(CP_ACP, 0, lpszParam, -1, *lpszOut, dwStrLen);
65 else
66 *lpszOut = NULL;
67 return TRUE;
70 /*************************************************************************
71 * AssocCreate [SHLWAPI.@]
73 * Create a new IQueryAssociations object.
75 * PARAMS
76 * clsid [I] CLSID of object
77 * refiid [I] REFIID of interface
78 * lpInterface [O] Destination for the created IQueryAssociations object
80 * RETURNS
81 * Success: S_OK. lpInterface contains the new object.
82 * Failure: An HRESULT error code indicating the error.
84 * NOTES
85 * clsid must be equal to CLSID_QueryAssociations and
86 * refiid must be equal to IID_IQueryAssociations, IID_IUnknown or this function will fail
88 HRESULT WINAPI AssocCreate(CLSID clsid, REFIID refiid, void **lpInterface)
90 TRACE("(%s,%s,%p)\n", debugstr_guid(&clsid), debugstr_guid(refiid),
91 lpInterface);
93 if (!lpInterface)
94 return E_INVALIDARG;
96 *(DWORD*)lpInterface = 0;
98 if (!IsEqualGUID(&clsid, &CLSID_QueryAssociations))
99 return CLASS_E_CLASSNOTAVAILABLE;
101 return SHCoCreateInstance( NULL, &clsid, NULL, refiid, lpInterface );
104 /*************************************************************************
105 * AssocGetPerceivedType [SHLWAPI.@]
107 * Detect the type of a file by inspecting its extension
109 * PARAMS
110 * lpszExt [I] File extension to evaluate.
111 * lpType [O] Pointer to perceived type
112 * lpFlag [O] Pointer to perceived type flag
113 * lppszType [O] Address to pointer for perceived type text
115 * RETURNS
116 * Success: S_OK. lpType and lpFlag contain the perceived type and
117 * its information. If lppszType is not NULL, it will point
118 * to a string with perceived type text.
119 * Failure: An HRESULT error code indicating the error.
121 * NOTES
122 * lppszType is optional and it can be NULL.
123 * if lpType or lpFlag are NULL, the function will crash.
124 * if lpszExt is NULL, an error is returned.
126 * BUGS
127 * Unimplemented.
129 HRESULT WINAPI AssocGetPerceivedType(LPCWSTR lpszExt, PERCEIVED *lpType,
130 INT *lpFlag, LPWSTR *lppszType)
132 FIXME("(%s, %p, %p, %p) not supported\n", debugstr_w(lpszExt), lpType, lpFlag, lppszType);
134 if (lpszExt == NULL)
135 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
137 return E_NOTIMPL;
140 /*************************************************************************
141 * AssocQueryKeyW [SHLWAPI.@]
143 * See AssocQueryKeyA.
145 HRESULT WINAPI AssocQueryKeyW(ASSOCF cfFlags, ASSOCKEY assockey, LPCWSTR pszAssoc,
146 LPCWSTR pszExtra, HKEY *phkeyOut)
148 HRESULT hRet;
149 IQueryAssociations* lpAssoc;
151 TRACE("(0x%lx,%d,%s,%s,%p)\n", cfFlags, assockey, debugstr_w(pszAssoc),
152 debugstr_w(pszExtra), phkeyOut);
154 hRet = AssocCreate( CLSID_QueryAssociations, &IID_IQueryAssociations, (void **)&lpAssoc );
155 if (FAILED(hRet)) return hRet;
157 cfFlags &= SHLWAPI_DEF_ASSOCF;
158 hRet = IQueryAssociations_Init(lpAssoc, cfFlags, pszAssoc, NULL, NULL);
160 if (SUCCEEDED(hRet))
161 hRet = IQueryAssociations_GetKey(lpAssoc, cfFlags, assockey, pszExtra, phkeyOut);
163 IQueryAssociations_Release(lpAssoc);
164 return hRet;
167 /*************************************************************************
168 * AssocQueryKeyA [SHLWAPI.@]
170 * Get a file association key from the registry.
172 * PARAMS
173 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
174 * assockey [I] Type of key to get
175 * pszAssoc [I] Key name to search below
176 * pszExtra [I] Extra information about the key location
177 * phkeyOut [O] Destination for the association key
179 * RETURNS
180 * Success: S_OK. phkeyOut contains the key.
181 * Failure: An HRESULT error code indicating the error.
183 HRESULT WINAPI AssocQueryKeyA(ASSOCF cfFlags, ASSOCKEY assockey, LPCSTR pszAssoc,
184 LPCSTR pszExtra, HKEY *phkeyOut)
186 WCHAR szAssocW[MAX_PATH], *lpszAssocW = NULL;
187 WCHAR szExtraW[MAX_PATH], *lpszExtraW = NULL;
188 HRESULT hRet = E_OUTOFMEMORY;
190 TRACE("(0x%lx,%d,%s,%s,%p)\n", cfFlags, assockey, debugstr_a(pszAssoc),
191 debugstr_a(pszExtra), phkeyOut);
193 if (SHLWAPI_ParamAToW(pszAssoc, szAssocW, MAX_PATH, &lpszAssocW) &&
194 SHLWAPI_ParamAToW(pszExtra, szExtraW, MAX_PATH, &lpszExtraW))
196 hRet = AssocQueryKeyW(cfFlags, assockey, lpszAssocW, lpszExtraW, phkeyOut);
199 if (lpszAssocW != szAssocW)
200 HeapFree(GetProcessHeap(), 0, lpszAssocW);
202 if (lpszExtraW != szExtraW)
203 HeapFree(GetProcessHeap(), 0, lpszExtraW);
205 return hRet;
208 /*************************************************************************
209 * AssocQueryStringW [SHLWAPI.@]
211 * See AssocQueryStringA.
213 HRESULT WINAPI AssocQueryStringW(ASSOCF cfFlags, ASSOCSTR str, LPCWSTR pszAssoc,
214 LPCWSTR pszExtra, LPWSTR pszOut, DWORD *pcchOut)
216 HRESULT hRet;
217 IQueryAssociations* lpAssoc;
219 TRACE("(0x%lx,%d,%s,%s,%p,%p)\n", cfFlags, str, debugstr_w(pszAssoc),
220 debugstr_w(pszExtra), pszOut, pcchOut);
222 if (!pcchOut)
223 return E_UNEXPECTED;
225 hRet = AssocCreate( CLSID_QueryAssociations, &IID_IQueryAssociations, (void **)&lpAssoc );
226 if (FAILED(hRet)) return hRet;
228 hRet = IQueryAssociations_Init(lpAssoc, cfFlags & SHLWAPI_DEF_ASSOCF,
229 pszAssoc, NULL, NULL);
231 if (SUCCEEDED(hRet))
232 hRet = IQueryAssociations_GetString(lpAssoc, cfFlags, str, pszExtra,
233 pszOut, pcchOut);
235 IQueryAssociations_Release(lpAssoc);
236 return hRet;
239 /*************************************************************************
240 * AssocQueryStringA [SHLWAPI.@]
242 * Get a file association string from the registry.
244 * PARAMS
245 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
246 * str [I] Type of string to get (ASSOCSTR enum from "shlwapi.h")
247 * pszAssoc [I] Key name to search below
248 * pszExtra [I] Extra information about the string location
249 * pszOut [O] Destination for the association string
250 * pcchOut [O] Length of pszOut
252 * RETURNS
253 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
254 * Failure: An HRESULT error code indicating the error.
256 HRESULT WINAPI AssocQueryStringA(ASSOCF cfFlags, ASSOCSTR str, LPCSTR pszAssoc,
257 LPCSTR pszExtra, LPSTR pszOut, DWORD *pcchOut)
259 WCHAR szAssocW[MAX_PATH], *lpszAssocW = NULL;
260 WCHAR szExtraW[MAX_PATH], *lpszExtraW = NULL;
261 HRESULT hRet = E_OUTOFMEMORY;
263 TRACE("(0x%lx,0x%d,%s,%s,%p,%p)\n", cfFlags, str, debugstr_a(pszAssoc),
264 debugstr_a(pszExtra), pszOut, pcchOut);
266 if (!pcchOut)
267 hRet = E_UNEXPECTED;
268 else if (SHLWAPI_ParamAToW(pszAssoc, szAssocW, MAX_PATH, &lpszAssocW) &&
269 SHLWAPI_ParamAToW(pszExtra, szExtraW, MAX_PATH, &lpszExtraW))
271 WCHAR szReturnW[MAX_PATH], *lpszReturnW = szReturnW;
272 DWORD dwLenOut = *pcchOut;
274 if (dwLenOut >= MAX_PATH)
275 lpszReturnW = HeapAlloc(GetProcessHeap(), 0,
276 (dwLenOut + 1) * sizeof(WCHAR));
277 else
278 dwLenOut = ARRAY_SIZE(szReturnW);
280 if (!lpszReturnW)
281 hRet = E_OUTOFMEMORY;
282 else
284 hRet = AssocQueryStringW(cfFlags, str, lpszAssocW, lpszExtraW,
285 lpszReturnW, &dwLenOut);
287 if (SUCCEEDED(hRet))
288 dwLenOut = WideCharToMultiByte(CP_ACP, 0, lpszReturnW, -1,
289 pszOut, *pcchOut, NULL, NULL);
291 *pcchOut = dwLenOut;
292 if (lpszReturnW != szReturnW)
293 HeapFree(GetProcessHeap(), 0, lpszReturnW);
297 if (lpszAssocW != szAssocW)
298 HeapFree(GetProcessHeap(), 0, lpszAssocW);
299 if (lpszExtraW != szExtraW)
300 HeapFree(GetProcessHeap(), 0, lpszExtraW);
301 return hRet;
304 /*************************************************************************
305 * AssocQueryStringByKeyW [SHLWAPI.@]
307 * See AssocQueryStringByKeyA.
309 HRESULT WINAPI AssocQueryStringByKeyW(ASSOCF cfFlags, ASSOCSTR str, HKEY hkAssoc,
310 LPCWSTR pszExtra, LPWSTR pszOut,
311 DWORD *pcchOut)
313 HRESULT hRet;
314 IQueryAssociations* lpAssoc;
316 TRACE("(0x%lx,0x%d,%p,%s,%p,%p)\n", cfFlags, str, hkAssoc,
317 debugstr_w(pszExtra), pszOut, pcchOut);
319 hRet = AssocCreate( CLSID_QueryAssociations, &IID_IQueryAssociations, (void **)&lpAssoc );
320 if (FAILED(hRet)) return hRet;
322 cfFlags &= SHLWAPI_DEF_ASSOCF;
323 hRet = IQueryAssociations_Init(lpAssoc, cfFlags, 0, hkAssoc, NULL);
325 if (SUCCEEDED(hRet))
326 hRet = IQueryAssociations_GetString(lpAssoc, cfFlags, str, pszExtra,
327 pszOut, pcchOut);
329 IQueryAssociations_Release(lpAssoc);
330 return hRet;
333 /*************************************************************************
334 * AssocQueryStringByKeyA [SHLWAPI.@]
336 * Get a file association string from the registry, given a starting key.
338 * PARAMS
339 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
340 * str [I] Type of string to get
341 * hkAssoc [I] Key to search below
342 * pszExtra [I] Extra information about the string location
343 * pszOut [O] Destination for the association string
344 * pcchOut [O] Length of pszOut
346 * RETURNS
347 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
348 * Failure: An HRESULT error code indicating the error.
350 HRESULT WINAPI AssocQueryStringByKeyA(ASSOCF cfFlags, ASSOCSTR str, HKEY hkAssoc,
351 LPCSTR pszExtra, LPSTR pszOut,
352 DWORD *pcchOut)
354 WCHAR szExtraW[MAX_PATH], *lpszExtraW = szExtraW;
355 WCHAR szReturnW[MAX_PATH], *lpszReturnW = szReturnW;
356 HRESULT hRet = E_OUTOFMEMORY;
358 TRACE("(0x%lx,0x%d,%p,%s,%p,%p)\n", cfFlags, str, hkAssoc,
359 debugstr_a(pszExtra), pszOut, pcchOut);
361 if (!pcchOut)
362 hRet = E_INVALIDARG;
363 else if (SHLWAPI_ParamAToW(pszExtra, szExtraW, MAX_PATH, &lpszExtraW))
365 DWORD dwLenOut = *pcchOut;
366 if (dwLenOut >= MAX_PATH)
367 lpszReturnW = HeapAlloc(GetProcessHeap(), 0,
368 (dwLenOut + 1) * sizeof(WCHAR));
370 if (lpszReturnW)
372 hRet = AssocQueryStringByKeyW(cfFlags, str, hkAssoc, lpszExtraW,
373 lpszReturnW, &dwLenOut);
375 if (SUCCEEDED(hRet))
376 WideCharToMultiByte(CP_ACP,0,szReturnW,-1,pszOut,dwLenOut,0,0);
377 *pcchOut = dwLenOut;
379 if (lpszReturnW != szReturnW)
380 HeapFree(GetProcessHeap(), 0, lpszReturnW);
384 if (lpszExtraW != szExtraW)
385 HeapFree(GetProcessHeap(), 0, lpszExtraW);
386 return hRet;
390 /**************************************************************************
391 * AssocIsDangerous (SHLWAPI.@)
393 * Determine if a file association is dangerous (potentially malware).
395 * PARAMS
396 * lpszAssoc [I] Name of file or file extension to check.
398 * RETURNS
399 * TRUE, if lpszAssoc may potentially be malware (executable),
400 * FALSE, Otherwise.
402 BOOL WINAPI AssocIsDangerous(LPCWSTR lpszAssoc)
404 FIXME("%s\n", debugstr_w(lpszAssoc));
405 return FALSE;