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
21 #define WIN32_NO_STATUS
23 #define COM_NO_WINDOWS_H
31 //#include "winuser.h"
33 //#include "guiddef.h"
35 //#include "corerror.h"
36 #include "fusionpriv.h"
38 #include <wine/debug.h>
39 #include <wine/unicode.h>
40 #include <wine/list.h>
42 WINE_DEFAULT_DEBUG_CHANNEL(fusion
);
44 typedef struct _tagASMNAME
52 IAssemblyEnum IAssemblyEnum_iface
;
54 struct list assemblies
;
59 static inline IAssemblyEnumImpl
*impl_from_IAssemblyEnum(IAssemblyEnum
*iface
)
61 return CONTAINING_RECORD(iface
, IAssemblyEnumImpl
, IAssemblyEnum_iface
);
64 static HRESULT WINAPI
IAssemblyEnumImpl_QueryInterface(IAssemblyEnum
*iface
,
65 REFIID riid
, LPVOID
*ppobj
)
67 IAssemblyEnumImpl
*This
= impl_from_IAssemblyEnum(iface
);
69 TRACE("(%p, %s, %p)\n", This
, debugstr_guid(riid
), ppobj
);
73 if (IsEqualIID(riid
, &IID_IUnknown
) ||
74 IsEqualIID(riid
, &IID_IAssemblyEnum
))
76 IUnknown_AddRef(iface
);
81 WARN("(%p, %s, %p): not found\n", This
, debugstr_guid(riid
), ppobj
);
85 static ULONG WINAPI
IAssemblyEnumImpl_AddRef(IAssemblyEnum
*iface
)
87 IAssemblyEnumImpl
*This
= impl_from_IAssemblyEnum(iface
);
88 ULONG refCount
= InterlockedIncrement(&This
->ref
);
90 TRACE("(%p)->(ref before = %u)\n", This
, refCount
- 1);
95 static ULONG WINAPI
IAssemblyEnumImpl_Release(IAssemblyEnum
*iface
)
97 IAssemblyEnumImpl
*This
= impl_from_IAssemblyEnum(iface
);
98 ULONG refCount
= InterlockedDecrement(&This
->ref
);
99 struct list
*item
, *cursor
;
101 TRACE("(%p)->(ref before = %u)\n", This
, refCount
+ 1);
105 LIST_FOR_EACH_SAFE(item
, cursor
, &This
->assemblies
)
107 ASMNAME
*asmname
= LIST_ENTRY(item
, ASMNAME
, entry
);
109 list_remove(&asmname
->entry
);
110 IAssemblyName_Release(asmname
->name
);
111 HeapFree(GetProcessHeap(), 0, asmname
);
114 HeapFree(GetProcessHeap(), 0, This
);
120 static HRESULT WINAPI
IAssemblyEnumImpl_GetNextAssembly(IAssemblyEnum
*iface
,
122 IAssemblyName
**ppName
,
125 IAssemblyEnumImpl
*asmenum
= impl_from_IAssemblyEnum(iface
);
128 TRACE("(%p, %p, %p, %d)\n", iface
, pvReserved
, ppName
, dwFlags
);
133 asmname
= LIST_ENTRY(asmenum
->iter
, ASMNAME
, entry
);
137 *ppName
= asmname
->name
;
138 IAssemblyName_AddRef(*ppName
);
140 asmenum
->iter
= list_next(&asmenum
->assemblies
, asmenum
->iter
);
145 static HRESULT WINAPI
IAssemblyEnumImpl_Reset(IAssemblyEnum
*iface
)
147 IAssemblyEnumImpl
*asmenum
= impl_from_IAssemblyEnum(iface
);
149 TRACE("(%p)\n", iface
);
151 asmenum
->iter
= list_head(&asmenum
->assemblies
);
155 static HRESULT WINAPI
IAssemblyEnumImpl_Clone(IAssemblyEnum
*iface
,
156 IAssemblyEnum
**ppEnum
)
158 FIXME("(%p, %p) stub!\n", iface
, ppEnum
);
162 static const IAssemblyEnumVtbl AssemblyEnumVtbl
= {
163 IAssemblyEnumImpl_QueryInterface
,
164 IAssemblyEnumImpl_AddRef
,
165 IAssemblyEnumImpl_Release
,
166 IAssemblyEnumImpl_GetNextAssembly
,
167 IAssemblyEnumImpl_Reset
,
168 IAssemblyEnumImpl_Clone
171 static void parse_name(IAssemblyName
*name
, int depth
, LPWSTR path
, LPWSTR buf
)
173 WCHAR disp
[MAX_PATH
];
174 LPCWSTR verptr
, pubkeyptr
;
176 DWORD size
, major_size
, minor_size
, build_size
, revision_size
;
177 WORD major
, minor
, build
, revision
;
179 static const WCHAR star
[] = {'*',0};
180 static const WCHAR ss_fmt
[] = {'%','s','\\','%','s',0};
181 static const WCHAR verpubkey
[] = {'%','s','\\','%','s','_','_','%','s',0};
182 static const WCHAR ver_fmt
[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
184 WCHAR version
[24]; /* strlen("65535") * 4 + 3 + 1 */
185 WCHAR token_str
[TOKEN_LENGTH
+ 1];
186 BYTE token
[BYTES_PER_TOKEN
];
192 hr
= IAssemblyName_GetName(name
, &size
, disp
);
194 sprintfW(buf
, ss_fmt
, path
, disp
);
196 sprintfW(buf
, ss_fmt
, path
, star
);
200 major_size
= sizeof(major
);
201 IAssemblyName_GetProperty(name
, ASM_NAME_MAJOR_VERSION
, &major
, &major_size
);
203 minor_size
= sizeof(minor
);
204 IAssemblyName_GetProperty(name
, ASM_NAME_MINOR_VERSION
, &minor
, &minor_size
);
206 build_size
= sizeof(build
);
207 IAssemblyName_GetProperty(name
, ASM_NAME_BUILD_NUMBER
, &build
, &build_size
);
209 revision_size
= sizeof(revision
);
210 IAssemblyName_GetProperty(name
, ASM_NAME_REVISION_NUMBER
, &revision
, &revision_size
);
212 if (!major_size
|| !minor_size
|| !build_size
|| !revision_size
) verptr
= star
;
215 sprintfW(version
, ver_fmt
, major
, minor
, build
, revision
);
219 size
= sizeof(token
);
220 IAssemblyName_GetProperty(name
, ASM_NAME_PUBLIC_KEY_TOKEN
, token
, &size
);
222 if (!size
) pubkeyptr
= star
;
225 token_to_str(token
, token_str
);
226 pubkeyptr
= token_str
;
229 sprintfW(buf
, verpubkey
, path
, verptr
, pubkeyptr
);
233 static int compare_assembly_names(ASMNAME
*asmname1
, ASMNAME
*asmname2
)
236 WORD version1
, version2
;
237 WCHAR name1
[MAX_PATH
], name2
[MAX_PATH
];
238 WCHAR token_str1
[TOKEN_LENGTH
+ 1], token_str2
[TOKEN_LENGTH
+ 1];
239 BYTE token1
[BYTES_PER_TOKEN
], token2
[BYTES_PER_TOKEN
];
242 size
= sizeof(name1
);
243 IAssemblyName_GetProperty(asmname1
->name
, ASM_NAME_NAME
, name1
, &size
);
244 size
= sizeof(name2
);
245 IAssemblyName_GetProperty(asmname2
->name
, ASM_NAME_NAME
, name2
, &size
);
247 if ((ret
= strcmpiW(name1
, name2
))) return ret
;
249 for (i
= ASM_NAME_MAJOR_VERSION
; i
< ASM_NAME_CULTURE
; i
++)
251 size
= sizeof(version1
);
252 IAssemblyName_GetProperty(asmname1
->name
, i
, &version1
, &size
);
253 size
= sizeof(version2
);
254 IAssemblyName_GetProperty(asmname2
->name
, i
, &version2
, &size
);
256 if (version1
< version2
) return -1;
257 if (version1
> version2
) return 1;
260 /* FIXME: compare cultures */
262 size
= sizeof(token1
);
263 IAssemblyName_GetProperty(asmname1
->name
, ASM_NAME_PUBLIC_KEY_TOKEN
, token1
, &size
);
264 size
= sizeof(token2
);
265 IAssemblyName_GetProperty(asmname2
->name
, ASM_NAME_PUBLIC_KEY_TOKEN
, token2
, &size
);
267 token_to_str(token1
, token_str1
);
268 token_to_str(token2
, token_str2
);
270 if ((ret
= strcmpiW(token_str1
, token_str2
))) return ret
;
275 /* insert assembly in list preserving sort order */
276 static void insert_assembly(struct list
*assemblies
, ASMNAME
*to_insert
)
280 LIST_FOR_EACH(item
, assemblies
)
282 ASMNAME
*name
= LIST_ENTRY(item
, ASMNAME
, entry
);
284 if (compare_assembly_names(name
, to_insert
) > 0)
286 list_add_before(&name
->entry
, &to_insert
->entry
);
290 list_add_tail(assemblies
, &to_insert
->entry
);
293 static HRESULT
enum_gac_assemblies(struct list
*assemblies
, IAssemblyName
*name
,
294 int depth
, LPWSTR path
)
296 WIN32_FIND_DATAW ffd
;
298 WCHAR disp
[MAX_PATH
];
299 WCHAR asmpath
[MAX_PATH
];
305 static WCHAR parent
[MAX_PATH
];
307 static const WCHAR dot
[] = {'.',0};
308 static const WCHAR dotdot
[] = {'.','.',0};
309 static const WCHAR search_fmt
[] = {'%','s','\\','*',0};
310 static const WCHAR dblunder
[] = {'_','_',0};
311 static const WCHAR path_fmt
[] = {'%','s','\\','%','s','\\','%','s','.','d','l','l',0};
312 static const WCHAR fmt
[] = {'%','s',',',' ','V','e','r','s','i','o','n','=','%','s',',',' ',
313 'C','u','l','t','u','r','e','=','n','e','u','t','r','a','l',',',' ',
314 'P','u','b','l','i','c','K','e','y','T','o','k','e','n','=','%','s',0};
315 static const WCHAR ss_fmt
[] = {'%','s','\\','%','s',0};
318 parse_name(name
, depth
, path
, buf
);
320 sprintfW(buf
, search_fmt
, path
);
322 hfind
= FindFirstFileW(buf
, &ffd
);
323 if (hfind
== INVALID_HANDLE_VALUE
)
328 if (!lstrcmpW(ffd
.cFileName
, dot
) || !lstrcmpW(ffd
.cFileName
, dotdot
))
334 ptr
= strrchrW(buf
, '\\') + 1;
338 lstrcpyW(parent
, ptr
);
342 sprintfW(asmpath
, path_fmt
, path
, ffd
.cFileName
, parent
);
344 ptr
= strstrW(ffd
.cFileName
, dblunder
);
348 sprintfW(disp
, fmt
, parent
, ffd
.cFileName
, ptr
);
350 asmname
= HeapAlloc(GetProcessHeap(), 0, sizeof(ASMNAME
));
357 hr
= CreateAssemblyNameObject(&asmname
->name
, disp
,
358 CANOF_PARSE_DISPLAY_NAME
, NULL
);
361 HeapFree(GetProcessHeap(), 0, asmname
);
365 hr
= IAssemblyName_SetPath(asmname
->name
, asmpath
);
368 IAssemblyName_Release(asmname
->name
);
369 HeapFree(GetProcessHeap(), 0, asmname
);
373 insert_assembly(assemblies
, asmname
);
377 sprintfW(buf
, ss_fmt
, path
, ffd
.cFileName
);
378 hr
= enum_gac_assemblies(assemblies
, name
, depth
+ 1, buf
);
381 } while (FindNextFileW(hfind
, &ffd
) != 0);
387 static HRESULT
enumerate_gac(IAssemblyEnumImpl
*asmenum
, IAssemblyName
*pName
)
389 WCHAR path
[MAX_PATH
];
394 static WCHAR under32
[] = {'_','3','2',0};
395 static WCHAR msil
[] = {'_','M','S','I','L',0};
398 hr
= GetCachePath(ASM_CACHE_GAC
, buf
, &size
);
403 lstrcatW(path
, under32
);
404 hr
= enum_gac_assemblies(&asmenum
->assemblies
, pName
, 0, path
);
409 lstrcatW(path
, msil
);
410 hr
= enum_gac_assemblies(&asmenum
->assemblies
, pName
, 0, path
);
414 hr
= enum_gac_assemblies(&asmenum
->assemblies
, pName
, 0, buf
);
421 /******************************************************************
422 * CreateAssemblyEnum (FUSION.@)
424 HRESULT WINAPI
CreateAssemblyEnum(IAssemblyEnum
**pEnum
, IUnknown
*pUnkReserved
,
425 IAssemblyName
*pName
, DWORD dwFlags
, LPVOID pvReserved
)
427 IAssemblyEnumImpl
*asmenum
;
430 TRACE("(%p, %p, %p, %08x, %p)\n", pEnum
, pUnkReserved
,
431 pName
, dwFlags
, pvReserved
);
436 if (dwFlags
== 0 || dwFlags
== ASM_CACHE_ROOT
)
439 asmenum
= HeapAlloc(GetProcessHeap(), 0, sizeof(IAssemblyEnumImpl
));
441 return E_OUTOFMEMORY
;
443 asmenum
->IAssemblyEnum_iface
.lpVtbl
= &AssemblyEnumVtbl
;
445 list_init(&asmenum
->assemblies
);
447 if (dwFlags
& ASM_CACHE_GAC
)
449 hr
= enumerate_gac(asmenum
, pName
);
452 HeapFree(GetProcessHeap(), 0, asmenum
);
457 asmenum
->iter
= list_head(&asmenum
->assemblies
);
458 *pEnum
= &asmenum
->IAssemblyEnum_iface
;