General object wrapper

O

Ole Nielsby

I need to wrap managed objects in unmanaged objects in a
general manner, i.e. I need a C++ COM style object that
will wrap any managed object.

I expected (optimistically) to be able to declare my class
as something like:


class GeneralObjectWrapper : public IUnknown
{
public:
GeneralObjectWrapper(System::Object wrapme);

// this method will do method calls by means of reflection
MY_RESULT bool WINAPI GenericMethodCall
(WCHAR* methodName, MY_VARIANT args[], int argcount, ...);

private:
System::Object wrapped;
}


I was hoping the C++/clr compiler would take care of the
plumbing. But - it just doesn't work. The compiler says I
can't have a managed field in an unmanaged class, which
means I'll need to create an interop wrapper. But how is
this best done?

Is there a generic wrapper class/template that I can subclass
and augment with stdcall virtuals?

I'd imagine this is how the COM interop layer creates COM
wrappers for managed objects, but it is not clear to me if
this is possible from C++.

Or is there a rock bottom C++ struct that can be registered
with the CLR as a ptr location? Would it be possible to
keep it in a MASM allocated structure? (this would save
me the expense of an extra COM style wrapper object.)

I'm not too familiar with C++ - the thing I'm trying to interface
with CLR is a MASM interpreter for a scripting language with
features that simply won't map to IL in a sensible way, and
the docs aren't tailored to this kind of project, so if someone
could give me a hint...

I realize I might be able to accomplish something similar by
using automatically generated IDispach wrappers, but I want
a more direct interface.

I'm using VS2005 pro.
 
W

William DePalo [MVP VC++]

Ole Nielsby said:
I need to wrap managed objects in unmanaged objects in a
general manner, i.e. I need a C++ COM style object that
will wrap any managed object.

Then I think that you are in luck. Paul Di Lascia published a managed
wrapper class (ManWrap) here:

http://msdn.microsoft.com/msdnmag/issues/05/04/C/

If there is anything in there that you don't understand, just post again.

Regards,
Will
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top