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
>--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