Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Obtaining the class name of a late-bound COM object in C#

139 views
Skip to first unread message

Mike Timms

unread,
Feb 13, 2003, 4:40:47 PM2/13/03
to
In VB.NET I can get the class name of a dynamically created loaded COM object ( i.e. no Interop Assembly reference used)  using the VB TypeName function as in the following code:
 
     Dim t As System.Type = Type.GetTypeFromProgID("MyComponent.Class1")
     Dim class1 As Object = Activator.CreateInstance(t)
 
    .........
    
     Dim name As String = TypeName(class1)
  
Does anybody have an idea of how this could be achieved in C#?
 
thanks,
 
Mike

Mattias Sjögren

unread,
Feb 16, 2003, 10:28:04 AM2/16/03
to
Mike,

>Does anybody have an idea of how this could be achieved in C#?

[ComImport, Guid("B196B283-BAB4-101A-B69C-00AA00341D07"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IProvideClassInfo
{
UCOMITypeInfo GetClassInfo();
}

[ComImport, Guid("00020400-0000-0000-C000-000000000046"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IDispatch
{
uint GetTypeInfoCount();
UCOMITypeInfo GetTypeInfo(uint iTInfo, int lcid);
// GetIDsOfNames and Invoke left out
}


static string TypeName(object comObject)
{
if ( !comObject.GetType().IsCOMObject )
throw new ArgumentException( "Not a COM object.", "comObject" );

UCOMITypeInfo ti = null;
IProvideClassInfo pci = comObject as IProvideClassInfo;
if ( pci != null )
ti = pci.GetClassInfo();
else {
IDispatch d = comObject as IDispatch;
if ( d != null && d.GetTypeInfoCount() > 0 )
ti = d.GetTypeInfo( 0, 0x409 );
}

if ( ti != null )
return Marshal.GetTypeInfoName( ti );
else
return null;
}

Mattias

===
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Mike Timms

unread,
Feb 17, 2003, 10:29:15 AM2/17/03
to
Mattias,

Thanks for this.

My temporary workaround was to import the Visual Basic framework class into
my C# project and wrap the VB.NET TypeName method as in:

using Microsoft.VisualBasic;
........

public static string TypeName( object source )
{
return Microsoft.VisualBasic.Information.TypeName( source );
}

I compared using this approach against the C# code with the Excel 9.0
library and the only difference I'm seeing is that the VB method returns the
coclass name (e.g. Application) while the C# code returns the interface
name (e.g. _Application).

- Mike


"Mattias Sjögren" <mattias.don...@mvps.org> wrote in message
news:OYgk9$c1CHA...@TK2MSFTNGP11.phx.gbl...

Mattias Sjögren

unread,
Feb 23, 2003, 6:52:20 PM2/23/03
to
Mike,

>My temporary workaround was to import the Visual Basic framework class into
>my C# project and wrap the VB.NET TypeName method as in:

That works too. :-)


>I compared using this approach against the C# code with the Excel 9.0
>library and the only difference I'm seeing is that the VB method returns the
>coclass name (e.g. Application) while the C# code returns the interface
>name (e.g. _Application).

Right, the VB.NET function is simply hardcoded to remove an initial
underscore if it's there. You can do that too if you like

string name = return Marshal.GetTypeInfoName( ti );
if ( name.Length > 0 && name[0] == '_' )
name = name.Substring( 1 );

0 new messages