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 2006

Tip: Looking for answers? Try searching our database.

COM Factory for managed objects?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
arunprakashb@gmail.com - 21 Sep 2006 22:53 GMT
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.

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.