Hosting CLR
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.clr.

Post a new message to this list...

Tobias Ulm
Hi to all,

i'm trying to host the CLR form a VBA, VB6 or VC++6 Application. Has someone
a source code sample for this?

thanks a lot

Tobi

Reply to this message...
 
    
Christian Weyer
Tobias,

the following is a code snippet which was posted on the DM list (UNTESTED!):
---
[1] Component.cs
// Generate a public/private keypair using sn -k
using System;
using System.Reflection;
using System.Runtime.InteropServices;
[assembly:AssemblyKeyFile("PublicPrivate.snk")]
[assembly:ClassInterface(ClassInterfaceType.AutoDual)]
public class Greeter {
public string SayHello(string name) {
return "Hello, " + name;
}
}

[2] Hosted.frm
' Insert this into a VB project that references mscoree.tlb &
mscorlib.tlb
Dim rt As mscoree.CorRuntimeHost
Set rt = New CorRuntimeHost
rt.Start
Dim unk As IUnknown
rt.GetDefaultDomain unk
Dim ad As AppDomain
Set ad = unk
Dim oh As ObjectHandle
Set oh = ad.CreateInstance("Component, Version=0.0.0.0,
Culture=neutral, PublicKeyToken=2db8c699139dab54", "Greeter") ' Fix the
PublicKeyToken to match the new token created for [1]
Dim o As Object
Set o = oh.Unwrap
MsgBox o.SayHello("MSCOREE")
---

HTH,
Christian
---------------------------------
.NET XML Web Services Repertory
http://www.xmlwebservices.cc/
---------------------------------

Tobias Ulm wrote:

[Original message clipped]

Reply to this message...
 
    
Michael Willers
"Tobias Ulm" <Click here to reveal e-mail address> wrote

> i'm trying to host the CLR form a VBA, VB6 or VC++6 Application. Has
someone
> a source code sample for this?

Hi Tobi

More on this and the clr
www.devcoach.de

Regards
Michael

Reply to this message...
 
    
Michael Willers
"Michael Willers" <Click here to reveal e-mail address> wrote

it's really easy with c++ too. i've just hacked a sample. here's the code

<code>
int _tmain(int argc, _TCHAR* argv[])
{
CComPtr<ICorRuntimeHost> pCLR;
HRESULT hr = CorBindToRuntimeEx(L"v1.0.3214", L"wks", 0,
__uuidof(CorRuntimeHost),
__uuidof(pCLR),
(void**)&pCLR);
if (FAILED(hr)) return hr;

hr = pCLR->Start();
if (FAILED(hr)) return hr;

CComPtr<IUnknown> pUnk;
hr = pCLR->GetDefaultDomain(&pUnk);
if (FAILED(hr)) return hr;

CComPtr<_AppDomain> pDomain;
hr = pUnk->QueryInterface(&pDomain);
if (FAILED(hr)) return hr;

// Call Context
CComVariant var(73);
hr = pDomain->SetData(CComBSTR(L"MyDataNow"), var);
if (FAILED(hr)) return hr;

long retval;
hr =
pDomain->ExecuteAssembly_2(CComBSTR(L"<assemblypath\assemblyname.exe>"),
&retval);
if (FAILED(hr)) return hr;

CComPtr<_ObjectHandle> handle;
hr =
pDomain->CreateInstanceFrom(CComBSTR(L"<assemblypath\assemblyname.exe>"),
CComBSTR(L"<Typename>"),
&handle);
if (FAILED(hr)) return hr;

CComVariant var2;
hr = handle->Unwrap(&var2);
if (FAILED(hr)) return hr;

CComPtr<IMyInterface> pI;
hr = var2.punkVal->QueryInterface(&pI);
if (FAILED(hr)) return hr;

double n;
hr = pI->DoSomething(5, 2, &n);

return 0;
}
</code>

Hope that helps
Greetings to hannes and the team

Michael

Reply to this message...
 
 
System.AppDomain
System.ComponentModel.Component
System.Runtime.InteropServices.ClassInterfaceType
System.Runtime.Remoting.ObjectHandle





DevWeek 2007 - The 10th annual DevWeek conference, the UK's biggest event for .NET developers, takes place in London at the end of February. Topics include .NET 3.0, Windows Vista, ASP.NET AJAX, VS Team System, C# 3.0 and .NET debugging, and speakers include Jeff Prosise, Fritz Onion, Dino Esposito, Dave Wheeler, Ingo Rammer and Keith Brown. www.devweek.com
Ad
SharePoint Portal Server?
Ask Matthew Baxter-Reynolds for advice and help on setting up SharePoint
BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
Bespoke Software?
Commission Matthew Baxter-Reynolds and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... QuickCode.NET
log4net
NAnt
NUnit
CodeSmith
Reflector
NUnit add-in
SourceBrowse
Filemon
FxCop
SecondCopy
 
MBR IT Solutions - .NET 247 is owned and operated by Matthew Baxter-Reynolds, the man behind .NET 247. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.mbrit.com
 Copyright © Matthew Baxter-Reynolds 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com - www.winfx247.com - www.adminlife.com - www.salessorcerer.com - www.mbrit.com