471,337 Members | 1,193 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 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 7456
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Matt Frame | last post: by
18 posts views Thread by Christopher W. Douglas | last post: by
9 posts views Thread by Alexander Baranovsky | last post: by
5 posts views Thread by John A Grandy | last post: by
12 posts views Thread by =?Utf-8?B?Um9nZXIgTWFydGlu?= | last post: by
reply views Thread by rosydwin | last post: by

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.