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
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 /*************************************************************************
43 * Internal helper function: Convert ASCII parameter to Unicode.
45 static BOOL
SHLWAPI_ParamAToW(LPCSTR lpszParam
, LPWSTR lpszBuff
, DWORD dwLen
,
50 DWORD dwStrLen
= MultiByteToWideChar(CP_ACP
, 0, lpszParam
, -1, NULL
, 0);
54 *lpszOut
= lpszBuff
; /* Use Buffer, it is big enough */
58 /* Create a new buffer big enough for the string */
59 *lpszOut
= HeapAlloc(GetProcessHeap(), 0,
60 dwStrLen
* sizeof(WCHAR
));
64 MultiByteToWideChar(CP_ACP
, 0, lpszParam
, -1, *lpszOut
, dwStrLen
);
71 /*************************************************************************
72 * AssocCreate [SHLWAPI.@]
74 * Create a new IQueryAssociations object.
77 * clsid [I] CLSID of object
78 * refiid [I] REFIID of interface
79 * lpInterface [O] Destination for the created IQueryAssociations object
82 * Success: S_OK. lpInterface contains the new object.
83 * Failure: An HRESULT error code indicating the error.
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
),
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
)
114 IQueryAssociations
* lpAssoc
;
116 TRACE("(0x%x,%d,%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
);
126 hRet
= IQueryAssociations_GetKey(lpAssoc
, cfFlags
, assockey
, pszExtra
, phkeyOut
);
128 IQueryAssociations_Release(lpAssoc
);
132 /*************************************************************************
133 * AssocQueryKeyA [SHLWAPI.@]
135 * Get a file association key from the registry.
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
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%x,%d,%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
);
173 /*************************************************************************
174 * AssocQueryStringW [SHLWAPI.@]
176 * See AssocQueryStringA.
178 HRESULT WINAPI
AssocQueryStringW(ASSOCF cfFlags
, ASSOCSTR str
, LPCWSTR pszAssoc
,
179 LPCWSTR pszExtra
, LPWSTR pszOut
, DWORD
*pcchOut
)
182 IQueryAssociations
* lpAssoc
;
184 TRACE("(0x%x,%d,%s,%s,%p,%p)\n", cfFlags
, str
, debugstr_w(pszAssoc
),
185 debugstr_w(pszExtra
), pszOut
, pcchOut
);
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
);
197 hRet
= IQueryAssociations_GetString(lpAssoc
, cfFlags
, str
, pszExtra
,
200 IQueryAssociations_Release(lpAssoc
);
204 /*************************************************************************
205 * AssocQueryStringA [SHLWAPI.@]
207 * Get a file association string from the registry.
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
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%x,0x%d,%s,%s,%p,%p)\n", cfFlags
, str
, debugstr_a(pszAssoc
),
229 debugstr_a(pszExtra
), pszOut
, pcchOut
);
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
));
243 dwLenOut
= sizeof(szReturnW
) / sizeof(szReturnW
[0]);
246 hRet
= E_OUTOFMEMORY
;
249 hRet
= AssocQueryStringW(cfFlags
, str
, lpszAssocW
, lpszExtraW
,
250 lpszReturnW
, &dwLenOut
);
253 dwLenOut
= WideCharToMultiByte(CP_ACP
, 0, lpszReturnW
, -1,
254 pszOut
, *pcchOut
, NULL
, NULL
);
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
);
269 /*************************************************************************
270 * AssocQueryStringByKeyW [SHLWAPI.@]
272 * See AssocQueryStringByKeyA.
274 HRESULT WINAPI
AssocQueryStringByKeyW(ASSOCF cfFlags
, ASSOCSTR str
, HKEY hkAssoc
,
275 LPCWSTR pszExtra
, LPWSTR pszOut
,
279 IQueryAssociations
* lpAssoc
;
281 TRACE("(0x%x,0x%d,%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
);
291 hRet
= IQueryAssociations_GetString(lpAssoc
, cfFlags
, str
, pszExtra
,
294 IQueryAssociations_Release(lpAssoc
);
298 /*************************************************************************
299 * AssocQueryStringByKeyA [SHLWAPI.@]
301 * Get a file association string from the registry, given a starting key.
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
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
,
319 WCHAR szExtraW
[MAX_PATH
], *lpszExtraW
= szExtraW
;
320 WCHAR szReturnW
[MAX_PATH
], *lpszReturnW
= szReturnW
;
321 HRESULT hRet
= E_OUTOFMEMORY
;
323 TRACE("(0x%x,0x%d,%p,%s,%p,%p)\n", cfFlags
, str
, hkAssoc
,
324 debugstr_a(pszExtra
), pszOut
, pcchOut
);
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
));
337 hRet
= AssocQueryStringByKeyW(cfFlags
, str
, hkAssoc
, lpszExtraW
,
338 lpszReturnW
, &dwLenOut
);
341 WideCharToMultiByte(CP_ACP
,0,szReturnW
,-1,pszOut
,dwLenOut
,0,0);
344 if (lpszReturnW
!= szReturnW
)
345 HeapFree(GetProcessHeap(), 0, lpszReturnW
);
349 if (lpszExtraW
!= szExtraW
)
350 HeapFree(GetProcessHeap(), 0, lpszExtraW
);
355 /**************************************************************************
356 * AssocIsDangerous (SHLWAPI.@)
358 * Determine if a file association is dangerous (potentially malware).
361 * lpszAssoc [I] Name of file or file extension to check.
364 * TRUE, if lpszAssoc may potentially be malware (executable),
367 BOOL WINAPI
AssocIsDangerous(LPCWSTR lpszAssoc
)
369 FIXME("%s\n", debugstr_w(lpszAssoc
));