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 #define WIN32_NO_STATUS
22 #define COM_NO_WINDOWS_H
31 //#include "objbase.h"
32 //#include "shlguid.h"
35 //#include "wine/unicode.h"
36 #include <wine/debug.h>
38 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
40 /* Default IQueryAssociations::Init() flags */
41 #define SHLWAPI_DEF_ASSOCF (ASSOCF_INIT_BYEXENAME|ASSOCF_INIT_DEFAULTTOSTAR| \
42 ASSOCF_INIT_DEFAULTTOFOLDER)
44 /*************************************************************************
47 * Internal helper function: Convert ASCII parameter to Unicode.
49 static BOOL
SHLWAPI_ParamAToW(LPCSTR lpszParam
, LPWSTR lpszBuff
, DWORD dwLen
,
54 DWORD dwStrLen
= MultiByteToWideChar(CP_ACP
, 0, lpszParam
, -1, NULL
, 0);
58 *lpszOut
= lpszBuff
; /* Use Buffer, it is big enough */
62 /* Create a new buffer big enough for the string */
63 *lpszOut
= HeapAlloc(GetProcessHeap(), 0,
64 dwStrLen
* sizeof(WCHAR
));
68 MultiByteToWideChar(CP_ACP
, 0, lpszParam
, -1, *lpszOut
, dwStrLen
);
75 /*************************************************************************
76 * AssocCreate [SHLWAPI.@]
78 * Create a new IQueryAssociations object.
81 * clsid [I] CLSID of object
82 * refiid [I] REFIID of interface
83 * lpInterface [O] Destination for the created IQueryAssociations object
86 * Success: S_OK. lpInterface contains the new object.
87 * Failure: An HRESULT error code indicating the error.
90 * clsid must be equal to CLSID_QueryAssociations and
91 * refiid must be equal to IID_IQueryAssociations, IID_IUnknown or this function will fail
93 HRESULT WINAPI
AssocCreate(CLSID clsid
, REFIID refiid
, void **lpInterface
)
95 TRACE("(%s,%s,%p)\n", debugstr_guid(&clsid
), debugstr_guid(refiid
),
101 *(DWORD
*)lpInterface
= 0;
103 if (!IsEqualGUID(&clsid
, &CLSID_QueryAssociations
))
104 return CLASS_E_CLASSNOTAVAILABLE
;
106 return SHCoCreateInstance( NULL
, &clsid
, NULL
, refiid
, lpInterface
);
109 /*************************************************************************
110 * AssocGetPerceivedType [SHLWAPI.@]
112 * Detect the type of a file by inspecting its extension
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
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.
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.
134 HRESULT WINAPI
AssocGetPerceivedType(LPCWSTR lpszExt
, PERCEIVED
*lpType
,
135 INT
*lpFlag
, LPWSTR
*lppszType
)
137 FIXME("(%s, %p, %p, %p) not supported\n", debugstr_w(lpszExt
), lpType
, lpFlag
, lppszType
);
140 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
145 /*************************************************************************
146 * AssocQueryKeyW [SHLWAPI.@]
148 * See AssocQueryKeyA.
150 HRESULT WINAPI
AssocQueryKeyW(ASSOCF cfFlags
, ASSOCKEY assockey
, LPCWSTR pszAssoc
,
151 LPCWSTR pszExtra
, HKEY
*phkeyOut
)
154 IQueryAssociations
* lpAssoc
;
156 TRACE("(0x%x,%d,%s,%s,%p)\n", cfFlags
, assockey
, debugstr_w(pszAssoc
),
157 debugstr_w(pszExtra
), phkeyOut
);
159 hRet
= AssocCreate( CLSID_QueryAssociations
, &IID_IQueryAssociations
, (void **)&lpAssoc
);
160 if (FAILED(hRet
)) return hRet
;
162 cfFlags
&= SHLWAPI_DEF_ASSOCF
;
163 hRet
= IQueryAssociations_Init(lpAssoc
, cfFlags
, pszAssoc
, NULL
, NULL
);
166 hRet
= IQueryAssociations_GetKey(lpAssoc
, cfFlags
, assockey
, pszExtra
, phkeyOut
);
168 IQueryAssociations_Release(lpAssoc
);
172 /*************************************************************************
173 * AssocQueryKeyA [SHLWAPI.@]
175 * Get a file association key from the registry.
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
185 * Success: S_OK. phkeyOut contains the key.
186 * Failure: An HRESULT error code indicating the error.
188 HRESULT WINAPI
AssocQueryKeyA(ASSOCF cfFlags
, ASSOCKEY assockey
, LPCSTR pszAssoc
,
189 LPCSTR pszExtra
, HKEY
*phkeyOut
)
191 WCHAR szAssocW
[MAX_PATH
], *lpszAssocW
= NULL
;
192 WCHAR szExtraW
[MAX_PATH
], *lpszExtraW
= NULL
;
193 HRESULT hRet
= E_OUTOFMEMORY
;
195 TRACE("(0x%x,%d,%s,%s,%p)\n", cfFlags
, assockey
, debugstr_a(pszAssoc
),
196 debugstr_a(pszExtra
), phkeyOut
);
198 if (SHLWAPI_ParamAToW(pszAssoc
, szAssocW
, MAX_PATH
, &lpszAssocW
) &&
199 SHLWAPI_ParamAToW(pszExtra
, szExtraW
, MAX_PATH
, &lpszExtraW
))
201 hRet
= AssocQueryKeyW(cfFlags
, assockey
, lpszAssocW
, lpszExtraW
, phkeyOut
);
204 if (lpszAssocW
!= szAssocW
)
205 HeapFree(GetProcessHeap(), 0, lpszAssocW
);
207 if (lpszExtraW
!= szExtraW
)
208 HeapFree(GetProcessHeap(), 0, lpszExtraW
);
213 /*************************************************************************
214 * AssocQueryStringW [SHLWAPI.@]
216 * See AssocQueryStringA.
218 HRESULT WINAPI
AssocQueryStringW(ASSOCF cfFlags
, ASSOCSTR str
, LPCWSTR pszAssoc
,
219 LPCWSTR pszExtra
, LPWSTR pszOut
, DWORD
*pcchOut
)
222 IQueryAssociations
* lpAssoc
;
224 TRACE("(0x%x,%d,%s,%s,%p,%p)\n", cfFlags
, str
, debugstr_w(pszAssoc
),
225 debugstr_w(pszExtra
), pszOut
, pcchOut
);
230 hRet
= AssocCreate( CLSID_QueryAssociations
, &IID_IQueryAssociations
, (void **)&lpAssoc
);
231 if (FAILED(hRet
)) return hRet
;
233 hRet
= IQueryAssociations_Init(lpAssoc
, cfFlags
& SHLWAPI_DEF_ASSOCF
,
234 pszAssoc
, NULL
, NULL
);
237 hRet
= IQueryAssociations_GetString(lpAssoc
, cfFlags
, str
, pszExtra
,
240 IQueryAssociations_Release(lpAssoc
);
244 /*************************************************************************
245 * AssocQueryStringA [SHLWAPI.@]
247 * Get a file association string from the registry.
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
258 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
259 * Failure: An HRESULT error code indicating the error.
261 HRESULT WINAPI
AssocQueryStringA(ASSOCF cfFlags
, ASSOCSTR str
, LPCSTR pszAssoc
,
262 LPCSTR pszExtra
, LPSTR pszOut
, DWORD
*pcchOut
)
264 WCHAR szAssocW
[MAX_PATH
], *lpszAssocW
= NULL
;
265 WCHAR szExtraW
[MAX_PATH
], *lpszExtraW
= NULL
;
266 HRESULT hRet
= E_OUTOFMEMORY
;
268 TRACE("(0x%x,0x%d,%s,%s,%p,%p)\n", cfFlags
, str
, debugstr_a(pszAssoc
),
269 debugstr_a(pszExtra
), pszOut
, pcchOut
);
273 else if (SHLWAPI_ParamAToW(pszAssoc
, szAssocW
, MAX_PATH
, &lpszAssocW
) &&
274 SHLWAPI_ParamAToW(pszExtra
, szExtraW
, MAX_PATH
, &lpszExtraW
))
276 WCHAR szReturnW
[MAX_PATH
], *lpszReturnW
= szReturnW
;
277 DWORD dwLenOut
= *pcchOut
;
279 if (dwLenOut
>= MAX_PATH
)
280 lpszReturnW
= HeapAlloc(GetProcessHeap(), 0,
281 (dwLenOut
+ 1) * sizeof(WCHAR
));
283 dwLenOut
= sizeof(szReturnW
) / sizeof(szReturnW
[0]);
286 hRet
= E_OUTOFMEMORY
;
289 hRet
= AssocQueryStringW(cfFlags
, str
, lpszAssocW
, lpszExtraW
,
290 lpszReturnW
, &dwLenOut
);
293 dwLenOut
= WideCharToMultiByte(CP_ACP
, 0, lpszReturnW
, -1,
294 pszOut
, *pcchOut
, NULL
, NULL
);
297 if (lpszReturnW
!= szReturnW
)
298 HeapFree(GetProcessHeap(), 0, lpszReturnW
);
302 if (lpszAssocW
!= szAssocW
)
303 HeapFree(GetProcessHeap(), 0, lpszAssocW
);
304 if (lpszExtraW
!= szExtraW
)
305 HeapFree(GetProcessHeap(), 0, lpszExtraW
);
309 /*************************************************************************
310 * AssocQueryStringByKeyW [SHLWAPI.@]
312 * See AssocQueryStringByKeyA.
314 HRESULT WINAPI
AssocQueryStringByKeyW(ASSOCF cfFlags
, ASSOCSTR str
, HKEY hkAssoc
,
315 LPCWSTR pszExtra
, LPWSTR pszOut
,
319 IQueryAssociations
* lpAssoc
;
321 TRACE("(0x%x,0x%d,%p,%s,%p,%p)\n", cfFlags
, str
, hkAssoc
,
322 debugstr_w(pszExtra
), pszOut
, pcchOut
);
324 hRet
= AssocCreate( CLSID_QueryAssociations
, &IID_IQueryAssociations
, (void **)&lpAssoc
);
325 if (FAILED(hRet
)) return hRet
;
327 cfFlags
&= SHLWAPI_DEF_ASSOCF
;
328 hRet
= IQueryAssociations_Init(lpAssoc
, cfFlags
, 0, hkAssoc
, NULL
);
331 hRet
= IQueryAssociations_GetString(lpAssoc
, cfFlags
, str
, pszExtra
,
334 IQueryAssociations_Release(lpAssoc
);
338 /*************************************************************************
339 * AssocQueryStringByKeyA [SHLWAPI.@]
341 * Get a file association string from the registry, given a starting key.
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
352 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
353 * Failure: An HRESULT error code indicating the error.
355 HRESULT WINAPI
AssocQueryStringByKeyA(ASSOCF cfFlags
, ASSOCSTR str
, HKEY hkAssoc
,
356 LPCSTR pszExtra
, LPSTR pszOut
,
359 WCHAR szExtraW
[MAX_PATH
], *lpszExtraW
= szExtraW
;
360 WCHAR szReturnW
[MAX_PATH
], *lpszReturnW
= szReturnW
;
361 HRESULT hRet
= E_OUTOFMEMORY
;
363 TRACE("(0x%x,0x%d,%p,%s,%p,%p)\n", cfFlags
, str
, hkAssoc
,
364 debugstr_a(pszExtra
), pszOut
, pcchOut
);
368 else if (SHLWAPI_ParamAToW(pszExtra
, szExtraW
, MAX_PATH
, &lpszExtraW
))
370 DWORD dwLenOut
= *pcchOut
;
371 if (dwLenOut
>= MAX_PATH
)
372 lpszReturnW
= HeapAlloc(GetProcessHeap(), 0,
373 (dwLenOut
+ 1) * sizeof(WCHAR
));
377 hRet
= AssocQueryStringByKeyW(cfFlags
, str
, hkAssoc
, lpszExtraW
,
378 lpszReturnW
, &dwLenOut
);
381 WideCharToMultiByte(CP_ACP
,0,szReturnW
,-1,pszOut
,dwLenOut
,0,0);
384 if (lpszReturnW
!= szReturnW
)
385 HeapFree(GetProcessHeap(), 0, lpszReturnW
);
389 if (lpszExtraW
!= szExtraW
)
390 HeapFree(GetProcessHeap(), 0, lpszExtraW
);
395 /**************************************************************************
396 * AssocIsDangerous (SHLWAPI.@)
398 * Determine if a file association is dangerous (potentially malware).
401 * lpszAssoc [I] Name of file or file extension to check.
404 * TRUE, if lpszAssoc may potentially be malware (executable),
407 BOOL WINAPI
AssocIsDangerous(LPCWSTR lpszAssoc
)
409 FIXME("%s\n", debugstr_w(lpszAssoc
));