Groups | Blog | Home
all groups > dotnet interop > september 2005 >

dotnet interop : Com Interop QueryInterface


Robert Jordan
9/14/2005 12:00:00 AM
Hi Vishal,

[quoted text, click to view]

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/cpconcreatingwrappermanually.asp

Mattias Sjögren
9/14/2005 12:00:00 AM
[quoted text, click to view]

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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
va_acharya
9/14/2005 1:50:15 AM

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
-----------------------------------------------------------------------
va_acharya's Profile: http://www.msusenet.com/member.php?userid=509
View this thread: http://www.msusenet.com/t-187105519
va_acharya
9/14/2005 4:55:59 AM

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
-----------------------------------------------------------------------
va_acharya's Profile: http://www.msusenet.com/member.php?userid=509
View this thread: http://www.msusenet.com/t-187105519
va_acharya
9/14/2005 5:10:22 AM

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
-----------------------------------------------------------------------
va_acharya's Profile: http://www.msusenet.com/member.php?userid=509
View this thread: http://www.msusenet.com/t-187105519
Robert Jordan
9/14/2005 10:53:00 AM
Hi Vishal,

[quoted text, click to view]

How did you import the VB controls?

Robert Jordan
9/15/2005 12:00:00 AM
[quoted text, click to view]

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

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

va_acharya
9/15/2005 2:04:12 AM

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
-----------------------------------------------------------------------
va_acharya's Profile: http://www.msusenet.com/member.php?userid=509
View this thread: http://www.msusenet.com/t-187105519
AddThis Social Bookmark Button