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