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 IAssemblyEnum IAssemblyEnum_iface
;
50 struct list assemblies
;
55 static inline IAssemblyEnumImpl
*impl_from_IAssemblyEnum(IAssemblyEnum
*iface
)
57 return CONTAINING_RECORD(iface
, IAssemblyEnumImpl
, IAssemblyEnum_iface
);
60 static HRESULT WINAPI
IAssemblyEnumImpl_QueryInterface(IAssemblyEnum
*iface
,
61 REFIID riid
, LPVOID
*ppobj
)
63 IAssemblyEnumImpl
*This
= impl_from_IAssemblyEnum(iface
);
65 TRACE("(%p, %s, %p)\n", This
, debugstr_guid(riid
), ppobj
);
69 if (IsEqualIID(riid
, &IID_IUnknown
) ||
70 IsEqualIID(riid
, &IID_IAssemblyEnum
))
72 IUnknown_AddRef(iface
);
77 WARN("(%p, %s, %p): not found\n", This
, debugstr_guid(riid
), ppobj
);
81 static ULONG WINAPI
IAssemblyEnumImpl_AddRef(IAssemblyEnum
*iface
)
83 IAssemblyEnumImpl
*This
= impl_from_IAssemblyEnum(iface
);
84 ULONG refCount
= InterlockedIncrement(&This
->ref
);
86 TRACE("(%p)->(ref before = %u)\n", This
, refCount
- 1);
91 static ULONG WINAPI
IAssemblyEnumImpl_Release(IAssemblyEnum
*iface
)
93 IAssemblyEnumImpl
*This
= impl_from_IAssemblyEnum(iface
);
94 ULONG refCount
= InterlockedDecrement(&This
->ref
);
95 struct list
*item
, *cursor
;
97 TRACE("(%p)->(ref before = %u)\n", This
, refCount
+ 1);
101 LIST_FOR_EACH_SAFE(item
, cursor
, &This
->assemblies
)
103 ASMNAME
*asmname
= LIST_ENTRY(item
, ASMNAME
, entry
);
105 list_remove(&asmname
->entry
);
106 IAssemblyName_Release(asmname
->name
);
107 HeapFree(GetProcessHeap(), 0, asmname
);
110 HeapFree(GetProcessHeap(), 0, This
);
116 static HRESULT WINAPI
IAssemblyEnumImpl_GetNextAssembly(IAssemblyEnum
*iface
,
118 IAssemblyName
**ppName
,
121 IAssemblyEnumImpl
*asmenum
= impl_from_IAssemblyEnum(iface
);
124 TRACE("(%p, %p, %p, %d)\n", iface
, pvReserved
, ppName
, dwFlags
);
129 asmname
= LIST_ENTRY(asmenum
->iter
, ASMNAME
, entry
);
133 *ppName
= asmname
->name
;
134 IAssemblyName_AddRef(*ppName
);
136 asmenum
->iter
= list_next(&asmenum
->assemblies
, asmenum
->iter
);
141 static HRESULT WINAPI
IAssemblyEnumImpl_Reset(IAssemblyEnum
*iface
)
143 IAssemblyEnumImpl
*asmenum
= impl_from_IAssemblyEnum(iface
);
145 TRACE("(%p)\n", iface
);
147 asmenum
->iter
= list_head(&asmenum
->assemblies
);
151 static HRESULT WINAPI
IAssemblyEnumImpl_Clone(IAssemblyEnum
*iface
,
152 IAssemblyEnum
**ppEnum
)
154 FIXME("(%p, %p) stub!\n", iface
, ppEnum
);
158 static const IAssemblyEnumVtbl AssemblyEnumVtbl
= {
159 IAssemblyEnumImpl_QueryInterface
,
160 IAssemblyEnumImpl_AddRef
,
161 IAssemblyEnumImpl_Release
,
162 IAssemblyEnumImpl_GetNextAssembly
,
163 IAssemblyEnumImpl_Reset
,
164 IAssemblyEnumImpl_Clone
167 static void parse_name(IAssemblyName
*name
, int depth
, LPWSTR path
, LPWSTR buf
)
169 WCHAR disp
[MAX_PATH
];
170 LPCWSTR verptr
, pubkeyptr
;
172 DWORD size
, major_size
, minor_size
, build_size
, revision_size
;
173 WORD major
, minor
, build
, revision
;
175 static const WCHAR star
[] = {'*',0};
176 static const WCHAR ss_fmt
[] = {'%','s','\\','%','s',0};
177 static const WCHAR verpubkey
[] = {'%','s','\\','%','s','_','_','%','s',0};
178 static const WCHAR ver_fmt
[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
180 WCHAR version
[24]; /* strlen("65535") * 4 + 3 + 1 */
181 WCHAR token_str
[TOKEN_LENGTH
+ 1];
182 BYTE token
[BYTES_PER_TOKEN
];
188 hr
= IAssemblyName_GetName(name
, &size
, disp
);
190 sprintfW(buf
, ss_fmt
, path
, disp
);
192 sprintfW(buf
, ss_fmt
, path
, star
);
196 major_size
= sizeof(major
);
197 IAssemblyName_GetProperty(name
, ASM_NAME_MAJOR_VERSION
, &major
, &major_size
);
199 minor_size
= sizeof(minor
);
200 IAssemblyName_GetProperty(name
, ASM_NAME_MINOR_VERSION
, &minor
, &minor_size
);
202 build_size
= sizeof(build
);
203 IAssemblyName_GetProperty(name
, ASM_NAME_BUILD_NUMBER
, &build
, &build_size
);
205 revision_size
= sizeof(revision
);
206 IAssemblyName_GetProperty(name
, ASM_NAME_REVISION_NUMBER
, &revision
, &revision_size
);
208 if (!major_size
|| !minor_size
|| !build_size
|| !revision_size
) verptr
= star
;
211 sprintfW(version
, ver_fmt
, major
, minor
, build
, revision
);
215 size
= sizeof(token
);
216 IAssemblyName_GetProperty(name
, ASM_NAME_PUBLIC_KEY_TOKEN
, token
, &size
);
218 if (!size
) pubkeyptr
= star
;
221 token_to_str(token
, token_str
);
222 pubkeyptr
= token_str
;
225 sprintfW(buf
, verpubkey
, path
, verptr
, pubkeyptr
);
229 static int compare_assembly_names(ASMNAME
*asmname1
, ASMNAME
*asmname2
)
232 WORD version1
, version2
;
233 WCHAR name1
[MAX_PATH
], name2
[MAX_PATH
];
234 WCHAR token_str1
[TOKEN_LENGTH
+ 1], token_str2
[TOKEN_LENGTH
+ 1];
235 BYTE token1
[BYTES_PER_TOKEN
], token2
[BYTES_PER_TOKEN
];
238 size
= sizeof(name1
);
239 IAssemblyName_GetProperty(asmname1
->name
, ASM_NAME_NAME
, name1
, &size
);
240 size
= sizeof(name2
);
241 IAssemblyName_GetProperty(asmname2
->name
, ASM_NAME_NAME
, name2
, &size
);
243 if ((ret
= strcmpiW(name1
, name2
))) return ret
;
245 for (i
= ASM_NAME_MAJOR_VERSION
; i
< ASM_NAME_CULTURE
; i
++)
247 size
= sizeof(version1
);
248 IAssemblyName_GetProperty(asmname1
->name
, i
, &version1
, &size
);
249 size
= sizeof(version2
);
250 IAssemblyName_GetProperty(asmname2
->name
, i
, &version2
, &size
);
252 if (version1
< version2
) return -1;
253 if (version1
> version2
) return 1;
256 /* FIXME: compare cultures */
258 size
= sizeof(token1
);
259 IAssemblyName_GetProperty(asmname1
->name
, ASM_NAME_PUBLIC_KEY_TOKEN
, token1
, &size
);
260 size
= sizeof(token2
);
261 IAssemblyName_GetProperty(asmname2
->name
, ASM_NAME_PUBLIC_KEY_TOKEN
, token2
, &size
);
263 token_to_str(token1
, token_str1
);
264 token_to_str(token2
, token_str2
);
266 if ((ret
= strcmpiW(token_str1
, token_str2
))) return ret
;
271 /* insert assembly in list preserving sort order */
272 static void insert_assembly(struct list
*assemblies
, ASMNAME
*to_insert
)
276 LIST_FOR_EACH(item
, assemblies
)
278 ASMNAME
*name
= LIST_ENTRY(item
, ASMNAME
, entry
);
280 if (compare_assembly_names(name
, to_insert
) > 0)
282 list_add_before(&name
->entry
, &to_insert
->entry
);
286 list_add_tail(assemblies
, &to_insert
->entry
);
289 static HRESULT
enum_gac_assemblies(struct list
*assemblies
, IAssemblyName
*name
,
290 int depth
, LPWSTR path
)
292 WIN32_FIND_DATAW ffd
;
294 WCHAR disp
[MAX_PATH
];
295 WCHAR asmpath
[MAX_PATH
];
301 static WCHAR parent
[MAX_PATH
];
303 static const WCHAR dot
[] = {'.',0};
304 static const WCHAR dotdot
[] = {'.','.',0};
305 static const WCHAR search_fmt
[] = {'%','s','\\','*',0};
306 static const WCHAR dblunder
[] = {'_','_',0};
307 static const WCHAR path_fmt
[] = {'%','s','\\','%','s','\\','%','s','.','d','l','l',0};
308 static const WCHAR fmt
[] = {'%','s',',',' ','V','e','r','s','i','o','n','=','%','s',',',' ',
309 'C','u','l','t','u','r','e','=','n','e','u','t','r','a','l',',',' ',
310 'P','u','b','l','i','c','K','e','y','T','o','k','e','n','=','%','s',0};
311 static const WCHAR ss_fmt
[] = {'%','s','\\','%','s',0};
314 parse_name(name
, depth
, path
, buf
);
316 sprintfW(buf
, search_fmt
, path
);
318 hfind
= FindFirstFileW(buf
, &ffd
);
319 if (hfind
== INVALID_HANDLE_VALUE
)
324 if (!lstrcmpW(ffd
.cFileName
, dot
) || !lstrcmpW(ffd
.cFileName
, dotdot
))
330 ptr
= strrchrW(buf
, '\\') + 1;
334 lstrcpyW(parent
, ptr
);
338 sprintfW(asmpath
, path_fmt
, path
, ffd
.cFileName
, parent
);
340 ptr
= strstrW(ffd
.cFileName
, dblunder
);
344 sprintfW(disp
, fmt
, parent
, ffd
.cFileName
, ptr
);
346 asmname
= HeapAlloc(GetProcessHeap(), 0, sizeof(ASMNAME
));
353 hr
= CreateAssemblyNameObject(&asmname
->name
, disp
,
354 CANOF_PARSE_DISPLAY_NAME
, NULL
);
357 HeapFree(GetProcessHeap(), 0, asmname
);
361 hr
= IAssemblyName_SetPath(asmname
->name
, asmpath
);
364 IAssemblyName_Release(asmname
->name
);
365 HeapFree(GetProcessHeap(), 0, asmname
);
369 insert_assembly(assemblies
, asmname
);
373 sprintfW(buf
, ss_fmt
, path
, ffd
.cFileName
);
374 hr
= enum_gac_assemblies(assemblies
, name
, depth
+ 1, buf
);
377 } while (FindNextFileW(hfind
, &ffd
) != 0);
383 static HRESULT
enumerate_gac(IAssemblyEnumImpl
*asmenum
, IAssemblyName
*pName
)
385 WCHAR path
[MAX_PATH
];
390 static WCHAR under32
[] = {'_','3','2',0};
391 static WCHAR msil
[] = {'_','M','S','I','L',0};
394 hr
= GetCachePath(ASM_CACHE_GAC
, buf
, &size
);
399 lstrcatW(path
, under32
);
400 hr
= enum_gac_assemblies(&asmenum
->assemblies
, pName
, 0, path
);
405 lstrcatW(path
, msil
);
406 hr
= enum_gac_assemblies(&asmenum
->assemblies
, pName
, 0, path
);
410 hr
= enum_gac_assemblies(&asmenum
->assemblies
, pName
, 0, buf
);
417 /******************************************************************
418 * CreateAssemblyEnum (FUSION.@)
420 HRESULT WINAPI
CreateAssemblyEnum(IAssemblyEnum
**pEnum
, IUnknown
*pUnkReserved
,
421 IAssemblyName
*pName
, DWORD dwFlags
, LPVOID pvReserved
)
423 IAssemblyEnumImpl
*asmenum
;
426 TRACE("(%p, %p, %p, %08x, %p)\n", pEnum
, pUnkReserved
,
427 pName
, dwFlags
, pvReserved
);
432 if (dwFlags
== 0 || dwFlags
== ASM_CACHE_ROOT
)
435 asmenum
= HeapAlloc(GetProcessHeap(), 0, sizeof(IAssemblyEnumImpl
));
437 return E_OUTOFMEMORY
;
439 asmenum
->IAssemblyEnum_iface
.lpVtbl
= &AssemblyEnumVtbl
;
441 list_init(&asmenum
->assemblies
);
443 if (dwFlags
& ASM_CACHE_GAC
)
445 hr
= enumerate_gac(asmenum
, pName
);
448 HeapFree(GetProcessHeap(), 0, asmenum
);
453 asmenum
->iter
= list_head(&asmenum
->assemblies
);
454 *pEnum
= &asmenum
->IAssemblyEnum_iface
;