Hi All,
I am trying to do this and am stuck in one place. If someone can help
me it would be great. I have a managed object exposed to COM via
regasm.
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IPanel123
....
[Serializable]
[Guid("196B6B65-371C-49d6-BF42-814AF6171107"),
ComVisible(true),
ProgId("TestingRegistry.UserControl123")]
[ClassInterface(ClassInterfaceType.None)]
public partial class UserControl123 : UserControl, IPanel123
Now this object has to be instantiated from a COM client that has a CLR
hosting component (the CLR hosting component is what we control, not
the COM client. So we cannot control how the COM client creates an
instance of UserControl123) . When we regasm a .Net assembly, by
default mscoree.dll sits as the InProcServer32 and it exposes the class
factory to create instances of the managed object. Now we want to
mimick that because mscoree.dll always creates the managed objects in
default application domain. (we dont want to do that because our Custom
CLR hosting component creates a new app domain for security reasons).
So i wrote a class factory in C++ that would be able to create managed
objects in the same application domain as created by our custom clr
hosting component. Here is the create instance method implementation
that i tried.
HRESULT __stdcall ControlClassFactory::CreateInstance(IUnknown*
pUnknownOuter,
const IID& iid,
void** ppv)
{
/ * Create the managed class using CCLRLoader which is
the CLR hosting component's singleton instance */
HRESULT hr;
mscorlib::_ObjectHandle *pObjHandle;
/* Creates the managed object in the same application
domain as the CLR hosting component */
hr = CCLRLoader::TheInstance()->CreateInstance( szAddInAssemblyName,
szConnectClassName, iid, &pObjHandle);
CComVariant VntUnwrapped;
hr = pObjHandle->Unwrap(&VntUnwrapped);
*ppv = (void*)VntUnwrapped.punkVal;
/*Instead of this line i have already tried
following, with the same result
hr = VntUnwrapped.punkVal->QueryInterface( iid, ppv
);
hr = VntUnwrapped.pdispVal->QueryInterface( iid, ppv
); */
return hr;
}
The problem that i am facing is this. When i try to type cast the
instance of this object that is created and returned to the managed
world as
// This line is in the managed code and returns the managed object
created
object ctr = userForms.Controls.Item( "Contrl123" )
IPanel123 pnl = (IPanel123)ctr;
At this line am getting exception. Invalid Type Cast Exception. Unable
to type cast because the query interface with the GUID failed with the
error E_NOINTERFACE.
Any pointers is appreciated.
arunprakashb@gmail.com - 21 Sep 2006 23:05 GMT
I forgot to mention a key point. As soon as i create the instance of
the object in the class factory, i am able to query for the interface
(using its GUID) and it returns S_OK. However, when i use the same
instance in .Net and do a QueryInterface using Marshal class I get the
E_NOINTERFACE there.
> Hi All,
> I am trying to do this and am stuck in one place. If someone can help
[quoted text clipped - 73 lines]
>
> Any pointers is appreciated.