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
= malloc(dwStrLen
* sizeof(WCHAR
));
62 MultiByteToWideChar(CP_ACP
, 0, lpszParam
, -1, *lpszOut
, dwStrLen
);
69 /*************************************************************************
70 * AssocCreate [SHLWAPI.@]
72 * Create a new IQueryAssociations object.
75 * clsid [I] CLSID of object
76 * refiid [I] REFIID of interface
77 * lpInterface [O] Destination for the created IQueryAssociations object
80 * Success: S_OK. lpInterface contains the new object.
81 * Failure: An HRESULT error code indicating the error.
84 * clsid must be equal to CLSID_QueryAssociations and
85 * refiid must be equal to IID_IQueryAssociations, IID_IUnknown or this function will fail
87 HRESULT WINAPI
AssocCreate(CLSID clsid
, REFIID refiid
, void **lpInterface
)
89 TRACE("(%s,%s,%p)\n", debugstr_guid(&clsid
), debugstr_guid(refiid
),
95 *(DWORD
*)lpInterface
= 0;
97 if (!IsEqualGUID(&clsid
, &CLSID_QueryAssociations
))
98 return CLASS_E_CLASSNOTAVAILABLE
;
100 return SHCoCreateInstance( NULL
, &clsid
, NULL
, refiid
, lpInterface
);
103 /*************************************************************************
104 * AssocGetPerceivedType [SHLWAPI.@]
106 * Detect the type of a file by inspecting its extension
109 * lpszExt [I] File extension to evaluate.
110 * lpType [O] Pointer to perceived type
111 * lpFlag [O] Pointer to perceived type flag
112 * lppszType [O] Address to pointer for perceived type text
115 * Success: S_OK. lpType and lpFlag contain the perceived type and
116 * its information. If lppszType is not NULL, it will point
117 * to a string with perceived type text.
118 * Failure: An HRESULT error code indicating the error.
121 * lppszType is optional and it can be NULL.
122 * if lpType or lpFlag are NULL, the function will crash.
123 * if lpszExt is NULL, an error is returned.
128 HRESULT WINAPI
AssocGetPerceivedType(LPCWSTR lpszExt
, PERCEIVED
*lpType
,
129 INT
*lpFlag
, LPWSTR
*lppszType
)
131 FIXME("(%s, %p, %p, %p) not supported\n", debugstr_w(lpszExt
), lpType
, lpFlag
, lppszType
);
134 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
139 /*************************************************************************
140 * AssocQueryKeyW [SHLWAPI.@]
142 * See AssocQueryKeyA.
144 HRESULT WINAPI
AssocQueryKeyW(ASSOCF cfFlags
, ASSOCKEY assockey
, LPCWSTR pszAssoc
,
145 LPCWSTR pszExtra
, HKEY
*phkeyOut
)
148 IQueryAssociations
* lpAssoc
;
150 TRACE("(0x%lx,%d,%s,%s,%p)\n", cfFlags
, assockey
, debugstr_w(pszAssoc
),
151 debugstr_w(pszExtra
), phkeyOut
);
153 hRet
= AssocCreate( CLSID_QueryAssociations
, &IID_IQueryAssociations
, (void **)&lpAssoc
);
154 if (FAILED(hRet
)) return hRet
;
156 cfFlags
&= SHLWAPI_DEF_ASSOCF
;
157 hRet
= IQueryAssociations_Init(lpAssoc
, cfFlags
, pszAssoc
, NULL
, NULL
);
160 hRet
= IQueryAssociations_GetKey(lpAssoc
, cfFlags
, assockey
, pszExtra
, phkeyOut
);
162 IQueryAssociations_Release(lpAssoc
);
166 /*************************************************************************
167 * AssocQueryKeyA [SHLWAPI.@]
169 * Get a file association key from the registry.
172 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
173 * assockey [I] Type of key to get
174 * pszAssoc [I] Key name to search below
175 * pszExtra [I] Extra information about the key location
176 * phkeyOut [O] Destination for the association key
179 * Success: S_OK. phkeyOut contains the key.
180 * Failure: An HRESULT error code indicating the error.
182 HRESULT WINAPI
AssocQueryKeyA(ASSOCF cfFlags
, ASSOCKEY assockey
, LPCSTR pszAssoc
,
183 LPCSTR pszExtra
, HKEY
*phkeyOut
)
185 WCHAR szAssocW
[MAX_PATH
], *lpszAssocW
= NULL
;
186 WCHAR szExtraW
[MAX_PATH
], *lpszExtraW
= NULL
;
187 HRESULT hRet
= E_OUTOFMEMORY
;
189 TRACE("(0x%lx,%d,%s,%s,%p)\n", cfFlags
, assockey
, debugstr_a(pszAssoc
),
190 debugstr_a(pszExtra
), phkeyOut
);
192 if (SHLWAPI_ParamAToW(pszAssoc
, szAssocW
, MAX_PATH
, &lpszAssocW
) &&
193 SHLWAPI_ParamAToW(pszExtra
, szExtraW
, MAX_PATH
, &lpszExtraW
))
195 hRet
= AssocQueryKeyW(cfFlags
, assockey
, lpszAssocW
, lpszExtraW
, phkeyOut
);
198 if (lpszAssocW
!= szAssocW
)
201 if (lpszExtraW
!= szExtraW
)
207 /*************************************************************************
208 * AssocQueryStringW [SHLWAPI.@]
210 * See AssocQueryStringA.
212 HRESULT WINAPI
AssocQueryStringW(ASSOCF cfFlags
, ASSOCSTR str
, LPCWSTR pszAssoc
,
213 LPCWSTR pszExtra
, LPWSTR pszOut
, DWORD
*pcchOut
)
216 IQueryAssociations
* lpAssoc
;
218 TRACE("(0x%lx,%d,%s,%s,%p,%p)\n", cfFlags
, str
, debugstr_w(pszAssoc
),
219 debugstr_w(pszExtra
), pszOut
, pcchOut
);
224 hRet
= AssocCreate( CLSID_QueryAssociations
, &IID_IQueryAssociations
, (void **)&lpAssoc
);
225 if (FAILED(hRet
)) return hRet
;
227 hRet
= IQueryAssociations_Init(lpAssoc
, cfFlags
& SHLWAPI_DEF_ASSOCF
,
228 pszAssoc
, NULL
, NULL
);
231 hRet
= IQueryAssociations_GetString(lpAssoc
, cfFlags
, str
, pszExtra
,
234 IQueryAssociations_Release(lpAssoc
);
238 /*************************************************************************
239 * AssocQueryStringA [SHLWAPI.@]
241 * Get a file association string from the registry.
244 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
245 * str [I] Type of string to get (ASSOCSTR enum from "shlwapi.h")
246 * pszAssoc [I] Key name to search below
247 * pszExtra [I] Extra information about the string location
248 * pszOut [O] Destination for the association string
249 * pcchOut [O] Length of pszOut
252 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
253 * Failure: An HRESULT error code indicating the error.
255 HRESULT WINAPI
AssocQueryStringA(ASSOCF cfFlags
, ASSOCSTR str
, LPCSTR pszAssoc
,
256 LPCSTR pszExtra
, LPSTR pszOut
, DWORD
*pcchOut
)
258 WCHAR szAssocW
[MAX_PATH
], *lpszAssocW
= NULL
;
259 WCHAR szExtraW
[MAX_PATH
], *lpszExtraW
= NULL
;
260 HRESULT hRet
= E_OUTOFMEMORY
;
262 TRACE("(0x%lx,0x%d,%s,%s,%p,%p)\n", cfFlags
, str
, debugstr_a(pszAssoc
),
263 debugstr_a(pszExtra
), pszOut
, pcchOut
);
267 else if (SHLWAPI_ParamAToW(pszAssoc
, szAssocW
, MAX_PATH
, &lpszAssocW
) &&
268 SHLWAPI_ParamAToW(pszExtra
, szExtraW
, MAX_PATH
, &lpszExtraW
))
270 WCHAR szReturnW
[MAX_PATH
], *lpszReturnW
= szReturnW
;
271 DWORD dwLenOut
= *pcchOut
;
273 if (dwLenOut
>= MAX_PATH
)
274 lpszReturnW
= malloc((dwLenOut
+ 1) * sizeof(WCHAR
));
276 dwLenOut
= ARRAY_SIZE(szReturnW
);
279 hRet
= E_OUTOFMEMORY
;
282 hRet
= AssocQueryStringW(cfFlags
, str
, lpszAssocW
, lpszExtraW
,
283 lpszReturnW
, &dwLenOut
);
286 dwLenOut
= WideCharToMultiByte(CP_ACP
, 0, lpszReturnW
, -1,
287 pszOut
, *pcchOut
, NULL
, NULL
);
290 if (lpszReturnW
!= szReturnW
)
295 if (lpszAssocW
!= szAssocW
)
297 if (lpszExtraW
!= szExtraW
)
302 /*************************************************************************
303 * AssocQueryStringByKeyW [SHLWAPI.@]
305 * See AssocQueryStringByKeyA.
307 HRESULT WINAPI
AssocQueryStringByKeyW(ASSOCF cfFlags
, ASSOCSTR str
, HKEY hkAssoc
,
308 LPCWSTR pszExtra
, LPWSTR pszOut
,
312 IQueryAssociations
* lpAssoc
;
314 TRACE("(0x%lx,0x%d,%p,%s,%p,%p)\n", cfFlags
, str
, hkAssoc
,
315 debugstr_w(pszExtra
), pszOut
, pcchOut
);
317 hRet
= AssocCreate( CLSID_QueryAssociations
, &IID_IQueryAssociations
, (void **)&lpAssoc
);
318 if (FAILED(hRet
)) return hRet
;
320 cfFlags
&= SHLWAPI_DEF_ASSOCF
;
321 hRet
= IQueryAssociations_Init(lpAssoc
, cfFlags
, 0, hkAssoc
, NULL
);
324 hRet
= IQueryAssociations_GetString(lpAssoc
, cfFlags
, str
, pszExtra
,
327 IQueryAssociations_Release(lpAssoc
);
331 /*************************************************************************
332 * AssocQueryStringByKeyA [SHLWAPI.@]
334 * Get a file association string from the registry, given a starting key.
337 * cfFlags [I] ASSOCF_ flags from "shlwapi.h"
338 * str [I] Type of string to get
339 * hkAssoc [I] Key to search below
340 * pszExtra [I] Extra information about the string location
341 * pszOut [O] Destination for the association string
342 * pcchOut [O] Length of pszOut
345 * Success: S_OK. pszOut contains the string, pcchOut contains its length.
346 * Failure: An HRESULT error code indicating the error.
348 HRESULT WINAPI
AssocQueryStringByKeyA(ASSOCF cfFlags
, ASSOCSTR str
, HKEY hkAssoc
,
349 LPCSTR pszExtra
, LPSTR pszOut
,
352 WCHAR szExtraW
[MAX_PATH
], *lpszExtraW
= szExtraW
;
353 WCHAR szReturnW
[MAX_PATH
], *lpszReturnW
= szReturnW
;
354 HRESULT hRet
= E_OUTOFMEMORY
;
356 TRACE("(0x%lx,0x%d,%p,%s,%p,%p)\n", cfFlags
, str
, hkAssoc
,
357 debugstr_a(pszExtra
), pszOut
, pcchOut
);
361 else if (SHLWAPI_ParamAToW(pszExtra
, szExtraW
, MAX_PATH
, &lpszExtraW
))
363 DWORD dwLenOut
= *pcchOut
;
364 if (dwLenOut
>= MAX_PATH
)
365 lpszReturnW
= malloc((dwLenOut
+ 1) * sizeof(WCHAR
));
369 hRet
= AssocQueryStringByKeyW(cfFlags
, str
, hkAssoc
, lpszExtraW
,
370 lpszReturnW
, &dwLenOut
);
373 WideCharToMultiByte(CP_ACP
,0,szReturnW
,-1,pszOut
,dwLenOut
,0,0);
376 if (lpszReturnW
!= szReturnW
)
381 if (lpszExtraW
!= szExtraW
)
387 /**************************************************************************
388 * AssocIsDangerous (SHLWAPI.@)
390 * Determine if a file association is dangerous (potentially malware).
393 * lpszAssoc [I] Name of file or file extension to check.
396 * TRUE, if lpszAssoc may potentially be malware (executable),
399 BOOL WINAPI
AssocIsDangerous(LPCWSTR lpszAssoc
)
401 FIXME("%s\n", debugstr_w(lpszAssoc
));