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/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
35 /* Default IQueryAssociations::Init() flags */
36 #define SHLWAPI_DEF_ASSOCF (ASSOCF_INIT_BYEXENAME|ASSOCF_INIT_DEFAULTTOSTAR| \
37 ASSOCF_INIT_DEFAULTTOFOLDER)
39 /*************************************************************************
42 * Internal helper function: Convert ANSI parameter to Unicode.
44 static BOOL
SHLWAPI_ParamAToW(LPCSTR lpszParam
, LPWSTR lpszBuff
, DWORD dwLen
,
49 DWORD dwStrLen
= MultiByteToWideChar(CP_ACP
, 0, lpszParam
, -1, NULL
, 0);
53 *lpszOut
= lpszBuff
; /* Use Buffer, it is big enough */
57 /* Create a new buffer big enough for the string */
58 *lpszOut
= HeapAlloc(GetProcessHeap(), 0,
59 dwStrLen
* sizeof(WCHAR
));
63 MultiByteToWideChar(CP_ACP
, 0, lpszParam
, -1, *lpszOut
, dwStrLen
);
70 /*************************************************************************
71 * AssocCreate [SHLWAPI.@]
73 * Create a new IQueryAssociations object.
76 * clsid [I] CLSID of object
77 * refiid [I] REFIID of interface
78 * lpInterface [O] Destination for the created IQueryAssociations object
81 * Success: S_OK. lpInterface contains the new object.
82 * Failure: An HRESULT error code indicating the error.
85 * clsid must be equal to CLSID_QueryAssociations and
86 * refiid must be equal to IID_IQueryAssociations, IID_IUnknown or this function will fail
88 HRESULT WINAPI
AssocCreate(CLSID clsid
, REFIID refiid
, void **lpInterface
)
90 TRACE("(%s,%s,%p)\n", debugstr_guid(&clsid
), debugstr_guid(refiid
),
96 *(DWORD
*)lpInterface
= 0;
98 if (!IsEqualGUID(&clsid
, &CLSID_QueryAssociations
))
99 return CLASS_E_CLASSNOTAVAILABLE
;
101 return SHCoCreateInstance( NULL
, &clsid
, NULL
, refiid
, lpInterface
);
104 /*************************************************************************
105 * AssocGetPerceivedType [SHLWAPI.@]
107 * Detect the type of a file by inspecting its extension
110 * lpszExt [I] File extension to evaluate.
111 * lpType [O] Pointer to perceived type
112 * lpFlag [O] Pointer to perceived type flag
113 * lppszType [O] Address to pointer for perceived type text
116 * Success: S_OK. lpType and lpFlag contain the perceived type and
117 * its information. If lppszType is not NULL, it will point
118 * to a string with perceived type text.
119 * Failure: An HRESULT error code indicating the error.
122 * lppszType is optional and it can be NULL.
123 * if lpType or lpFlag are NULL, the function will crash.
124 * if lpszExt is NULL, an error is returned.
129 HRESULT WINAPI
AssocGetPerceivedType(LPCWSTR lpszExt
, PERCEIVED
*lpType
,
130 INT
*lpFlag
, LPWSTR
*lppszType
)
132 FIXME("(%s, %p, %p, %p) not supported\n", debugstr_w(lpszExt
), lpType
, lpFlag
, lppszType
);
135 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
140 /*************************************************************************
141 * AssocQueryKeyW [SHLWAPI.@]
143 * See AssocQueryKeyA.
145 HRESULT WINAPI
AssocQueryKeyW(ASSOCF cfFlags
, ASSOCKEY assockey
, LPCWSTR pszAssoc
,
146 LPCWSTR pszExtra
, HKEY
*phkeyOut
)
149 IQueryAssociations
* lpAssoc
;
151 TRACE("(0x%lx,%d,%s,%s,%p)\n", cfFlags
, assockey
, debugstr_w(pszAssoc
),
152 debugstr_w(pszExtra
), phkeyOut
);
154 hRet
= AssocCreate( CLSID_QueryAssociations
, &IID_IQueryAssociations
, (void **)&lpAssoc
);
155 if (FAILED(hRet
)) return hRet
;
157 cfFlags
&= SHLWAPI_DEF_ASSOCF
;
158 hRet
= IQueryAssociations_Init(lpAssoc
, cfFlags
, pszAssoc
, NULL
, NULL
);
161 hRet
= IQueryAssociations_GetKey(lpAssoc
, cfFlags
, assockey
, pszExtra
, phkeyOut
);
163 IQueryAssociations_Release(lpAssoc
);
167 /*************************************************************************
168 * AssocQueryKeyA [SHLWAPI.@]
170 * Get a file association key from the registry.
173 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
174 * assockey [I] Type of key to get
175 * pszAssoc [I] Key name to search below
176 * pszExtra [I] Extra information about the key location
177 * phkeyOut [O] Destination for the association key
180 * Success: S_OK. phkeyOut contains the key.
181 * Failure: An HRESULT error code indicating the error.
183 HRESULT WINAPI
AssocQueryKeyA(ASSOCF cfFlags
, ASSOCKEY assockey
, LPCSTR pszAssoc
,
184 LPCSTR pszExtra
, HKEY
*phkeyOut
)
186 WCHAR szAssocW
[MAX_PATH
], *lpszAssocW
= NULL
;
187 WCHAR szExtraW
[MAX_PATH
], *lpszExtraW
= NULL
;
188 HRESULT hRet
= E_OUTOFMEMORY
;
190 TRACE("(0x%lx,%d,%s,%s,%p)\n", cfFlags
, assockey
, debugstr_a(pszAssoc
),
191 debugstr_a(pszExtra
), phkeyOut
);
193 if (SHLWAPI_ParamAToW(pszAssoc
, szAssocW
, MAX_PATH
, &lpszAssocW
) &&
194 SHLWAPI_ParamAToW(pszExtra
, szExtraW
, MAX_PATH
, &lpszExtraW
))
196 hRet
= AssocQueryKeyW(cfFlags
, assockey
, lpszAssocW
, lpszExtraW
, phkeyOut
);
199 if (lpszAssocW
!= szAssocW
)
200 HeapFree(GetProcessHeap(), 0, lpszAssocW
);
202 if (lpszExtraW
!= szExtraW
)
203 HeapFree(GetProcessHeap(), 0, lpszExtraW
);
208 /*************************************************************************
209 * AssocQueryStringW [SHLWAPI.@]
211 * See AssocQueryStringA.
213 HRESULT WINAPI
AssocQueryStringW(ASSOCF cfFlags
, ASSOCSTR str
, LPCWSTR pszAssoc
,
214 LPCWSTR pszExtra
, LPWSTR pszOut
, DWORD
*pcchOut
)
217 IQueryAssociations
* lpAssoc
;
219 TRACE("(0x%lx,%d,%s,%s,%p,%p)\n", cfFlags
, str
, debugstr_w(pszAssoc
),
220 debugstr_w(pszExtra
), pszOut
, pcchOut
);
225 hRet
= AssocCreate( CLSID_QueryAssociations
, &IID_IQueryAssociations
, (void **)&lpAssoc
);
226 if (FAILED(hRet
)) return hRet
;
228 hRet
= IQueryAssociations_Init(lpAssoc
, cfFlags
& SHLWAPI_DEF_ASSOCF
,
229 pszAssoc
, NULL
, NULL
);
232 hRet
= IQueryAssociations_GetString(lpAssoc
, cfFlags
, str
, pszExtra
,
235 IQueryAssociations_Release(lpAssoc
);
239 /*************************************************************************
240 * AssocQueryStringA [SHLWAPI.@]
242 * Get a file association string from the registry.
245 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
246 * str [I] Type of string to get (ASSOCSTR enum from "shlwapi.h")
247 * pszAssoc [I] Key name to search below
248 * pszExtra [I] Extra information about the string location
249 * pszOut [O] Destination for the association string
250 * pcchOut [O] Length of pszOut
253 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
254 * Failure: An HRESULT error code indicating the error.
256 HRESULT WINAPI
AssocQueryStringA(ASSOCF cfFlags
, ASSOCSTR str
, LPCSTR pszAssoc
,
257 LPCSTR pszExtra
, LPSTR pszOut
, DWORD
*pcchOut
)
259 WCHAR szAssocW
[MAX_PATH
], *lpszAssocW
= NULL
;
260 WCHAR szExtraW
[MAX_PATH
], *lpszExtraW
= NULL
;
261 HRESULT hRet
= E_OUTOFMEMORY
;
263 TRACE("(0x%lx,0x%d,%s,%s,%p,%p)\n", cfFlags
, str
, debugstr_a(pszAssoc
),
264 debugstr_a(pszExtra
), pszOut
, pcchOut
);
268 else if (SHLWAPI_ParamAToW(pszAssoc
, szAssocW
, MAX_PATH
, &lpszAssocW
) &&
269 SHLWAPI_ParamAToW(pszExtra
, szExtraW
, MAX_PATH
, &lpszExtraW
))
271 WCHAR szReturnW
[MAX_PATH
], *lpszReturnW
= szReturnW
;
272 DWORD dwLenOut
= *pcchOut
;
274 if (dwLenOut
>= MAX_PATH
)
275 lpszReturnW
= HeapAlloc(GetProcessHeap(), 0,
276 (dwLenOut
+ 1) * sizeof(WCHAR
));
278 dwLenOut
= ARRAY_SIZE(szReturnW
);
281 hRet
= E_OUTOFMEMORY
;
284 hRet
= AssocQueryStringW(cfFlags
, str
, lpszAssocW
, lpszExtraW
,
285 lpszReturnW
, &dwLenOut
);
288 dwLenOut
= WideCharToMultiByte(CP_ACP
, 0, lpszReturnW
, -1,
289 pszOut
, *pcchOut
, NULL
, NULL
);
292 if (lpszReturnW
!= szReturnW
)
293 HeapFree(GetProcessHeap(), 0, lpszReturnW
);
297 if (lpszAssocW
!= szAssocW
)
298 HeapFree(GetProcessHeap(), 0, lpszAssocW
);
299 if (lpszExtraW
!= szExtraW
)
300 HeapFree(GetProcessHeap(), 0, lpszExtraW
);
304 /*************************************************************************
305 * AssocQueryStringByKeyW [SHLWAPI.@]
307 * See AssocQueryStringByKeyA.
309 HRESULT WINAPI
AssocQueryStringByKeyW(ASSOCF cfFlags
, ASSOCSTR str
, HKEY hkAssoc
,
310 LPCWSTR pszExtra
, LPWSTR pszOut
,
314 IQueryAssociations
* lpAssoc
;
316 TRACE("(0x%lx,0x%d,%p,%s,%p,%p)\n", cfFlags
, str
, hkAssoc
,
317 debugstr_w(pszExtra
), pszOut
, pcchOut
);
319 hRet
= AssocCreate( CLSID_QueryAssociations
, &IID_IQueryAssociations
, (void **)&lpAssoc
);
320 if (FAILED(hRet
)) return hRet
;
322 cfFlags
&= SHLWAPI_DEF_ASSOCF
;
323 hRet
= IQueryAssociations_Init(lpAssoc
, cfFlags
, 0, hkAssoc
, NULL
);
326 hRet
= IQueryAssociations_GetString(lpAssoc
, cfFlags
, str
, pszExtra
,
329 IQueryAssociations_Release(lpAssoc
);
333 /*************************************************************************
334 * AssocQueryStringByKeyA [SHLWAPI.@]
336 * Get a file association string from the registry, given a starting key.
339 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
340 * str [I] Type of string to get
341 * hkAssoc [I] Key to search below
342 * pszExtra [I] Extra information about the string location
343 * pszOut [O] Destination for the association string
344 * pcchOut [O] Length of pszOut
347 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
348 * Failure: An HRESULT error code indicating the error.
350 HRESULT WINAPI
AssocQueryStringByKeyA(ASSOCF cfFlags
, ASSOCSTR str
, HKEY hkAssoc
,
351 LPCSTR pszExtra
, LPSTR pszOut
,
354 WCHAR szExtraW
[MAX_PATH
], *lpszExtraW
= szExtraW
;
355 WCHAR szReturnW
[MAX_PATH
], *lpszReturnW
= szReturnW
;
356 HRESULT hRet
= E_OUTOFMEMORY
;
358 TRACE("(0x%lx,0x%d,%p,%s,%p,%p)\n", cfFlags
, str
, hkAssoc
,
359 debugstr_a(pszExtra
), pszOut
, pcchOut
);
363 else if (SHLWAPI_ParamAToW(pszExtra
, szExtraW
, MAX_PATH
, &lpszExtraW
))
365 DWORD dwLenOut
= *pcchOut
;
366 if (dwLenOut
>= MAX_PATH
)
367 lpszReturnW
= HeapAlloc(GetProcessHeap(), 0,
368 (dwLenOut
+ 1) * sizeof(WCHAR
));
372 hRet
= AssocQueryStringByKeyW(cfFlags
, str
, hkAssoc
, lpszExtraW
,
373 lpszReturnW
, &dwLenOut
);
376 WideCharToMultiByte(CP_ACP
,0,szReturnW
,-1,pszOut
,dwLenOut
,0,0);
379 if (lpszReturnW
!= szReturnW
)
380 HeapFree(GetProcessHeap(), 0, lpszReturnW
);
384 if (lpszExtraW
!= szExtraW
)
385 HeapFree(GetProcessHeap(), 0, lpszExtraW
);
390 /**************************************************************************
391 * AssocIsDangerous (SHLWAPI.@)
393 * Determine if a file association is dangerous (potentially malware).
396 * lpszAssoc [I] Name of file or file extension to check.
399 * TRUE, if lpszAssoc may potentially be malware (executable),
402 BOOL WINAPI
AssocIsDangerous(LPCWSTR lpszAssoc
)
404 FIXME("%s\n", debugstr_w(lpszAssoc
));