b34c2724551683e5b8f39ed41368e5c7e03ae087
[reactos.git] / dll / win32 / shlwapi / assoc.c
1 /*
2 * IQueryAssociations helper functions
3 *
4 * Copyright 2002 Jon Griffiths
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "precomp.h"
22
23 /* Default IQueryAssociations::Init() flags */
24 #define SHLWAPI_DEF_ASSOCF (ASSOCF_INIT_BYEXENAME|ASSOCF_INIT_DEFAULTTOSTAR| \
25 ASSOCF_INIT_DEFAULTTOFOLDER)
26
27 /*************************************************************************
28 * SHLWAPI_ParamAToW
29 *
30 * Internal helper function: Convert ASCII parameter to Unicode.
31 */
32 static BOOL SHLWAPI_ParamAToW(LPCSTR lpszParam, LPWSTR lpszBuff, DWORD dwLen,
33 LPWSTR* lpszOut)
34 {
35 if (lpszParam)
36 {
37 DWORD dwStrLen = MultiByteToWideChar(CP_ACP, 0, lpszParam, -1, NULL, 0);
38
39 if (dwStrLen < dwLen)
40 {
41 *lpszOut = lpszBuff; /* Use Buffer, it is big enough */
42 }
43 else
44 {
45 /* Create a new buffer big enough for the string */
46 *lpszOut = HeapAlloc(GetProcessHeap(), 0,
47 dwStrLen * sizeof(WCHAR));
48 if (!*lpszOut)
49 return FALSE;
50 }
51 MultiByteToWideChar(CP_ACP, 0, lpszParam, -1, *lpszOut, dwStrLen);
52 }
53 else
54 *lpszOut = NULL;
55 return TRUE;
56 }
57
58 /*************************************************************************
59 * AssocCreate [SHLWAPI.@]
60 *
61 * Create a new IQueryAssociations object.
62 *
63 * PARAMS
64 * clsid [I] CLSID of object
65 * refiid [I] REFIID of interface
66 * lpInterface [O] Destination for the created IQueryAssociations object
67 *
68 * RETURNS
69 * Success: S_OK. lpInterface contains the new object.
70 * Failure: An HRESULT error code indicating the error.
71 *
72 * NOTES
73 * clsid must be equal to CLSID_QueryAssociations and
74 * refiid must be equal to IID_IQueryAssociations, IID_IUnknown or this function will fail
75 */
76 HRESULT WINAPI AssocCreate(CLSID clsid, REFIID refiid, void **lpInterface)
77 {
78 TRACE("(%s,%s,%p)\n", debugstr_guid(&clsid), debugstr_guid(refiid),
79 lpInterface);
80
81 if (!lpInterface)
82 return E_INVALIDARG;
83
84 *(DWORD*)lpInterface = 0;
85
86 if (!IsEqualGUID(&clsid, &CLSID_QueryAssociations))
87 return CLASS_E_CLASSNOTAVAILABLE;
88
89 return SHCoCreateInstance( NULL, &clsid, NULL, refiid, lpInterface );
90 }
91
92 /*************************************************************************
93 * AssocGetPerceivedType [SHLWAPI.@]
94 *
95 * Detect the type of a file by inspecting its extension
96 *
97 * PARAMS
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
102 *
103 * RETURNS
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.
108 *
109 * NOTES
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.
113 *
114 * BUGS
115 * Unimplemented.
116 */
117 HRESULT WINAPI AssocGetPerceivedType(LPCWSTR lpszExt, PERCEIVED *lpType,
118 INT *lpFlag, LPWSTR *lppszType)
119 {
120 FIXME("(%s, %p, %p, %p) not supported\n", debugstr_w(lpszExt), lpType, lpFlag, lppszType);
121
122 if (lpszExt == NULL)
123 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
124
125 return E_NOTIMPL;
126 }
127
128 /*************************************************************************
129 * AssocQueryKeyW [SHLWAPI.@]
130 *
131 * See AssocQueryKeyA.
132 */
133 HRESULT WINAPI AssocQueryKeyW(ASSOCF cfFlags, ASSOCKEY assockey, LPCWSTR pszAssoc,
134 LPCWSTR pszExtra, HKEY *phkeyOut)
135 {
136 HRESULT hRet;
137 IQueryAssociations* lpAssoc;
138
139 TRACE("(0x%x,%d,%s,%s,%p)\n", cfFlags, assockey, debugstr_w(pszAssoc),
140 debugstr_w(pszExtra), phkeyOut);
141
142 hRet = AssocCreate( CLSID_QueryAssociations, &IID_IQueryAssociations, (void **)&lpAssoc );
143 if (FAILED(hRet)) return hRet;
144
145 cfFlags &= SHLWAPI_DEF_ASSOCF;
146 hRet = IQueryAssociations_Init(lpAssoc, cfFlags, pszAssoc, NULL, NULL);
147
148 if (SUCCEEDED(hRet))
149 hRet = IQueryAssociations_GetKey(lpAssoc, cfFlags, assockey, pszExtra, phkeyOut);
150
151 IQueryAssociations_Release(lpAssoc);
152 return hRet;
153 }
154
155 /*************************************************************************
156 * AssocQueryKeyA [SHLWAPI.@]
157 *
158 * Get a file association key from the registry.
159 *
160 * PARAMS
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
166 *
167 * RETURNS
168 * Success: S_OK. phkeyOut contains the key.
169 * Failure: An HRESULT error code indicating the error.
170 */
171 HRESULT WINAPI AssocQueryKeyA(ASSOCF cfFlags, ASSOCKEY assockey, LPCSTR pszAssoc,
172 LPCSTR pszExtra, HKEY *phkeyOut)
173 {
174 WCHAR szAssocW[MAX_PATH], *lpszAssocW = NULL;
175 WCHAR szExtraW[MAX_PATH], *lpszExtraW = NULL;
176 HRESULT hRet = E_OUTOFMEMORY;
177
178 TRACE("(0x%x,%d,%s,%s,%p)\n", cfFlags, assockey, debugstr_a(pszAssoc),
179 debugstr_a(pszExtra), phkeyOut);
180
181 if (SHLWAPI_ParamAToW(pszAssoc, szAssocW, MAX_PATH, &lpszAssocW) &&
182 SHLWAPI_ParamAToW(pszExtra, szExtraW, MAX_PATH, &lpszExtraW))
183 {
184 hRet = AssocQueryKeyW(cfFlags, assockey, lpszAssocW, lpszExtraW, phkeyOut);
185 }
186
187 if (lpszAssocW != szAssocW)
188 HeapFree(GetProcessHeap(), 0, lpszAssocW);
189
190 if (lpszExtraW != szExtraW)
191 HeapFree(GetProcessHeap(), 0, lpszExtraW);
192
193 return hRet;
194 }
195
196 /*************************************************************************
197 * AssocQueryStringW [SHLWAPI.@]
198 *
199 * See AssocQueryStringA.
200 */
201 HRESULT WINAPI AssocQueryStringW(ASSOCF cfFlags, ASSOCSTR str, LPCWSTR pszAssoc,
202 LPCWSTR pszExtra, LPWSTR pszOut, DWORD *pcchOut)
203 {
204 HRESULT hRet;
205 IQueryAssociations* lpAssoc;
206
207 TRACE("(0x%x,%d,%s,%s,%p,%p)\n", cfFlags, str, debugstr_w(pszAssoc),
208 debugstr_w(pszExtra), pszOut, pcchOut);
209
210 if (!pcchOut)
211 return E_UNEXPECTED;
212
213 hRet = AssocCreate( CLSID_QueryAssociations, &IID_IQueryAssociations, (void **)&lpAssoc );
214 if (FAILED(hRet)) return hRet;
215
216 hRet = IQueryAssociations_Init(lpAssoc, cfFlags & SHLWAPI_DEF_ASSOCF,
217 pszAssoc, NULL, NULL);
218
219 if (SUCCEEDED(hRet))
220 hRet = IQueryAssociations_GetString(lpAssoc, cfFlags, str, pszExtra,
221 pszOut, pcchOut);
222
223 IQueryAssociations_Release(lpAssoc);
224 return hRet;
225 }
226
227 /*************************************************************************
228 * AssocQueryStringA [SHLWAPI.@]
229 *
230 * Get a file association string from the registry.
231 *
232 * PARAMS
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
239 *
240 * RETURNS
241 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
242 * Failure: An HRESULT error code indicating the error.
243 */
244 HRESULT WINAPI AssocQueryStringA(ASSOCF cfFlags, ASSOCSTR str, LPCSTR pszAssoc,
245 LPCSTR pszExtra, LPSTR pszOut, DWORD *pcchOut)
246 {
247 WCHAR szAssocW[MAX_PATH], *lpszAssocW = NULL;
248 WCHAR szExtraW[MAX_PATH], *lpszExtraW = NULL;
249 HRESULT hRet = E_OUTOFMEMORY;
250
251 TRACE("(0x%x,0x%d,%s,%s,%p,%p)\n", cfFlags, str, debugstr_a(pszAssoc),
252 debugstr_a(pszExtra), pszOut, pcchOut);
253
254 if (!pcchOut)
255 hRet = E_UNEXPECTED;
256 else if (SHLWAPI_ParamAToW(pszAssoc, szAssocW, MAX_PATH, &lpszAssocW) &&
257 SHLWAPI_ParamAToW(pszExtra, szExtraW, MAX_PATH, &lpszExtraW))
258 {
259 WCHAR szReturnW[MAX_PATH], *lpszReturnW = szReturnW;
260 DWORD dwLenOut = *pcchOut;
261
262 if (dwLenOut >= MAX_PATH)
263 lpszReturnW = HeapAlloc(GetProcessHeap(), 0,
264 (dwLenOut + 1) * sizeof(WCHAR));
265 else
266 dwLenOut = sizeof(szReturnW) / sizeof(szReturnW[0]);
267
268 if (!lpszReturnW)
269 hRet = E_OUTOFMEMORY;
270 else
271 {
272 hRet = AssocQueryStringW(cfFlags, str, lpszAssocW, lpszExtraW,
273 lpszReturnW, &dwLenOut);
274
275 if (SUCCEEDED(hRet))
276 dwLenOut = WideCharToMultiByte(CP_ACP, 0, lpszReturnW, -1,
277 pszOut, *pcchOut, NULL, NULL);
278
279 *pcchOut = dwLenOut;
280 if (lpszReturnW != szReturnW)
281 HeapFree(GetProcessHeap(), 0, lpszReturnW);
282 }
283 }
284
285 if (lpszAssocW != szAssocW)
286 HeapFree(GetProcessHeap(), 0, lpszAssocW);
287 if (lpszExtraW != szExtraW)
288 HeapFree(GetProcessHeap(), 0, lpszExtraW);
289 return hRet;
290 }
291
292 /*************************************************************************
293 * AssocQueryStringByKeyW [SHLWAPI.@]
294 *
295 * See AssocQueryStringByKeyA.
296 */
297 HRESULT WINAPI AssocQueryStringByKeyW(ASSOCF cfFlags, ASSOCSTR str, HKEY hkAssoc,
298 LPCWSTR pszExtra, LPWSTR pszOut,
299 DWORD *pcchOut)
300 {
301 HRESULT hRet;
302 IQueryAssociations* lpAssoc;
303
304 TRACE("(0x%x,0x%d,%p,%s,%p,%p)\n", cfFlags, str, hkAssoc,
305 debugstr_w(pszExtra), pszOut, pcchOut);
306
307 hRet = AssocCreate( CLSID_QueryAssociations, &IID_IQueryAssociations, (void **)&lpAssoc );
308 if (FAILED(hRet)) return hRet;
309
310 cfFlags &= SHLWAPI_DEF_ASSOCF;
311 hRet = IQueryAssociations_Init(lpAssoc, cfFlags, 0, hkAssoc, NULL);
312
313 if (SUCCEEDED(hRet))
314 hRet = IQueryAssociations_GetString(lpAssoc, cfFlags, str, pszExtra,
315 pszOut, pcchOut);
316
317 IQueryAssociations_Release(lpAssoc);
318 return hRet;
319 }
320
321 /*************************************************************************
322 * AssocQueryStringByKeyA [SHLWAPI.@]
323 *
324 * Get a file association string from the registry, given a starting key.
325 *
326 * PARAMS
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
333 *
334 * RETURNS
335 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
336 * Failure: An HRESULT error code indicating the error.
337 */
338 HRESULT WINAPI AssocQueryStringByKeyA(ASSOCF cfFlags, ASSOCSTR str, HKEY hkAssoc,
339 LPCSTR pszExtra, LPSTR pszOut,
340 DWORD *pcchOut)
341 {
342 WCHAR szExtraW[MAX_PATH], *lpszExtraW = szExtraW;
343 WCHAR szReturnW[MAX_PATH], *lpszReturnW = szReturnW;
344 HRESULT hRet = E_OUTOFMEMORY;
345
346 TRACE("(0x%x,0x%d,%p,%s,%p,%p)\n", cfFlags, str, hkAssoc,
347 debugstr_a(pszExtra), pszOut, pcchOut);
348
349 if (!pcchOut)
350 hRet = E_INVALIDARG;
351 else if (SHLWAPI_ParamAToW(pszExtra, szExtraW, MAX_PATH, &lpszExtraW))
352 {
353 DWORD dwLenOut = *pcchOut;
354 if (dwLenOut >= MAX_PATH)
355 lpszReturnW = HeapAlloc(GetProcessHeap(), 0,
356 (dwLenOut + 1) * sizeof(WCHAR));
357
358 if (lpszReturnW)
359 {
360 hRet = AssocQueryStringByKeyW(cfFlags, str, hkAssoc, lpszExtraW,
361 lpszReturnW, &dwLenOut);
362
363 if (SUCCEEDED(hRet))
364 WideCharToMultiByte(CP_ACP,0,szReturnW,-1,pszOut,dwLenOut,0,0);
365 *pcchOut = dwLenOut;
366
367 if (lpszReturnW != szReturnW)
368 HeapFree(GetProcessHeap(), 0, lpszReturnW);
369 }
370 }
371
372 if (lpszExtraW != szExtraW)
373 HeapFree(GetProcessHeap(), 0, lpszExtraW);
374 return hRet;
375 }
376
377
378 /**************************************************************************
379 * AssocIsDangerous (SHLWAPI.@)
380 *
381 * Determine if a file association is dangerous (potentially malware).
382 *
383 * PARAMS
384 * lpszAssoc [I] Name of file or file extension to check.
385 *
386 * RETURNS
387 * TRUE, if lpszAssoc may potentially be malware (executable),
388 * FALSE, Otherwise.
389 */
390 BOOL WINAPI AssocIsDangerous(LPCWSTR lpszAssoc)
391 {
392 FIXME("%s\n", debugstr_w(lpszAssoc));
393 return FALSE;
394 }