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 / CLR / December 2003

Tip: Looking for answers? Try searching our database.

Hosting the CLR

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David Battams - 05 Dec 2003 14:26 GMT
I posted this message on microsoft.public.dotnet.framework.windowsforms
since the original thread related to an error that results when trying to
show a form when I create it on a worker thread. (Error creating window
handle.) The problem only occurs when I manually host the CLR and then use a
worker thread to construct a windows form, but then show the form on the
main application thread. I realize that it's not the done thing to post on
multiple forums, but the main content of this question is much more
appropriate to this forum, and I didn't get any answer on
microsoft.public.dotnet.framework.windowsforms.

So, is there anywhere I can find an example of how to host the CLR?

The way I am doing it now is:

a) Call CorBindToRuntimeEx
CComPtr<ICorRuntimeHost> spRuntimeHost;
CorBindToRuntimeEx(_T("v1.1.4322"), _T("wks"),
   STARTUP_LOADER_OPTIMIZATION_SINGLE_DOMAIN | STARTUP_CONCURRENT_GC,
   CLSID_CorRuntimeHost, IID_ICorRuntimeHost, (LPVOID*)(&spRuntimeHost));

b) Starting the CLR
spRuntimeHost->Start();

c) Getting _AppDomain interface
CComPtr<IUnknown> spUnk;
spRuntimeHost->GetDefaultDomain(&spUnk);
_AppDomainPtr spDefAppDomain;
spUnk->QueryInterface(&spDefAppDomain);

d) Getting IDispatch of the managed object (calling GetIDsOfNames on the
managed object directly seems to fail. Mangled method names?)
CComPtr<_ObjectHandle> spObjectHandle;
spDefAppDomain->CreateInstance( _bstr_t("MyName"),
_bstr_t("MyNamespace.MyClass"), &spObjectHandle);
CComVariant VntUnwrapped;
spObjectHandle->Unwrap(&VntUnwrapped);
if (VntUnwrapped.vt != VT_DISPATCH) return FALSE;
CComPtr<IDispatch> pDisp;
pDisp = VntUnwrapped.pdispVal;

e) Invoking the method on the managed class (entry point to managed code)
OLECHAR FAR* szMember = _T("StartMyManagedApplication");
DISPID dispid;
DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
pDisp->GetIDsOfNames (IID_NULL, &szMember, 1, LOCALE_SYSTEM_DEFAULT,
&dispid);
pDisp->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD,
    &dispparamsNoArgs, NULL, NULL, NULL);

I'd be very interested to know if this is the correct way, or if not, where
I can
find out further information on how to do this correctly.

Thanks in advance,
David.
Ben Rush - 05 Dec 2003 16:25 GMT
"So, is there anywhere I can find an example of how to host the CLR?"

Plenty. Just google for the information. This was a good starting place for
me to begin...

http://www.gotdotnet.com/team/clr/about_clr_Hosting.aspx

I have never hosted the CLR myself, but I play with rotor a lot. I probably
wouldn't be the best person to review your code, though, as I've never
actually hosted the CLR, so I'll reserve that to someone else.

> I posted this message on microsoft.public.dotnet.framework.windowsforms
> since the original thread related to an error that results when trying to
[quoted text clipped - 51 lines]
> Thanks in advance,
> David.
David Battams - 08 Dec 2003 15:34 GMT
Thanks Ben. That was a useful link.

Avoiding Invoke on IDispatch helped me get past the problem I was
experiencing. :-)

David.

> "So, is there anywhere I can find an example of how to host the CLR?"
>
[quoted text clipped - 64 lines]
> > Thanks in advance,
> > David.

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.