>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.
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...
>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 );