2 * SHLWAPI 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * These function simplify the process of using the IQueryAssociations
22 * interface, primarily by providing the means to get an interface pointer.
23 * For those not interested in the object, the AssocQuery* functions hide
24 * the object completely and simply provide the functionality.
26 * We require the implementation from shell32 because there is no interface
27 * for exporting an IQueryAssociations object available from that dll -
28 * as it is, we need the constructor.
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
39 /* Default IQueryAssociations::Init() flags */
40 #define SHLWAPI_DEF_ASSOCF (ASSOCF_INIT_BYEXENAME|ASSOCF_INIT_DEFAULTTOSTAR| \
41 ASSOCF_INIT_DEFAULTTOFOLDER)
44 * This is a temporary placeholder. We need the whole IQueryAssociations object.
46 static IQueryAssociations
* IQueryAssociations_Constructor()
52 /*************************************************************************
55 * Internal helper function: Convert ASCII parameter to Unicode.
57 static BOOL
SHLWAPI_ParamAToW(LPCSTR lpszParam
, LPWSTR lpszBuff
, DWORD dwLen
,
62 DWORD dwStrLen
= lstrlenA(lpszParam
);
66 *lpszOut
= lpszBuff
; /* Use Buffer, it is big enough */
70 /* Create a new buffer big enough for the string */
71 *lpszOut
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0,
72 (dwStrLen
+ 1) * sizeof(WCHAR
));
76 MultiByteToWideChar(0, 0, lpszParam
, -1, *lpszOut
, -1);
83 /*************************************************************************
84 * AssocCreate [SHLWAPI.253]
86 * Create a new IQueryAssociations object.
89 * clsid [I] CLSID of object
90 * refiid [I] REFIID of interface
91 * lpInterface [O] Destination for the created IQueryAssociations object
94 * Success: S_OK. lpInterface contains the new object
95 * Failure: An HRESULT error value
97 HRESULT WINAPI
AssocCreate(CLSID clsid
, REFIID refiid
, void **lpInterface
)
100 IQueryAssociations
* lpAssoc
;
102 TRACE("(%s,%s,%p)\n", debugstr_guid(&clsid
), debugstr_guid(refiid
),
108 *(DWORD
*)lpInterface
= 0;
110 if (!IsEqualGUID(&clsid
, &IID_IQueryAssociations
))
113 lpAssoc
= IQueryAssociations_Constructor();
116 return E_OUTOFMEMORY
;
118 hRet
= IQueryAssociations_QueryInterface(lpAssoc
, refiid
, lpInterface
);
119 IQueryAssociations_Release(lpAssoc
);
123 /*************************************************************************
124 * AssocQueryKeyW [SHLWAPI.255]
126 * See AssocQueryKeyA.
128 HRESULT WINAPI
AssocQueryKeyW(ASSOCF flags
, ASSOCKEY key
, LPCWSTR pszAssoc
,
129 LPCWSTR pszExtra
, HKEY
*phkeyOut
)
132 IQueryAssociations
* lpAssoc
;
134 lpAssoc
= IQueryAssociations_Constructor();
137 return E_OUTOFMEMORY
;
139 flags
&= SHLWAPI_DEF_ASSOCF
;
140 hRet
= IQueryAssociations_Init(lpAssoc
, flags
, pszAssoc
, NULL
, NULL
);
143 hRet
= IQueryAssociations_GetKey(lpAssoc
, flags
, key
, pszExtra
, phkeyOut
);
145 IQueryAssociations_Release(lpAssoc
);
149 /*************************************************************************
150 * AssocQueryKeyA [SHLWAPI.254]
152 * Get a file association key from the registry.
155 * flags [I] Flags for the search
156 * key [I] Type of key to get
157 * pszAssoc [I] Key name to search below
158 * pszExtra [I] Extra information about the key location
159 * phkeyOut [O] Destination for the association key
162 * Success: S_OK. phkeyOut contains the key
163 * Failure: An HRESULT error code
165 HRESULT WINAPI
AssocQueryKeyA(ASSOCF flags
, ASSOCKEY key
, LPCSTR pszAssoc
,
166 LPCSTR pszExtra
, HKEY
*phkeyOut
)
168 WCHAR szAssocW
[MAX_PATH
], *lpszAssocW
= NULL
;
169 WCHAR szExtraW
[MAX_PATH
], *lpszExtraW
= NULL
;
170 HRESULT hRet
= E_OUTOFMEMORY
;
172 if (SHLWAPI_ParamAToW(pszAssoc
, szAssocW
, MAX_PATH
, &lpszAssocW
) &&
173 SHLWAPI_ParamAToW(pszExtra
, szExtraW
, MAX_PATH
, &lpszExtraW
))
175 hRet
= AssocQueryKeyW(flags
, key
, lpszAssocW
, lpszExtraW
, phkeyOut
);
178 if (lpszAssocW
&& lpszAssocW
!= szAssocW
)
179 HeapFree(GetProcessHeap(), 0, lpszAssocW
);
181 if (lpszExtraW
&& lpszExtraW
!= szExtraW
)
182 HeapFree(GetProcessHeap(), 0, lpszExtraW
);
187 /*************************************************************************
188 * AssocQueryStringW [SHLWAPI.384]
190 * See AssocQueryStringA.
192 HRESULT WINAPI
AssocQueryStringW(ASSOCF flags
, ASSOCSTR str
, LPCWSTR pszAssoc
,
193 LPCWSTR pszExtra
, LPWSTR pszOut
, DWORD
*pcchOut
)
196 IQueryAssociations
* lpAssoc
;
201 lpAssoc
= IQueryAssociations_Constructor();
204 return E_OUTOFMEMORY
;
206 hRet
= IQueryAssociations_Init(lpAssoc
, flags
& SHLWAPI_DEF_ASSOCF
,
207 pszAssoc
, NULL
, NULL
);
210 hRet
= IQueryAssociations_GetString(lpAssoc
, flags
, str
, pszExtra
,
213 IQueryAssociations_Release(lpAssoc
);
217 /*************************************************************************
218 * AssocQueryStringA [SHLWAPI.381]
220 * Get a file association string from the registry.
223 * flags [I] Flags for the search
224 * str [I] Type of string to get
225 * pszAssoc [I] Key name to search below
226 * pszExtra [I] Extra information about the string location
227 * pszOut [O] Destination for the association string
228 * pcchOut [O] Length of pszOut
231 * Success: S_OK. pszOut contains the string.
232 * Failure: An HRESULT error code.
234 HRESULT WINAPI
AssocQueryStringA(ASSOCF flags
, ASSOCSTR str
, LPCSTR pszAssoc
,
235 LPCSTR pszExtra
, LPSTR pszOut
, DWORD
*pcchOut
)
237 WCHAR szAssocW
[MAX_PATH
], *lpszAssocW
= NULL
;
238 WCHAR szExtraW
[MAX_PATH
], *lpszExtraW
= NULL
;
239 HRESULT hRet
= E_OUTOFMEMORY
;
243 else if (SHLWAPI_ParamAToW(pszAssoc
, szAssocW
, MAX_PATH
, &lpszAssocW
) &&
244 SHLWAPI_ParamAToW(pszExtra
, szExtraW
, MAX_PATH
, &lpszExtraW
))
246 WCHAR szReturnW
[MAX_PATH
], *lpszReturnW
= szReturnW
;
247 DWORD dwLenOut
= *pcchOut
;
249 if (dwLenOut
>= MAX_PATH
)
250 lpszReturnW
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0,
251 (dwLenOut
+ 1) * sizeof(WCHAR
));
254 hRet
= E_OUTOFMEMORY
;
257 hRet
= AssocQueryStringW(flags
, str
, lpszAssocW
, lpszExtraW
,
258 lpszReturnW
, &dwLenOut
);
261 WideCharToMultiByte(CP_ACP
,0,szReturnW
,-1,pszOut
,dwLenOut
,0,0);
264 if (lpszReturnW
&& lpszReturnW
!= szReturnW
)
265 HeapFree(GetProcessHeap(), 0, lpszReturnW
);
269 if (lpszAssocW
&& lpszAssocW
!= szAssocW
)
270 HeapFree(GetProcessHeap(), 0, lpszAssocW
);
271 if (lpszExtraW
&& lpszExtraW
!= szExtraW
)
272 HeapFree(GetProcessHeap(), 0, lpszExtraW
);
276 /*************************************************************************
277 * AssocQueryStringByKeyW [SHLWAPI.383]
279 * See AssocQueryStringByKeyA.
281 HRESULT WINAPI
AssocQueryStringByKeyW(ASSOCF flags
, ASSOCSTR str
, HKEY hkAssoc
,
282 LPCWSTR pszExtra
, LPWSTR pszOut
,
286 IQueryAssociations
* lpAssoc
;
288 lpAssoc
= IQueryAssociations_Constructor();
291 return E_OUTOFMEMORY
;
293 flags
&= SHLWAPI_DEF_ASSOCF
;
294 hRet
= IQueryAssociations_Init(lpAssoc
, flags
, 0, hkAssoc
, NULL
);
297 hRet
= IQueryAssociations_GetString(lpAssoc
, flags
, str
, pszExtra
,
300 IQueryAssociations_Release(lpAssoc
);
304 /*************************************************************************
305 * AssocQueryStringByKeyA [SHLWAPI.382]
307 * Get a file association string from the registry, given a starting key.
310 * flags [I] Flags for the search
311 * str [I] Type of string to get
312 * hkAssoc [I] Key to search below
313 * pszExtra [I] Extra information about the string location
314 * pszOut [O] Destination for the association string
315 * pcchOut [O] Length of pszOut
318 * Success: S_OK. pszOut contains the string.
319 * Failure: An HRESULT error code.
321 HRESULT WINAPI
AssocQueryStringByKeyA(ASSOCF flags
, ASSOCSTR str
, HKEY hkAssoc
,
322 LPCSTR pszExtra
, LPSTR pszOut
,
325 WCHAR szExtraW
[MAX_PATH
], *lpszExtraW
;
326 WCHAR szReturnW
[MAX_PATH
], *lpszReturnW
= szReturnW
;
327 HRESULT hRet
= E_OUTOFMEMORY
;
331 else if (SHLWAPI_ParamAToW(pszExtra
, szExtraW
, MAX_PATH
, &lpszExtraW
))
333 DWORD dwLenOut
= *pcchOut
;
334 if (dwLenOut
>= MAX_PATH
)
335 lpszReturnW
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0,
336 (dwLenOut
+ 1) * sizeof(WCHAR
));
340 hRet
= AssocQueryStringByKeyW(flags
, str
, hkAssoc
, lpszExtraW
,
341 lpszReturnW
, &dwLenOut
);
344 WideCharToMultiByte(CP_ACP
,0,szReturnW
,-1,pszOut
,dwLenOut
,0,0);
347 if (lpszReturnW
!= szReturnW
)
348 HeapFree(GetProcessHeap(), 0, lpszReturnW
);
352 if (lpszExtraW
&& lpszExtraW
!= szExtraW
)
353 HeapFree(GetProcessHeap(), 0, lpszExtraW
);