regedit: Allow importing strings with escaped NULL.
[wine.git] / dlls / shlwapi / assoc.c
blob31569908b30bb938fa523e40315aa90ff8eb75cc
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/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 /*************************************************************************
41 * SHLWAPI_ParamAToW
43 * Internal helper function: Convert ASCII parameter to Unicode.
45 static BOOL SHLWAPI_ParamAToW(LPCSTR lpszParam, LPWSTR lpszBuff, DWORD dwLen,
46 LPWSTR* lpszOut)
48 if (lpszParam)
50 DWORD dwStrLen = MultiByteToWideChar(CP_ACP, 0, lpszParam, -1, NULL, 0);
52 if (dwStrLen < dwLen)
54 *lpszOut = lpszBuff; /* Use Buffer, it is big enough */
56 else
58 /* Create a new buffer big enough for the string */
59 *lpszOut = HeapAlloc(GetProcessHeap(), 0,
60 dwStrLen * sizeof(WCHAR));
61 if (!*lpszOut)
62 return FALSE;
64 MultiByteToWideChar(CP_ACP, 0, lpszParam, -1, *lpszOut, dwStrLen);
66 else
67 *lpszOut = NULL;
68 return TRUE;
71 /*************************************************************************
72 * AssocCreate [SHLWAPI.@]
74 * Create a new IQueryAssociations object.
76 * PARAMS
77 * clsid [I] CLSID of object
78 * refiid [I] REFIID of interface
79 * lpInterface [O] Destination for the created IQueryAssociations object
81 * RETURNS
82 * Success: S_OK. lpInterface contains the new object.
83 * Failure: An HRESULT error code indicating the error.
85 * NOTES
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),
92 lpInterface);
94 if (!lpInterface)
95 return E_INVALIDARG;
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
110 * PARAMS
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
116 * RETURNS
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.
122 * NOTES
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.
127 * BUGS
128 * Unimplemented.
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);
135 if (lpszExt == NULL)
136 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
138 return E_NOTIMPL;
141 /*************************************************************************
142 * AssocQueryKeyW [SHLWAPI.@]
144 * See AssocQueryKeyA.
146 HRESULT WINAPI AssocQueryKeyW(ASSOCF cfFlags, ASSOCKEY assockey, LPCWSTR pszAssoc,
147 LPCWSTR pszExtra, HKEY *phkeyOut)
149 HRESULT hRet;
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);
161 if (SUCCEEDED(hRet))
162 hRet = IQueryAssociations_GetKey(lpAssoc, cfFlags, assockey, pszExtra, phkeyOut);
164 IQueryAssociations_Release(lpAssoc);
165 return hRet;
168 /*************************************************************************
169 * AssocQueryKeyA [SHLWAPI.@]
171 * Get a file association key from the registry.
173 * PARAMS
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
180 * RETURNS
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);
206 return hRet;
209 /*************************************************************************
210 * AssocQueryStringW [SHLWAPI.@]
212 * See AssocQueryStringA.
214 HRESULT WINAPI AssocQueryStringW(ASSOCF cfFlags, ASSOCSTR str, LPCWSTR pszAssoc,
215 LPCWSTR pszExtra, LPWSTR pszOut, DWORD *pcchOut)
217 HRESULT hRet;
218 IQueryAssociations* lpAssoc;
220 TRACE("(0x%x,%d,%s,%s,%p,%p)\n", cfFlags, str, debugstr_w(pszAssoc),
221 debugstr_w(pszExtra), pszOut, pcchOut);
223 if (!pcchOut)
224 return E_UNEXPECTED;
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);
232 if (SUCCEEDED(hRet))
233 hRet = IQueryAssociations_GetString(lpAssoc, cfFlags, str, pszExtra,
234 pszOut, pcchOut);
236 IQueryAssociations_Release(lpAssoc);
237 return hRet;
240 /*************************************************************************
241 * AssocQueryStringA [SHLWAPI.@]
243 * Get a file association string from the registry.
245 * PARAMS
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
253 * RETURNS
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);
267 if (!pcchOut)
268 hRet = E_UNEXPECTED;
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));
278 else
279 dwLenOut = sizeof(szReturnW) / sizeof(szReturnW[0]);
281 if (!lpszReturnW)
282 hRet = E_OUTOFMEMORY;
283 else
285 hRet = AssocQueryStringW(cfFlags, str, lpszAssocW, lpszExtraW,
286 lpszReturnW, &dwLenOut);
288 if (SUCCEEDED(hRet))
289 dwLenOut = WideCharToMultiByte(CP_ACP, 0, lpszReturnW, -1,
290 pszOut, *pcchOut, NULL, NULL);
292 *pcchOut = dwLenOut;
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);
302 return hRet;
305 /*************************************************************************
306 * AssocQueryStringByKeyW [SHLWAPI.@]
308 * See AssocQueryStringByKeyA.
310 HRESULT WINAPI AssocQueryStringByKeyW(ASSOCF cfFlags, ASSOCSTR str, HKEY hkAssoc,
311 LPCWSTR pszExtra, LPWSTR pszOut,
312 DWORD *pcchOut)
314 HRESULT hRet;
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);
326 if (SUCCEEDED(hRet))
327 hRet = IQueryAssociations_GetString(lpAssoc, cfFlags, str, pszExtra,
328 pszOut, pcchOut);
330 IQueryAssociations_Release(lpAssoc);
331 return hRet;
334 /*************************************************************************
335 * AssocQueryStringByKeyA [SHLWAPI.@]
337 * Get a file association string from the registry, given a starting key.
339 * PARAMS
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
347 * RETURNS
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,
353 DWORD *pcchOut)
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);
362 if (!pcchOut)
363 hRet = E_INVALIDARG;
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));
371 if (lpszReturnW)
373 hRet = AssocQueryStringByKeyW(cfFlags, str, hkAssoc, lpszExtraW,
374 lpszReturnW, &dwLenOut);
376 if (SUCCEEDED(hRet))
377 WideCharToMultiByte(CP_ACP,0,szReturnW,-1,pszOut,dwLenOut,0,0);
378 *pcchOut = dwLenOut;
380 if (lpszReturnW != szReturnW)
381 HeapFree(GetProcessHeap(), 0, lpszReturnW);
385 if (lpszExtraW != szExtraW)
386 HeapFree(GetProcessHeap(), 0, lpszExtraW);
387 return hRet;
391 /**************************************************************************
392 * AssocIsDangerous (SHLWAPI.@)
394 * Determine if a file association is dangerous (potentially malware).
396 * PARAMS
397 * lpszAssoc [I] Name of file or file extension to check.
399 * RETURNS
400 * TRUE, if lpszAssoc may potentially be malware (executable),
401 * FALSE, Otherwise.
403 BOOL WINAPI AssocIsDangerous(LPCWSTR lpszAssoc)
405 FIXME("%s\n", debugstr_w(lpszAssoc));
406 return FALSE;