2 * IAssemblyEnum implementation
4 * Copyright 2008 James Hawkins
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
32 #include "fusionpriv.h"
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
36 #include "wine/list.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(fusion
);
40 typedef struct _tagASMNAME
48 const IAssemblyEnumVtbl
*lpIAssemblyEnumVtbl
;
50 struct list assemblies
;
55 static HRESULT WINAPI
IAssemblyEnumImpl_QueryInterface(IAssemblyEnum
*iface
,
56 REFIID riid
, LPVOID
*ppobj
)
58 IAssemblyEnumImpl
*This
= (IAssemblyEnumImpl
*)iface
;
60 TRACE("(%p, %s, %p)\n", This
, debugstr_guid(riid
), ppobj
);
64 if (IsEqualIID(riid
, &IID_IUnknown
) ||
65 IsEqualIID(riid
, &IID_IAssemblyEnum
))
67 IUnknown_AddRef(iface
);
72 WARN("(%p, %s, %p): not found\n", This
, debugstr_guid(riid
), ppobj
);
76 static ULONG WINAPI
IAssemblyEnumImpl_AddRef(IAssemblyEnum
*iface
)
78 IAssemblyEnumImpl
*This
= (IAssemblyEnumImpl
*)iface
;
79 ULONG refCount
= InterlockedIncrement(&This
->ref
);
81 TRACE("(%p)->(ref before = %u)\n", This
, refCount
- 1);
86 static ULONG WINAPI
IAssemblyEnumImpl_Release(IAssemblyEnum
*iface
)
88 IAssemblyEnumImpl
*This
= (IAssemblyEnumImpl
*)iface
;
89 ULONG refCount
= InterlockedDecrement(&This
->ref
);
90 struct list
*item
, *cursor
;
92 TRACE("(%p)->(ref before = %u)\n", This
, refCount
+ 1);
96 LIST_FOR_EACH_SAFE(item
, cursor
, &This
->assemblies
)
98 ASMNAME
*asmname
= LIST_ENTRY(item
, ASMNAME
, entry
);
100 list_remove(&asmname
->entry
);
101 IAssemblyName_Release(asmname
->name
);
102 HeapFree(GetProcessHeap(), 0, asmname
);
105 HeapFree(GetProcessHeap(), 0, This
);
111 static HRESULT WINAPI
IAssemblyEnumImpl_GetNextAssembly(IAssemblyEnum
*iface
,
113 IAssemblyName
**ppName
,
116 IAssemblyEnumImpl
*asmenum
= (IAssemblyEnumImpl
*)iface
;
119 TRACE("(%p, %p, %p, %d)\n", iface
, pvReserved
, ppName
, dwFlags
);
124 asmname
= LIST_ENTRY(asmenum
->iter
, ASMNAME
, entry
);
128 *ppName
= asmname
->name
;
129 IAssemblyName_AddRef(*ppName
);
131 asmenum
->iter
= list_next(&asmenum
->assemblies
, asmenum
->iter
);
136 static HRESULT WINAPI
IAssemblyEnumImpl_Reset(IAssemblyEnum
*iface
)
138 IAssemblyEnumImpl
*asmenum
= (IAssemblyEnumImpl
*)iface
;
140 TRACE("(%p)\n", iface
);
142 asmenum
->iter
= list_head(&asmenum
->assemblies
);
146 static HRESULT WINAPI
IAssemblyEnumImpl_Clone(IAssemblyEnum
*iface
,
147 IAssemblyEnum
**ppEnum
)
149 FIXME("(%p, %p) stub!\n", iface
, ppEnum
);
153 static const IAssemblyEnumVtbl AssemblyEnumVtbl
= {
154 IAssemblyEnumImpl_QueryInterface
,
155 IAssemblyEnumImpl_AddRef
,
156 IAssemblyEnumImpl_Release
,
157 IAssemblyEnumImpl_GetNextAssembly
,
158 IAssemblyEnumImpl_Reset
,
159 IAssemblyEnumImpl_Clone
162 static void parse_name(IAssemblyName
*name
, int depth
, LPWSTR path
, LPWSTR buf
)
164 WCHAR disp
[MAX_PATH
];
165 LPCWSTR verptr
, pubkeyptr
;
167 DWORD size
, major_size
, minor_size
, build_size
, revision_size
;
168 WORD major
, minor
, build
, revision
;
170 static const WCHAR star
[] = {'*',0};
171 static const WCHAR ss_fmt
[] = {'%','s','\\','%','s',0};
172 static const WCHAR verpubkey
[] = {'%','s','\\','%','s','_','_','%','s',0};
173 static const WCHAR ver_fmt
[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
175 WCHAR version
[24]; /* strlen("65535") * 4 + 3 + 1 */
176 WCHAR token_str
[TOKEN_LENGTH
+ 1];
177 BYTE token
[BYTES_PER_TOKEN
];
183 hr
= IAssemblyName_GetName(name
, &size
, disp
);
185 sprintfW(buf
, ss_fmt
, path
, disp
);
187 sprintfW(buf
, ss_fmt
, path
, star
);
191 major_size
= sizeof(major
);
192 IAssemblyName_GetProperty(name
, ASM_NAME_MAJOR_VERSION
, &major
, &major_size
);
194 minor_size
= sizeof(minor
);
195 IAssemblyName_GetProperty(name
, ASM_NAME_MINOR_VERSION
, &minor
, &minor_size
);
197 build_size
= sizeof(build
);
198 IAssemblyName_GetProperty(name
, ASM_NAME_BUILD_NUMBER
, &build
, &build_size
);
200 revision_size
= sizeof(revision
);
201 IAssemblyName_GetProperty(name
, ASM_NAME_REVISION_NUMBER
, &revision
, &revision_size
);
203 if (!major_size
|| !minor_size
|| !build_size
|| !revision_size
) verptr
= star
;
206 sprintfW(version
, ver_fmt
, major
, minor
, build
, revision
);
210 size
= sizeof(token
);
211 IAssemblyName_GetProperty(name
, ASM_NAME_PUBLIC_KEY_TOKEN
, token
, &size
);
213 if (!size
) pubkeyptr
= star
;
216 token_to_str(token
, token_str
);
217 pubkeyptr
= token_str
;
220 sprintfW(buf
, verpubkey
, path
, verptr
, pubkeyptr
);
224 static int compare_assembly_names(ASMNAME
*asmname1
, ASMNAME
*asmname2
)
227 WORD version1
, version2
;
228 WCHAR name1
[MAX_PATH
], name2
[MAX_PATH
];
229 WCHAR token_str1
[TOKEN_LENGTH
+ 1], token_str2
[TOKEN_LENGTH
+ 1];
230 BYTE token1
[BYTES_PER_TOKEN
], token2
[BYTES_PER_TOKEN
];
233 size
= sizeof(name1
);
234 IAssemblyName_GetProperty(asmname1
->name
, ASM_NAME_NAME
, &name1
, &size
);
235 size
= sizeof(name2
);
236 IAssemblyName_GetProperty(asmname2
->name
, ASM_NAME_NAME
, &name2
, &size
);
238 if ((ret
= strcmpiW(name1
, name2
))) return ret
;
240 for (i
= ASM_NAME_MAJOR_VERSION
; i
< ASM_NAME_CULTURE
; i
++)
242 size
= sizeof(version1
);
243 IAssemblyName_GetProperty(asmname1
->name
, i
, &version1
, &size
);
244 size
= sizeof(version2
);
245 IAssemblyName_GetProperty(asmname2
->name
, i
, &version2
, &size
);
247 if (version1
< version2
) return -1;
248 if (version1
> version2
) return 1;
251 /* FIXME: compare cultures */
253 size
= sizeof(token1
);
254 IAssemblyName_GetProperty(asmname1
->name
, ASM_NAME_PUBLIC_KEY_TOKEN
, token1
, &size
);
255 size
= sizeof(token2
);
256 IAssemblyName_GetProperty(asmname2
->name
, ASM_NAME_PUBLIC_KEY_TOKEN
, token2
, &size
);
258 token_to_str(token1
, token_str1
);
259 token_to_str(token2
, token_str2
);
261 if ((ret
= strcmpiW(token_str1
, token_str2
))) return ret
;
266 /* insert assembly in list preserving sort order */
267 static void insert_assembly(struct list
*assemblies
, ASMNAME
*to_insert
)
271 LIST_FOR_EACH(item
, assemblies
)
273 ASMNAME
*name
= LIST_ENTRY(item
, ASMNAME
, entry
);
275 if (compare_assembly_names(name
, to_insert
) > 0)
277 list_add_before(&name
->entry
, &to_insert
->entry
);
281 list_add_tail(assemblies
, &to_insert
->entry
);
284 static HRESULT
enum_gac_assemblies(struct list
*assemblies
, IAssemblyName
*name
,
285 int depth
, LPWSTR path
)
287 WIN32_FIND_DATAW ffd
;
289 WCHAR disp
[MAX_PATH
];
290 WCHAR asmpath
[MAX_PATH
];
296 static WCHAR parent
[MAX_PATH
];
298 static const WCHAR dot
[] = {'.',0};
299 static const WCHAR dotdot
[] = {'.','.',0};
300 static const WCHAR search_fmt
[] = {'%','s','\\','*',0};
301 static const WCHAR dblunder
[] = {'_','_',0};
302 static const WCHAR path_fmt
[] = {'%','s','\\','%','s','\\','%','s','.','d','l','l',0};
303 static const WCHAR fmt
[] = {'%','s',',',' ','V','e','r','s','i','o','n','=','%','s',',',' ',
304 'C','u','l','t','u','r','e','=','n','e','u','t','r','a','l',',',' ',
305 'P','u','b','l','i','c','K','e','y','T','o','k','e','n','=','%','s',0};
306 static const WCHAR ss_fmt
[] = {'%','s','\\','%','s',0};
309 parse_name(name
, depth
, path
, buf
);
311 sprintfW(buf
, search_fmt
, path
);
313 hfind
= FindFirstFileW(buf
, &ffd
);
314 if (hfind
== INVALID_HANDLE_VALUE
)
319 if (!lstrcmpW(ffd
.cFileName
, dot
) || !lstrcmpW(ffd
.cFileName
, dotdot
))
325 ptr
= strrchrW(buf
, '\\') + 1;
329 lstrcpyW(parent
, ptr
);
333 sprintfW(asmpath
, path_fmt
, path
, ffd
.cFileName
, parent
);
335 ptr
= strstrW(ffd
.cFileName
, dblunder
);
339 sprintfW(disp
, fmt
, parent
, ffd
.cFileName
, ptr
);
341 asmname
= HeapAlloc(GetProcessHeap(), 0, sizeof(ASMNAME
));
348 hr
= CreateAssemblyNameObject(&asmname
->name
, disp
,
349 CANOF_PARSE_DISPLAY_NAME
, NULL
);
352 HeapFree(GetProcessHeap(), 0, asmname
);
356 hr
= IAssemblyName_SetPath(asmname
->name
, asmpath
);
359 IAssemblyName_Release(asmname
->name
);
360 HeapFree(GetProcessHeap(), 0, asmname
);
364 insert_assembly(assemblies
, asmname
);
368 sprintfW(buf
, ss_fmt
, path
, ffd
.cFileName
);
369 hr
= enum_gac_assemblies(assemblies
, name
, depth
+ 1, buf
);
372 } while (FindNextFileW(hfind
, &ffd
) != 0);
378 static HRESULT
enumerate_gac(IAssemblyEnumImpl
*asmenum
, IAssemblyName
*pName
)
380 WCHAR path
[MAX_PATH
];
385 static WCHAR under32
[] = {'_','3','2',0};
386 static WCHAR msil
[] = {'_','M','S','I','L',0};
389 hr
= GetCachePath(ASM_CACHE_GAC
, buf
, &size
);
394 lstrcatW(path
, under32
);
395 hr
= enum_gac_assemblies(&asmenum
->assemblies
, pName
, 0, path
);
400 lstrcatW(path
, msil
);
401 hr
= enum_gac_assemblies(&asmenum
->assemblies
, pName
, 0, path
);
405 hr
= enum_gac_assemblies(&asmenum
->assemblies
, pName
, 0, buf
);
412 /******************************************************************
413 * CreateAssemblyEnum (FUSION.@)
415 HRESULT WINAPI
CreateAssemblyEnum(IAssemblyEnum
**pEnum
, IUnknown
*pUnkReserved
,
416 IAssemblyName
*pName
, DWORD dwFlags
, LPVOID pvReserved
)
418 IAssemblyEnumImpl
*asmenum
;
421 TRACE("(%p, %p, %p, %08x, %p)\n", pEnum
, pUnkReserved
,
422 pName
, dwFlags
, pvReserved
);
427 if (dwFlags
== 0 || dwFlags
== ASM_CACHE_ROOT
)
430 asmenum
= HeapAlloc(GetProcessHeap(), 0, sizeof(IAssemblyEnumImpl
));
432 return E_OUTOFMEMORY
;
434 asmenum
->lpIAssemblyEnumVtbl
= &AssemblyEnumVtbl
;
436 list_init(&asmenum
->assemblies
);
438 if (dwFlags
& ASM_CACHE_GAC
)
440 hr
= enumerate_gac(asmenum
, pName
);
443 HeapFree(GetProcessHeap(), 0, asmenum
);
448 asmenum
->iter
= list_head(&asmenum
->assemblies
);
449 *pEnum
= (IAssemblyEnum
*)asmenum
;