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 / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Frustrating COM Problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
oddball.bfi@gmail.com - 25 Feb 2008 12:17 GMT
Hi there.

I need help retrieving the type of a COM object at run-time.  All the
examples I've found assume you know the object type in advance - I
need some way to get a list of Interfaces a COM object implements.

It's driving me mad - help!
nano2k - 25 Feb 2008 12:22 GMT
Try using Type.GetInterfaces()

On 25 Feb, 14:17, oddball....@gmail.com wrote:
> Hi there.
>
[quoted text clipped - 3 lines]
>
> It's driving me mad - help!
Richard Blewett - 25 Feb 2008 12:25 GMT
> Hi there.
>
[quoted text clipped - 3 lines]
>
> It's driving me mad - help!

Now I'm afraid thats a tough one as this doesn't need to be documented. The
COM object *may* have this information in the typelibrary. However, the only
way to truely know is to call QueryInterface (cast using "as") on every
interface it could be. This is essentially what oleview.exe does - calls QI
on the object for each interface in the registry

Signature

Regards

Richard Blewett
DevelopMentor
http://www.dotnetconsult.co.uk/weblog2

oddball.bfi@gmail.com - 25 Feb 2008 12:58 GMT
> <oddball....@gmail.com> wrote in message
>
[quoted text clipped - 19 lines]
> Richard Blewett
> DevelopMentorhttp://www.dotnetconsult.co.uk/weblog2

Oh dear god - that's hardly something I can do every time I run the
software...

I'm looking for any solutions that let me dodge this - anything at
all.  Is that how the Object Browser gets it's info too?
Richard Blewett - 25 Feb 2008 13:10 GMT
>> <oddball....@gmail.com> wrote in message
>>
[quoted text clipped - 28 lines]
> I'm looking for any solutions that let me dodge this - anything at
> all.  Is that how the Object Browser gets it's info too?

Well the issue is how accurate does this have to be?

You could use the typelib (which is what I'm guessing Type.GetInterfaces
will give you - although I might be wrong). Or you could run QI against the
COM object once and store the results somewhere. Do you care about a subset
of interfaces or do you really care about all of them (such as IMarshal,
IMarshal2, IPersistPropertyBag, etc)?

Signature

Regards

Richard Blewett
DevelopMentor
http://www.dotnetconsult.co.uk/weblog2

oddball.bfi@gmail.com - 25 Feb 2008 13:50 GMT
> <oddball....@gmail.com> wrote in message
>
[quoted text clipped - 46 lines]
> Richard Blewett
> DevelopMentorhttp://www.dotnetconsult.co.uk/weblog2

I basicaly need to identify the object type so I can send it to a
handler.  I only care about one or two specific interfaces, just
enough to pass the object to the right handler which runs a specific
cast and does it's jiggery pokery.

Type at = Type.GetType(wca.AddType); // Add type is a string name of a
COM interface.

Why is this returning NULL!!!!

Join me in the pub this evening and I'll tell you all about third-
party libraries that just throw a bunch of untyped objects at you and
expect you to deal.
Richard Blewett - 25 Feb 2008 14:20 GMT
>> <oddball....@gmail.com> wrote in message
>>
[quoted text clipped - 67 lines]
> party libraries that just throw a bunch of untyped objects at you and
> expect you to deal.

OK, so with the way you have called this method it is expecting the
interface to live within your application (assembly). If you want this to
work you will have to add the assembly name on to the string. Suppose the
assembly housing the Runtime Callable Wrapper (I assume you have already
created an RCW) is called MyCOMStuff.dll and has been signed with a public
key token, then you need to pass in something like

wca.AddType, MyCOMStuff, Version=0.0.0.0, Culture=neutral,
PublicKeyToken=<token used for signing>

This will get you the type object for the interface. The question is, is
this really what you want? Aren't you trying to find out if a specific
object implements an interface or have I misunderstood?

Regards

Richard Blewett
DevelopMentor
http://www.dotnetconsult.co.uk/weblog2
Roger Frost - 25 Feb 2008 14:23 GMT
> I basicaly need to identify the object type so I can send it to a
> handler.  I only care about one or two specific interfaces, just
[quoted text clipped - 5 lines]
>
> Why is this returning NULL!!!!

I don't know much about what your trying to do....but GetType() accepts a
string parameter, so maybe it should be:

Type at = Type.GetType("wca.AddType");
Roger Frost - 25 Feb 2008 14:37 GMT
Wait, according to your comment it is a string argument....sorry.

Signature

Roger Frost
"Logic Is Syntax Independent"

>> I basicaly need to identify the object type so I can send it to a
>> handler.  I only care about one or two specific interfaces, just
[quoted text clipped - 10 lines]
>
> Type at = Type.GetType("wca.AddType");
Willy Denoyette [MVP] - 25 Feb 2008 17:04 GMT
>> <oddball....@gmail.com> wrote in message
>>
[quoted text clipped - 67 lines]
> party libraries that just throw a bunch of untyped objects at you and
> expect you to deal.

What exactly do you mean with "string name of a COM interface" here?

Type at = Type.GetType(wca.AddType); // Add type is a string name of a COM
interface.

what does wca.AddType contain?

Interfaces are identified using UUID's, so you need to specify the UUID of
the interface (the IID) you are looking for in a call to QI.
Herewith a small sample calling a method on a native COM object using late
binding.

           string progId = "SimpleCom.Bird";
           Type t = Type.GetTypeFromProgID(progId);
           if (t == null)
           {
               throw new Exception("Invalid ProgID.");
           }
           Console.WriteLine("GUID for ProgID RemServer.Server is {0}.",
t.GUID);
           object o = Activator.CreateInstance(t);    // create an instance
of this server
           IntPtr pUnkn = Marshal.GetIUnknownForObject(o);    // Get the
IUnknow pointer
           Guid IID = new Guid("0938B479-ED9A-4997-B780-85AF420E01FF"); //
This is the Interface I need
           IntPtr ppv;
           int hResult = Marshal.QueryInterface(pUnkn, ref IID, out ppv);
// Does above Interface exists?
           if(hResult <  0)
           { throw ...            }
           else
           {
               // success, use interface
               object bird = Marshal.GetObjectForIUnknown(ppv);
               //late bound call
               bird .GetType().InvokeMember("Fly",
BindingFlags.InvokeMethod, null, bird , new Object[] {"To Somewhere"});

               ...

Willy.
Oddball - 25 Feb 2008 17:37 GMT
On 25 Feb, 17:04, "Willy Denoyette [MVP]" <willy.denoye...@telenet.be>
wrote:
> <oddball....@gmail.com> wrote in message
>
[quoted text clipped - 114 lines]
>
> Willy.

It's the AQN of the Type in the Interop assembly.
Willy Denoyette [MVP] - 25 Feb 2008 18:43 GMT
> On 25 Feb, 17:04, "Willy Denoyette [MVP]" <willy.denoye...@telenet.be>
> wrote:
[quoted text clipped - 129 lines]
>
> It's the AQN of the Type in the Interop assembly.

If you have imported the IA, then you can simply cast the interface you got
from an instantiation call to any other interface implemented by the
component.

           ...
           SomeNamespace.Animal animal = new SomeNamespace.AnimalClass();
           // cast animal to wanted ITF, this performs a QueryInterface
under the covers
           SomeNamespace.IBird bird = animal as SomeNamespace.IBird;
           if(bird != null)
               bird.Fly();
           else
           {
           SomeNamespace.IDog dog as SomeNamespace.IDog;
           if(dog != null)
               dog.Bark();
           ...

Willy.

Willy.

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.