Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / shlwapi / assoc.c
blobe7dcb1601a6bcd6aabf54af0c72f5e63d6276fba
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 * AssocQueryKeyW [SHLWAPI.@]
108 * See AssocQueryKeyA.
110 HRESULT WINAPI AssocQueryKeyW(ASSOCF cfFlags, ASSOCKEY assockey, LPCWSTR pszAssoc,
111 LPCWSTR pszExtra, HKEY *phkeyOut)
113 HRESULT hRet;
114 IQueryAssociations* lpAssoc;
116 TRACE("(0x%8x,0x%8x,%s,%s,%p)\n", cfFlags, assockey, debugstr_w(pszAssoc),
117 debugstr_w(pszExtra), phkeyOut);
119 hRet = AssocCreate( CLSID_QueryAssociations, &IID_IQueryAssociations, (void **)&lpAssoc );
120 if (FAILED(hRet)) return hRet;
122 cfFlags &= SHLWAPI_DEF_ASSOCF;
123 hRet = IQueryAssociations_Init(lpAssoc, cfFlags, pszAssoc, NULL, NULL);
125 if (SUCCEEDED(hRet))
126 hRet = IQueryAssociations_GetKey(lpAssoc, cfFlags, assockey, pszExtra, phkeyOut);
128 IQueryAssociations_Release(lpAssoc);
129 return hRet;
132 /*************************************************************************
133 * AssocQueryKeyA [SHLWAPI.@]
135 * Get a file association key from the registry.
137 * PARAMS
138 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
139 * assockey [I] Type of key to get
140 * pszAssoc [I] Key name to search below
141 * pszExtra [I] Extra information about the key location
142 * phkeyOut [O] Destination for the association key
144 * RETURNS
145 * Success: S_OK. phkeyOut contains the key.
146 * Failure: An HRESULT error code indicating the error.
148 HRESULT WINAPI AssocQueryKeyA(ASSOCF cfFlags, ASSOCKEY assockey, LPCSTR pszAssoc,
149 LPCSTR pszExtra, HKEY *phkeyOut)
151 WCHAR szAssocW[MAX_PATH], *lpszAssocW = NULL;
152 WCHAR szExtraW[MAX_PATH], *lpszExtraW = NULL;
153 HRESULT hRet = E_OUTOFMEMORY;
155 TRACE("(0x%8x,0x%8x,%s,%s,%p)\n", cfFlags, assockey, debugstr_a(pszAssoc),
156 debugstr_a(pszExtra), phkeyOut);
158 if (SHLWAPI_ParamAToW(pszAssoc, szAssocW, MAX_PATH, &lpszAssocW) &&
159 SHLWAPI_ParamAToW(pszExtra, szExtraW, MAX_PATH, &lpszExtraW))
161 hRet = AssocQueryKeyW(cfFlags, assockey, lpszAssocW, lpszExtraW, phkeyOut);
164 if (lpszAssocW != szAssocW)
165 HeapFree(GetProcessHeap(), 0, lpszAssocW);
167 if (lpszExtraW != szExtraW)
168 HeapFree(GetProcessHeap(), 0, lpszExtraW);
170 return hRet;
173 /*************************************************************************
174 * AssocQueryStringW [SHLWAPI.@]
176 * See AssocQueryStringA.
178 HRESULT WINAPI AssocQueryStringW(ASSOCF cfFlags, ASSOCSTR str, LPCWSTR pszAssoc,
179 LPCWSTR pszExtra, LPWSTR pszOut, DWORD *pcchOut)
181 HRESULT hRet;
182 IQueryAssociations* lpAssoc;
184 TRACE("(0x%8x,0x%8x,%s,%s,%p,%p)\n", cfFlags, str, debugstr_w(pszAssoc),
185 debugstr_w(pszExtra), pszOut, pcchOut);
187 if (!pcchOut)
188 return E_UNEXPECTED;
190 hRet = AssocCreate( CLSID_QueryAssociations, &IID_IQueryAssociations, (void **)&lpAssoc );
191 if (FAILED(hRet)) return hRet;
193 hRet = IQueryAssociations_Init(lpAssoc, cfFlags & SHLWAPI_DEF_ASSOCF,
194 pszAssoc, NULL, NULL);
196 if (SUCCEEDED(hRet))
197 hRet = IQueryAssociations_GetString(lpAssoc, cfFlags, str, pszExtra,
198 pszOut, pcchOut);
200 IQueryAssociations_Release(lpAssoc);
201 return hRet;
204 /*************************************************************************
205 * AssocQueryStringA [SHLWAPI.@]
207 * Get a file association string from the registry.
209 * PARAMS
210 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
211 * str [I] Type of string to get (ASSOCSTR enum from "shlwapi.h")
212 * pszAssoc [I] Key name to search below
213 * pszExtra [I] Extra information about the string location
214 * pszOut [O] Destination for the association string
215 * pcchOut [O] Length of pszOut
217 * RETURNS
218 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
219 * Failure: An HRESULT error code indicating the error.
221 HRESULT WINAPI AssocQueryStringA(ASSOCF cfFlags, ASSOCSTR str, LPCSTR pszAssoc,
222 LPCSTR pszExtra, LPSTR pszOut, DWORD *pcchOut)
224 WCHAR szAssocW[MAX_PATH], *lpszAssocW = NULL;
225 WCHAR szExtraW[MAX_PATH], *lpszExtraW = NULL;
226 HRESULT hRet = E_OUTOFMEMORY;
228 TRACE("(0x%8x,0x%8x,%s,%s,%p,%p)\n", cfFlags, str, debugstr_a(pszAssoc),
229 debugstr_a(pszExtra), pszOut, pcchOut);
231 if (!pcchOut)
232 hRet = E_UNEXPECTED;
233 else if (SHLWAPI_ParamAToW(pszAssoc, szAssocW, MAX_PATH, &lpszAssocW) &&
234 SHLWAPI_ParamAToW(pszExtra, szExtraW, MAX_PATH, &lpszExtraW))
236 WCHAR szReturnW[MAX_PATH], *lpszReturnW = szReturnW;
237 DWORD dwLenOut = *pcchOut;
239 if (dwLenOut >= MAX_PATH)
240 lpszReturnW = HeapAlloc(GetProcessHeap(), 0,
241 (dwLenOut + 1) * sizeof(WCHAR));
242 else
243 dwLenOut = sizeof(szReturnW) / sizeof(szReturnW[0]);
245 if (!lpszReturnW)
246 hRet = E_OUTOFMEMORY;
247 else
249 hRet = AssocQueryStringW(cfFlags, str, lpszAssocW, lpszExtraW,
250 lpszReturnW, &dwLenOut);
252 if (SUCCEEDED(hRet))
253 dwLenOut = WideCharToMultiByte(CP_ACP, 0, lpszReturnW, -1,
254 pszOut, *pcchOut, NULL, NULL);
256 *pcchOut = dwLenOut;
257 if (lpszReturnW != szReturnW)
258 HeapFree(GetProcessHeap(), 0, lpszReturnW);
262 if (lpszAssocW != szAssocW)
263 HeapFree(GetProcessHeap(), 0, lpszAssocW);
264 if (lpszExtraW != szExtraW)
265 HeapFree(GetProcessHeap(), 0, lpszExtraW);
266 return hRet;
269 /*************************************************************************
270 * AssocQueryStringByKeyW [SHLWAPI.@]
272 * See AssocQueryStringByKeyA.
274 HRESULT WINAPI AssocQueryStringByKeyW(ASSOCF cfFlags, ASSOCSTR str, HKEY hkAssoc,
275 LPCWSTR pszExtra, LPWSTR pszOut,
276 DWORD *pcchOut)
278 HRESULT hRet;
279 IQueryAssociations* lpAssoc;
281 TRACE("(0x%8x,0x%8x,%p,%s,%p,%p)\n", cfFlags, str, hkAssoc,
282 debugstr_w(pszExtra), pszOut, pcchOut);
284 hRet = AssocCreate( CLSID_QueryAssociations, &IID_IQueryAssociations, (void **)&lpAssoc );
285 if (FAILED(hRet)) return hRet;
287 cfFlags &= SHLWAPI_DEF_ASSOCF;
288 hRet = IQueryAssociations_Init(lpAssoc, cfFlags, 0, hkAssoc, NULL);
290 if (SUCCEEDED(hRet))
291 hRet = IQueryAssociations_GetString(lpAssoc, cfFlags, str, pszExtra,
292 pszOut, pcchOut);
294 IQueryAssociations_Release(lpAssoc);
295 return hRet;
298 /*************************************************************************
299 * AssocQueryStringByKeyA [SHLWAPI.@]
301 * Get a file association string from the registry, given a starting key.
303 * PARAMS
304 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
305 * str [I] Type of string to get
306 * hkAssoc [I] Key to search below
307 * pszExtra [I] Extra information about the string location
308 * pszOut [O] Destination for the association string
309 * pcchOut [O] Length of pszOut
311 * RETURNS
312 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
313 * Failure: An HRESULT error code indicating the error.
315 HRESULT WINAPI AssocQueryStringByKeyA(ASSOCF cfFlags, ASSOCSTR str, HKEY hkAssoc,
316 LPCSTR pszExtra, LPSTR pszOut,
317 DWORD *pcchOut)
319 WCHAR szExtraW[MAX_PATH], *lpszExtraW = szExtraW;
320 WCHAR szReturnW[MAX_PATH], *lpszReturnW = szReturnW;
321 HRESULT hRet = E_OUTOFMEMORY;
323 TRACE("(0x%8x,0x%8x,%p,%s,%p,%p)\n", cfFlags, str, hkAssoc,
324 debugstr_a(pszExtra), pszOut, pcchOut);
326 if (!pcchOut)
327 hRet = E_INVALIDARG;
328 else if (SHLWAPI_ParamAToW(pszExtra, szExtraW, MAX_PATH, &lpszExtraW))
330 DWORD dwLenOut = *pcchOut;
331 if (dwLenOut >= MAX_PATH)
332 lpszReturnW = HeapAlloc(GetProcessHeap(), 0,
333 (dwLenOut + 1) * sizeof(WCHAR));
335 if (lpszReturnW)
337 hRet = AssocQueryStringByKeyW(cfFlags, str, hkAssoc, lpszExtraW,
338 lpszReturnW, &dwLenOut);
340 if (SUCCEEDED(hRet))
341 WideCharToMultiByte(CP_ACP,0,szReturnW,-1,pszOut,dwLenOut,0,0);
342 *pcchOut = dwLenOut;
344 if (lpszReturnW != szReturnW)
345 HeapFree(GetProcessHeap(), 0, lpszReturnW);
349 if (lpszExtraW != szExtraW)
350 HeapFree(GetProcessHeap(), 0, lpszExtraW);
351 return hRet;
355 /**************************************************************************
356 * AssocIsDangerous (SHLWAPI.@)
358 * Determine if a file association is dangerous (potentially malware).
360 * PARAMS
361 * lpszAssoc [I] Name of file or file extension to check.
363 * RETURNS
364 * TRUE, if lpszAssoc may potentially be malware (executable),
365 * FALSE, Otherwise.
367 BOOL WINAPI AssocIsDangerous(LPCWSTR lpszAssoc)
369 FIXME("%s\n", debugstr_w(lpszAssoc));
370 return FALSE;