Hi,
I'm porting a c++ test harness for a COM layer to c# to help extend my
knowledge of interop and c#.
I've got a quick question for which I have one answer using late
binding but it feels ugly and there must be a better way but I'm
failing to figure it out.
Anyway the problem:
I have some c++ code in which I need to know the GUID of the coclass of
a COM object.
In c++ this is trivial because of being able to use the __uuidof
function and the fact that the Coclass ID is exposed in the files that
MIDL generates.
Now when I attempt to translate this to c# using the interop assembly
that tlbimp.exe generates I run into a problem because the coclass
GUIDs are not exposed.
The best I have achieved so far is this:
IFoo TempFoo = new FooClass();
Type FooType = Type.GetTypeFromHandle(Type.GetTypeHandle( TempFoo ));
Guid FooCoclassID = FooType.GUID;
This feels so wrong and I know uses a)late binding and b) creates an
unnecessary COM object.
All the Interop resources I have read seem to define and interface or
coclass IDs by hand if they are used. This strikes me as unwanton use
of "magic numbers" which makes me stamp my feet and shout "there must
be a better way"!
Any ideas?
John
John - 02 Jun 2005 11:41 GMT
Solved the problem using the typeof operator.