Hi TJoker
Had a similar query in a post in one of our local
newsgroups some time back and was really a brain-teaser.
The reqmt was to know the version of the component and
also the original path. At that time i had suggested the
solution of obtaining the GUID from the assembly and then
read the reguistry and obtaining the
localserver32/inprocserver32 key value. Was not sure
whether it was the right way to do it. Now tht Mattias has
suggested the same, glad tht i hit the right way.
Attaching the code herewith. Get an Assembly object which
u can get by passing the interop assembly file path and
name to the Assembly.LoadFrom function. Then pass this
object to the function below. it will give u the path.
The complete code is as follows. If u have problems in
the code/need the complete sample project files, do mail
me at (e-mail address removed) and wou8ld be glad to help out
using System;
using System.Reflection;
using System.IO;
using Microsoft.Win32;
using System.Runtime.InteropServices;
namespace
DummyComComponentVersionFromDotNetCSharpHelperProj
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class
DummyComComponentVersionFromDotNetCSharpHelper
{
public
DummyComComponentVersionFromDotNetCSharpHelper()
{
//
// TODO: Add constructor
logic here
//
}
public String
fnGetTypeVersionNumberFromAnyOneTypeInAssembly
(System.Reflection.Assembly p_objAssembly, Boolean
p_blnGetCOMLibraryPathAlso)
{
Type[] l_objtypes;
Type l_objType;
Object l_objObject;
l_objtypes =
p_objAssembly.GetTypes();
String l_strTypeVersion="";
String l_strVersion="";
String
l_strCOMLibraryPath="";
int l_intTypeCtr=0;
int
l_intTotalTypes=l_objtypes.Length;
for(l_intTypeCtr=0;
(l_intTypeCtr<l_intTotalTypes);l_intTypeCtr++)
{
try
{
l_objType=
(Type)l_objtypes.GetValue(l_intTypeCtr);
l_objObject = p_objAssembly.CreateInstance
(l_objType.ToString());
l_strVersion = (String)l_objObject.GetType
().InvokeMember(
"Version",BindingFlags.IgnoreCase|
BindingFlags.GetProperty|BindingFlags.NonPublic|
BindingFlags.Public|BindingFlags.Static|
BindingFlags.Instance ,null,l_objObject,null);
if
(l_strVersion != null && l_strVersion.Trim() != "")
{
l_strTypeVersion = l_strVersion;
if
(p_blnGetCOMLibraryPathAlso==true)
{
GuidAttribute l_objGuidAttribute=new GuidAttribute
(null);
Object[] l_objCustomAttributes =
l_objObject.GetType().GetCustomAttributes
(l_objGuidAttribute.GetType(), true);
GuidAttribute l_objGUIDAttribute = (GuidAttribute)
l_objCustomAttributes[0];
l_strCOMLibraryPath =
fnGetCOMLibraryPathFromRegistryBasedOnProgId
(l_objGUIDAttribute.Value);
}
}
}
catch(Exception
p_objException)
{
// If the
Class is not instantiable or if the version property is
not supported
// this
exception block is reached. Ignore and continue with next
type
Console.WriteLine("Error : " +
p_objException.Message);
}
finally
{
l_objObject=null;
}
}
return "Type Version
Information : " + l_strTypeVersion +
"\n" + "COM
Library Path : " + l_strCOMLibraryPath;
}
public String
fnGetCOMLibraryPathFromRegistryBasedOnProgId(String
p_strClsId)
{
RegistryKey
l_objMainRegistryKey;
//RegistryKey
l_objProgIdkey;
RegistryKey
l_objInprocServer_LocalServer32key;
String l_strClsid = "{" +
p_strClsId + "}";
String
l_strCOMLibraryPath="";
try
{
l_objMainRegistryKey = Registry.ClassesRoot;
l_objInprocServer_LocalServer32key =
l_objMainRegistryKey.OpenSubKey("CLSID\\" + l_strClsid
+ "\\InProcServer32");
if
(l_objInprocServer_LocalServer32key ==null)
{
l_objInprocServer_LocalServer32key =
l_objMainRegistryKey.OpenSubKey("CLSID\\" + l_strClsid
+ "\\LocalServer32");
if
(l_objInprocServer_LocalServer32key ==null)
{
return "";
}
}
l_strCOMLibraryPath = (String)
(l_objInprocServer_LocalServer32key.GetValue
(l_objInprocServer_LocalServer32key.GetValueNames()[0]));
return
l_strCOMLibraryPath;
}
catch(Exception
p_objException)
{
Console.WriteLine
("Error : " + p_objException.Message);
}
return "";
}
}
}
hth
regards,
sr