473,395 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Determine if assembly is installed in the GAC

I'm wondering if there is an easy way to programmatically determine if an
assembly is installed in the GAC.

This would be similar to our ability to easily determine if a file exists
(File.Exists(path)) - but for an assembly, of a particular version, etc in
the GAC.

I have googled this and failed to find anything useful.

Thanks

Jun 27 '08 #1
7 7602
"Cramer" <A@B.comwrote in message
news:up**************@TK2MSFTNGP02.phx.gbl...
I'm wondering if there is an easy way to programmatically determine if an
assembly is installed in the GAC.

This would be similar to our ability to easily determine if a file exists
(File.Exists(path)) - but for an assembly, of a particular version, etc in
the GAC.

I have googled this and failed to find anything useful.

Thanks

You'll have to cal into the fusion API's (native code API's and COM).
Here is a completes sample that illustrates how one can retrieve the path of
an assembly in the GAC.

// Note that this requires V2 of the framework!!!!.
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace GacStuff
{
internal class GacApi
{
[DllImport("fusion.dll")]
internal static extern IntPtr CreateAssemblyCache(
out IAssemblyCache ppAsmCache,
int reserved);

}
// GAC Interfaces - IAssemblyCache. As a sample, non used vtable entries
declared as dummy.
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown ),
Guid("e707dcde-d1cd-11d2-bab9-00c04f8eceae")]
internal interface IAssemblyCache
{
int Dummy1();
[PreserveSig()]
IntPtr QueryAssemblyInfo(
int flags,
[MarshalAs(UnmanagedType.LPWStr)]
String assemblyName,
ref ASSEMBLY_INFO assemblyInfo);
int Dummy2();
int Dummy3();
int Dummy4();
}
[StructLayout(LayoutKind.Sequential)]
internal struct ASSEMBLY_INFO
{
public int cbAssemblyInfo;
public int assemblyFlags;
public long assemblySizeInKB;
[MarshalAs(UnmanagedType.LPWStr)]
public String currentAssemblyPath;
public int cchBuf;
}

class Program
{
static void Main()
{
try
{
Console.WriteLine(QueryAssemblyInfo("System"));
}
catch(System.IO.FileNotFoundException e)
{
Console.WriteLine(e.Message);
}
}
// If assemblyName is not fully qualified, a random matching may be
returned!!!!
public static String QueryAssemblyInfo(String assemblyName)
{
ASSEMBLY_INFO assembyInfo = new ASSEMBLY_INFO ();
assembyInfo.cchBuf = 512;
assembyInfo.currentAssemblyPath = new String('\0',
assembyInfo.cchBuf) ;
IAssemblyCache assemblyCache = null;
// Get IAssemblyCache pointer
IntPtr hr = GacApi.CreateAssemblyCache(out assemblyCache, 0);
if (hr == IntPtr.Zero)
{
hr = assemblyCache.QueryAssemblyInfo(1, assemblyName, ref
assembyInfo);
if(hr != IntPtr.Zero)
Marshal.ThrowExceptionForHR(hr.ToInt32());
}
else
Marshal.ThrowExceptionForHR(hr.ToInt32());
return assembyInfo.currentAssemblyPath;
}
}
}

Willy.

Jun 27 '08 #2
Thanks for the code! I never would have had the time to come up with this
and instead documented assumptions my code would have instead have made
about the existance of the assembly in the GAC. Now it will be able to test
and report on those assumptions when invalid, before choking. I'll give it a
whirl.

-Cramer
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:Oe**************@TK2MSFTNGP04.phx.gbl...
"Cramer" <A@B.comwrote in message
news:up**************@TK2MSFTNGP02.phx.gbl...
>I'm wondering if there is an easy way to programmatically determine if an
assembly is installed in the GAC.

This would be similar to our ability to easily determine if a file exists
(File.Exists(path)) - but for an assembly, of a particular version, etc
in the GAC.

I have googled this and failed to find anything useful.

Thanks


You'll have to cal into the fusion API's (native code API's and COM).
Here is a completes sample that illustrates how one can retrieve the path
of an assembly in the GAC.

// Note that this requires V2 of the framework!!!!.
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace GacStuff
{
internal class GacApi
{
[DllImport("fusion.dll")]
internal static extern IntPtr CreateAssemblyCache(
out IAssemblyCache ppAsmCache,
int reserved);

}
// GAC Interfaces - IAssemblyCache. As a sample, non used vtable
entries declared as dummy.
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown ),
Guid("e707dcde-d1cd-11d2-bab9-00c04f8eceae")]
internal interface IAssemblyCache
{
int Dummy1();
[PreserveSig()]
IntPtr QueryAssemblyInfo(
int flags,
[MarshalAs(UnmanagedType.LPWStr)]
String assemblyName,
ref ASSEMBLY_INFO assemblyInfo);
int Dummy2();
int Dummy3();
int Dummy4();
}
[StructLayout(LayoutKind.Sequential)]
internal struct ASSEMBLY_INFO
{
public int cbAssemblyInfo;
public int assemblyFlags;
public long assemblySizeInKB;
[MarshalAs(UnmanagedType.LPWStr)]
public String currentAssemblyPath;
public int cchBuf;
}

