all groups > dotnet interop > december 2006 >
You're in the

dotnet interop

group:

Hosting the CLR in a C++ program


Hosting the CLR in a C++ program PLS
12/18/2006 5:43:45 PM
dotnet interop: I'm having a problem hosting the CLR in a C++ program.

I want to C++ program code to be agnostic about runtime versions, so I
can make the runtime version be a configuration parameter.

The sample I have shows passing the runtime version to
CorBindToRuntimeEx. So far, so good. But it also shows

#import "C:\\WINNT\\Microsoft.NET\\Framework\\v1.0.2914
\\Mscorlib.tlb" raw_interfaces_only

Doesn't this import tie the program to a specific runtime version? Is
there a way around importing this type library? I see that mscoree.h
does not include the COM interface definitions the CLR uses.

Pardon me if this is the wrong group to post this question.

Re: Hosting the CLR in a C++ program William DePalo [MVP VC++]
12/18/2006 11:36:48 PM
[quoted text, click to view]

Well, the compiler uses the import statement to create C++ wrapper classes.
We are dealing with interfaces more than anything else. If you run the
dependency walker or the linker's binary dump utility you will see that the
application in which you have the compiler directive is _not_ importing
functions in the Win32 sense to create a dependency on a specific DLL.

Just by the way ...

.... what I can tell you though is that it does get dicey when you start to
debug.

In an application of mine, I do host the runtime. And I had hoped that by
loading the CLR myself and deferring the load until after I read my
configuration that I would be OK.

What I have found recently is that is all well and good, up to the point of
debugging both the managed and native pieces of an application. Then the IDE
will get in the way.

By default, when you start to debug in _mixed_ mode, the VS 2003 IDE will
load the most recent version of the framework. That's bad if you have
version 2.0 or 3.0 of the framework installed because the IDE debugger is
itself a 1.1 application. Catch 22.

The issue and workaround are described here:

http://blogs.msdn.com/jmstall/archive/2005/12/05/VS2003_crashes_with_2005.aspx

Sadly, that makes an application much less agnostic about version.

Regards,
Will

Re: Hosting the CLR in a C++ program PLS
12/20/2006 6:20:48 PM
In your hosting application, how do you create a managed object and call
methods on it?

My problem is that I can call AppDomain.CreateInstance and get an
ObjectHandle, then unwrap that to get a reference to an Object.

I want to get an IDispatch interface from that, but calling
QueryInterface(IID_IDispatch... fails with E_NOINTERFACE.

Oddly enough, I can call the IDispatch methods, like Invoke, on that
reference, just not QueryInterface.

So how do I get an IDispatch interface to an object?

++PLS


In article <uAJxPdyIHHA.3676@TK2MSFTNGP03.phx.gbl>,
willd.no.spam@mvps.org says...
[quoted text, click to view]
Re: Hosting the CLR in a C++ program William DePalo [MVP VC++]
12/21/2006 1:48:23 AM
[quoted text, click to view]

In a C++ application, once I have gotten the default domain, I query for the
_AppDomain interface and having gotten that I use its CreateInstanceFrom()
method to construct a managed object from a type in an assembly. The
constructor of that object does whatever one-time initialization steps I
need to perform in my domain.

Later, on an ongoing basis, I use Activator::CreateInstance() to create the
objects I need. I use reflection to call methods on those objects. Check the
docs for the Type and MethodInfo classes and the latter's Invoke() method.

Regards,
Will

AddThis Social Bookmark Button