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 / February 2007

Tip: Looking for answers? Try searching our database.

Late-bound interop confusion

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Christian Fröschlin - 19 Feb 2007 14:29 GMT
Suppose I have a native COM class A which implements an interface ITest,
and a native COM class B which features a member function taking a
parameter of type ITest. With normal COM Interop, I can call this
function in C# by explicitely casting to ITest as in

  someB.TestFunction((ITest) someA);

which also take care of the QueryInterface behind the scenes.

Suppose further that for some obscure reason a user wants to
use the library in a late-bound scenario using reflection.
I can load the assembly, locate types "typeA", "typeB",
"typeITest", and dynamically create instances of A and B
storing them in object variables "objA" and "objB".

But I don't know how to pass objA as an argument when
invoking the TestFunction on objB:

typeB.InvokeMember("TestFunction",
                   BindingFlags.InvokeMethod,
                   null,
                   objB,
                   new object[] {objA}); // ???

With the above code, the function complains that the argument
is not of type ITest. I tried something along the lines of

Guid guidITest  = typeITest.GUID;
IntPtr pUnk     = Marshal.GetIUnknownForObject(objA);
IntPtr pITest;
int HRESULT     = Marshal.QueryInterface(pUnk,ref guidITest,out pITest);
object objITest = Marshal.GetTypedObjectForIUnknown(pUnk, typeITest);

but I invariably end up with some object instance of type A (AClass),
although it looks like the QueryInterface returns meaningful pointers.

Any insights appreciated.
Christian Fröschlin - 19 Feb 2007 14:36 GMT
> Guid guidITest  = typeITest.GUID;
> IntPtr pUnk     = Marshal.GetIUnknownForObject(objA);
> IntPtr pITest;
> int HRESULT     = Marshal.QueryInterface(pUnk,ref guidITest,out pITest);
> object objITest = Marshal.GetTypedObjectForIUnknown(pUnk, typeITest);

I should note that of course the result of QueryInterface is ignored
here anyway, it was just for testing. I didn't find a method in Marshal
to get an Object from the pointer except GetTypedObjectForIUnknown,
so I used the pUnk (using pITest didn't work either).
Christian Fröschlin - 20 Feb 2007 09:20 GMT
> With the above code, the function complains that the argument
> is not of type ITest.

After some further research, it all seems to boil down to COM
not supporting late binding for non-default interfaces. Some
links to further information for anyone interested:

http://www.microsoft.com/mind/0100/Basics/Basics0100.asp
http://www.thescripts.com/forum/thread241528.html
http://www.devarticles.com/c/a/C-Sharp/Using-Late-Bound-COM-Objects/3/

Rate this thread:







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.