Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / July 2003

Tip: Looking for answers? Try searching our database.

Determining COM object interface at runtime

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Beat Nideröst - 24 Jul 2003 14:28 GMT
Hello,

I've got a COM object that is being returned to me from a
COM method invocation. I don't know the type / COM
interfaces of the object returned to me, since this
varies between invocations. However, I would like to
determine the interfaces of the returned COM object, and
then cast the object at runtime to the correct Interface.
I solved the problem by just trying to cast to the
different interfaces possible, and checking if the cast
is successfull. There must be a better way. Does anybody
know how?

Regards, Beat Nideröst

Assembly Interop.InDesign, generated in Visual
Studio .NET 2003 from a COM typelibrary, contains
namespace InDesign with the COM Interfaces:

InDesign.Objects foundObjects = (InDesign.Objects)
textFrame.Search("", true, true, ""); // textFrame is a
COM Object exposing method Search()

foreach (System.MarshalByRefObject obj in foundObjects)
{
 InDesign.TextWord textWord = obj as InDesign.TextWord;
 if (null != textWord)
   textWord.TextContents = Value;
 else
 {
   InDesign.TextStyleRange styleRange = obj as
InDesign.TextStyleRange;
   if (null != styleRange)
   {
... etc. ...
   }
 }
}
Mattias Sj?gren - 26 Jul 2003 23:04 GMT
Beat,

>However, I would like to
>determine the interfaces of the returned COM object, and
[quoted text clipped - 3 lines]
>is successfull. There must be a better way. Does anybody
>know how?

No, there isn't really a better way. Asking if an object supports a
certain interface using IUnknown::QueryInterface (which is what the
runtime does under the hood when you attempt to cast or use the is
operator) is one of the fundamental properties of COM.

You could perhaps try to locate a typelib for the component, and then
use the ITypeLib/ITypeInfo APIs to determine which interfaces the
coclass is listing as supported in the typelib. Or do the same thing
using Reflection by calling Type.GetInterfaces() for the coclass type
in the interop assembly. But there's no guarantee that such a list
would be complete, so casting is the only way to know for sure.

Mattias

Signature

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

Beat Nideröst - 29 Jul 2003 08:46 GMT
Thanks for the information.

I understood the part about the IUnknown:QueryInterface,
since I used this before in C++. What I didn't understand
was the part about the typelib. Do you suggest to iterate
over the list of all the interfaces exposed in the type
library, and try to cast to these interfaces?

I know that my COM component is also used from VB
scripting (because all the samples in the documentation
are VB script!) The samples don't care what interface the
returned object exposes, they just call a method
named "GetTextContents()" on the object. Apparently, this
is a valid method for all returned object types.

Is there a way  to simulate this in C#.NET? Just look up
the method named "GetTextContents()" and then call the
method, without caring about interfaces? Is this
something I would use PInvoke() for? And is this related
to IDispatch()?

Regards, Beat Nideröst

>You could perhaps try to locate a typelib for the component, and then
>use the ITypeLib/ITypeInfo APIs to determine which interfaces the
[quoted text clipped - 4 lines]
>
>Mattias
Mattias Sj?gren - 29 Jul 2003 23:25 GMT
Beat,

>Do you suggest to iterate
>over the list of all the interfaces exposed in the type
>library, and try to cast to these interfaces?

Exactly, but it sounds like that isn't needed in this case.

>Is there a way  to simulate this in C#.NET? Just look up
>the method named "GetTextContents()" and then call the
>method, without caring about interfaces? Is this
>something I would use PInvoke() for? And is this related
>to IDispatch()?

Yes you can do this in C# (though it's easier in VB.NET), and yes it's
related to IDispatch. Late bound COM calls are done with
Type.InvokeMember in C#, something like

object o = TheComObject;
object returnVal = o.GetType().InvokeMember(
 "GetTextContents", BindingFlags.InvokeMethod, null, o,
 new object[] {...any method arguments here...} );

Mattias

Signature

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.