当前位置:首页 > Windows程序 > 正文

GAC API Interface

2023-05-23 Windows程序

标签:

Introduction

The GAC (aka Global Assembly Cache) is the central repository for assemblies installed on a Windows machine. It provides a uniform, versioned and safe access of assemblies by their strong assembly name. This article shows the official way how to use this GAC from your application.

The internal structure of the GAC is not documented in the MSDN library but the filesystem structure (e.g. C:\WINDOWS\assembly) seems simple enough to scan it. There is however an undocumented COM API to access the GAC the (sort of) official way. This is called the Fusion API because it is implemented by fusion.dll.

Background

The basis of this work is the unofficial documentation of the GAC API in the Microsoft KB article #317540 DOC: Global Assembly Cache (GAC) APIs Are Not Documented in the .NET Framework Software Development Kit (SDK) Documentation.This article explains in detail the use of the GAC API as implemented in fusion.dll. This DLL contains a few API calls to create COM interfaces to various GAC related functionality like adding or removing assemblies, enumerating installed assemblies and the like. What we did here is to simply implement this API including the COM interfaces in C# and adorn the code with the documentation snippets from the mentioned KB article.

There is another way to use this API and this way depends on the implementation of the GAC API inside the .NET framework. Obviously the .NET framework has full access to the GAC for various reasons, even though this access is not documented and flagged internal. The CodeProject article Article "Undocumented Fusion" by John Renauddescribes nicely how to use this internal implementation through reflection. This article is also insightful and describes some aspects of the API (like the history for instance) that is left undocumented in the KB article. We only give the naked implementation of the fusion.dll API and we recommend Johns article as extra background reading and for examples.

Using the code

The first step should be to read the Microsoft KB article linked above. We added almost all of this documentation as comments in our source for reference. This should give you an idea what part of the API you actually need. The examples we give here in this article all focus on the task of scanning the GAC for installed assemblies. As a side note, you should be familiar with the COM marshalling. See class

System.Runtime.InteropServices.Marshal and System.Runtime.InteropServices.IntPtr.

The following bits of code focus on how to scan the GAC. We start by creating the enumeration interface.

IAssemblyEnum ae = AssemblyCache.CreateGACEnum();

This wraps the basic call to CreateAssemblyEnum. On this interface we can now enumerate GetNextAssemblyuntil the call return a COM error.

IAssemblyName an; AssemblyName name; while (AssemblyCache.GetNextAssembly(ae, out an) == 0) { name = GetAssemblyName(an); .... }

The method GetAssemblyName (not part of the downloadable sources) uses several IAssemblyName methods to retrieve all necessary values to build a .NET-AssemblyName instance. We will show them in turn.

private AssemblyName GetAssemblyName(IAssemblyName nameRef) { AssemblyName name = new AssemblyName(); name.Name = AssemblyCache.GetName(nameRef); name.Version = AssemblyCache.GetVersion(nameRef); name.CultureInfo = AssemblyCache.GetCulture(nameRef); name.SetPublicKeyToken(AssemblyCache.GetPublicKeyToken(nameRef)); return name; }

GetName is an example how to obtain strings from the COM-API. If you are not familiar with the COM marshaling of C#, you might wonder about the use of the StringBuilder. It is marshaled as a changeable char*. Using ref string instead will lead to marshaling errors since string is immutable.

public static String GetName(IAssemblyName name) { uint bufferSize = 255; StringBuilder buffer = new StringBuilder((int) bufferSize); name.GetName(ref bufferSize, buffer); return buffer.ToString(); }

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/71596.html