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 / September 2005

Tip: Looking for answers? Try searching our database.

Com Interop QueryInterface

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
va_acharya - 14 Sep 2005 07:50 GMT
Hi
I am vishal
I have imported some VB UserControls in my C# application throug
Interop and now i want to make a call to an interface function whic
has been implemented on all the UserControls. For that i am usin
following code

--IntPtr unknownIntPtr = Marshal.GetIUnknownForObject(cnt); //cnt i
the com component
--System.Guid g= new Guid("191BC215-C9C0-4C14-866D-A63A2FBDADFD")
//guid of the interface ..........  
--IntPtr tempIntPtr;
--Marshal.QueryInterface(unknownIntPtr,ref g,out tempIntPtr); /
passing pointer of IunKnown interface of the com component

But it is not working fine ... i mean i am not getting the respectiv
interface pointer.....

Plz help
Thanks in Advance!

--
va_achary
Robert Jordan - 14 Sep 2005 09:53 GMT
Hi Vishal,

> I have imported some VB UserControls in my C# application through
> Interop and now i want to make a call to an interface function which
> has been implemented on all the UserControls. For that i am using
> following code

How did you import the VB controls?

Rob
va_acharya - 14 Sep 2005 10:55 GMT
Hi Rob,

thanks for prompt reply

i added the ocx in IDE toolbox and drag dropped the control on m
winform and then tried to fetch the Interface with the following code

IntPtr unknownIntPtr = Marshal.GetIUnknownForObject(ocxObject);
System.Guid g= new Guid("E5F53966-28F0-45CA-ADE6-3C5BD72B5A7C");  
IntPtr tempIntPtr;
Marshal.QueryInterface(unknownIntPtr,ref g,out tempIntPtr);

but here i am not getting the interface pointer in tempIntPtr variabl

--
va_achary
Robert Jordan - 14 Sep 2005 16:37 GMT
Hi Vishal,

> i added the ocx in IDE toolbox and drag dropped the control on my
> winform and then tried to fetch the Interface with the following code
[quoted text clipped - 3 lines]
> IntPtr tempIntPtr;
> Marshal.QueryInterface(unknownIntPtr,ref g,out tempIntPtr);

That's not the right way to do that. Take a look with
the Object Browser at your imported Interop assembly.
Do you see the interface there? If it's already
converted you can just cast to this interface:

(ISomeName)ocxObject

If the interface is not there, you have to declare
a wrapper for it using a couple of COM interop attributes:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cp
concreatingwrappermanually.asp


Rob
va_acharya - 14 Sep 2005 11:10 GMT
i sent a reply to you before, but it is not visible in thread so i a
writing you again.

i added VB ocx in IDE toolbox through com components window, and dra
dropped that ocx on my win form.
Then i passed that ocx object to the following function

IntPtr unknownIntPtr = Marshal.GetIUnknownForObject(ocxObject);
System.Guid g= new Guid("E5F53966-28F0-45CA-ADE6-3C5BD72B5A7C");  
IntPtr tempIntPtr;
Marshal.QueryInterface(unknownIntPtr,ref g,out tempIntPtr);

but here i am getting value "0" in tempIntPt

--
va_achary
Mattias Sjögren - 14 Sep 2005 16:47 GMT
>--IntPtr unknownIntPtr = Marshal.GetIUnknownForObject(cnt); //cnt is
>the com component

If cnt is a control derived from AxHost, you should use cnt.GetOcx()
to get to the real ActiveX object.

But why are you using GetIUnknownForObejct and explicit
QueryInterface? What are you going to do with the returned interface
poitner? Why not just cast to the interface?

Mattias

Signature

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

va_acharya - 15 Sep 2005 08:04 GMT
Hi Mattias

thanks for the solutions, this was the only mistake i was making in m

code. Actually i have some user controls in VB and on each and ever
control there is an interface is implemented.That interface contains
functions and all of them are declared in usercontrol as private. S
when we load those control from VB application we do something lik
this --
***IdlBaseInterface idl=ActiveControl idl.Show()*****now in vb it work
because it knows it's parent is IdlBaseInterface, but in case of .NE
AxHost is the base of all the imported UserControls so whenever i typ
cast them
*****IdlBaseInterface tempIdl=(IdlBaseInterface) someOcx**** i get a
error, Invalid type cast.

So first i extract Iunknown interface from the control and then throug
query interface i got the IdlBaseInterface pointer of the control an
after that i called this method

IdlBaseInterface id
=(IdlBaseInterface)Marshal.GetObjectForIUnknown(tempIntPtr);

now i can call that show method like this
idl.Show()

well still you think that there can be some other solution plz let m
know

Cheers!!
Visha

--
va_achary
Robert Jordan - 15 Sep 2005 17:08 GMT
> ***IdlBaseInterface idl=ActiveControl idl.Show()*****now in vb it works
> because it knows it's parent is IdlBaseInterface, but in case of .NET
> AxHost is the base of all the imported UserControls so whenever i type
> cast them
> *****IdlBaseInterface tempIdl=(IdlBaseInterface) someOcx**** i get an
> error, Invalid type cast.

*not* someOcx. someOcx.GetOcx() is your friend.

IdlBaseInterface tempIdl=(IdlBaseInterface) someOcx.GetOcx();

Rob

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.