class Program
{
static void Main()
{
try
{
Console.WriteLine(QueryAssemblyInfo("System"));
}
catch(System.IO.FileNotFoundException e)
{
Console.WriteLine(e.Message);
}
}
// If assemblyName is not fully qualified, a random matching may be
returned!!!!
public static String QueryAssemblyInfo(String assemblyName)
{
ASSEMBLY_INFO assembyInfo = new ASSEMBLY_INFO ();
assembyInfo.cchBuf = 512;
assembyInfo.currentAssemblyPath = new String('\0',
assembyInfo.cchBuf) ;
IAssemblyCache assemblyCache = null;
// Get IAssemblyCache pointer
IntPtr hr = GacApi.CreateAssemblyCache(out assemblyCache, 0);
if (hr == IntPtr.Zero)
{
hr = assemblyCache.QueryAssemblyInfo(1, assemblyName, ref
assembyInfo);
if(hr != IntPtr.Zero)
Marshal.ThrowExceptionForHR(hr.ToInt32());
}
else
Marshal.ThrowExceptionForHR(hr.ToInt32());
return assembyInfo.currentAssemblyPath;
}
}
}

Willy.



Jun 27 '08 #3
On Apr 24, 12:32*pm, "Cramer" <A...@B.comwrote:
I'm wondering if there is an easy way to programmatically determine if an
assembly is installed in the GAC.

This would be similar to our ability to easily determine if a file exists
(File.Exists(path)) - but for an assembly, of a particular version, etc in
the GAC.

I have googled this and failed to find anything useful.

Thanks
Hi,

IT should be simple, first you need a reference to the assembly (you
can use Assembly.GetAssembly(typeof( XXXX ) ); where XXXX is defined
in that assembly)
Then using Assembly.Location should be enough.
Jun 27 '08 #4
"Ignacio Machin ( .NET/ C# MVP )" <ig************@gmail.comwrote in
message
news:e6**********************************@y38g2000 hsy.googlegroups.com...
On Apr 24, 12:32 pm, "Cramer" <A...@B.comwrote:
I'm wondering if there is an easy way to programmatically determine if an
assembly is installed in the GAC.

This would be similar to our ability to easily determine if a file exists
(File.Exists(path)) - but for an assembly, of a particular version, etc in
the GAC.

I have googled this and failed to find anything useful.

Thanks
Hi,

IT should be simple, first you need a reference to the assembly (you
can use Assembly.GetAssembly(typeof( XXXX ) ); where XXXX is defined
in that assembly)
Then using Assembly.Location should be enough.
But this won't return the GAC location, nor will it tell you whether the
assembly is actually stored in the GAC.

Willy.

Jun 27 '08 #5
On Apr 24, 12:32 pm, "Cramer" <A...@B.comwrote:
I'm wondering if there is an easy way to programmatically determine if an
assembly is installed in the GAC.

This would be similar to our ability to easily determine if a file exists
(File.Exists(path)) - but for an assembly, of a particular version, etc in
the GAC.

I have googled this and failed to find anything useful.

Thanks
Try Assembly.LoadWithPartialName("SomeName");

if returned value is null then it is not in gac..
if you get a non null value , then check the GlobalAssemblyCache.

that mite work..
Jun 27 '08 #6
RE:
IT should be simple, first you need a reference to the assembly (you
can use Assembly.GetAssembly(typeof( XXXX ) ); where XXXX is defined
in that assembly)
Then using Assembly.Location should be enough.

I should have been more specific. I want to know if an assembly is
installed in the GAC *before* attempting to load it. This would be akin to
using File.Exists() before attempting to open a file.

Your approach assumes that the assembly is already loaded, which it is not
in my case. In my case I want to determine if an assembly of a particular
version etc is in the GAC, then load it if it's there, or write the fact to
a log if it's not in the GAC.

-Cramer

Jun 27 '08 #7
On Apr 24, 5:15 pm, "Cramer" <A...@B.comwrote:
RE:
IT should be simple, first you need a reference to the assembly (you
can use Assembly.GetAssembly(typeof( XXXX ) ); where XXXX is defined
in that assembly)
Then using Assembly.Location should be enough.

I should have been more specific. I want to know if an assembly is
installed in the GAC *before* attempting to load it. This would be akin to
using File.Exists() before attempting to open a file.

Your approach assumes that the assembly is already loaded, which it is not
in my case. In my case I want to determine if an assembly of a particular
version etc is in the GAC, then load it if it's there, or write the fact to
a log if it's not in the GAC.

-Cramer
Try Assembly.LoadWithPartialName("SomeName");

if returned value is null then it is not in gac..
if you get a non null value , then check the GlobalAssemblyCache.

that mite work..
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Matt Frame | last post by:
I wrote a small assembly to test the process of placing it into the Global Assembly Cache but now I cannot remove it. When I hi-light and click delete I get the following: Assembly 'Assembly...
18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
1
by: Scott | last post by:
Is there any way to determine through code if a given assembly is part of the .NET Framework? Any special attributes, etc? I saw one suggestion about checking the key the assembly was signed with,...
9
by: Alexander Baranovsky | last post by:
Hello friends, How I can determine full path of Microsoft.Basic.dll? Thanks, Alexander
2
by: jtyner | last post by:
I am trying to get QFE (Quick Fix Engineering) working with an assembly installed in the GAC. I have two books that claim if two different version of the assembly are installed in the GAC -AND-...
1
by: Tim F | last post by:
Problem: I'm receiving the error "File or assembly name XXXXX or one of its dependencies, was not found." when trying to execute code in an assmebly that has both a strong-name and has been...
5
by: John A Grandy | last post by:
How to use the .NET Reflector to determine which .NET version and assembly was compiled in ?
12
by: =?Utf-8?B?Um9nZXIgTWFydGlu?= | last post by:
I am writing a web app to be widely distributed where I do not know the installed .NET Framework version. I want to take advantage of some .NET 3.0 classes if they are installed, but gracefully...
8
by: Joe Withawk | last post by:
I have a solution consisting of a c# project as win application and a c++ project as classlibrary. Both are .net 2.0 The classlibrary handles some loading of quicktime movies, but that should not...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.