creating an umanaged object in a managed wrapper

  • Thread starter Thread starter JRoe
  • Start date Start date
J

JRoe

Hello,
I am beside myself with this problem.
I need to access unmanged C++ code in a C# environment. I have created
a managed C++ wrapper that wraps the unmanaged class.
When I call the constructor, it is returning undefined. Here is an
example:

Unmanaged C++

class Map_Interface
{
Map_Inteface()
{
...does stuff
}
}

Managed C++
#using mscorlib.dll

__gc class Map_Interface_Wrapper
{
private:
Map_Interface __nogc *myMapInterface;

public:
Map_Interface_Wrapper()
{ myMapInterface = new Map_Interface; }

};


C# Driver
.....
using Map_Interface_Wrapper;

Map_Interface_Wrapper aWrapper = new Map_Interface_Wrapper;


My problem is when the C# instantiates the Map_Interface_Wrapper, the
managed code constructor is being called and then calls the unmanaged
C++ constructor. When I step into this I see the objects being
initialized, but when it leaves the unmanaged constructor and returns
to the managed wrapper, the newly created object is still undefined. I
can see the address it is pointing to is the same as the unmanaged
object.
I have no idea how to fix this. I've been looking all over the
internet to see if anyone has had this problem and can't seem to find
it. All the documentation I've read shows to do it this way, but for
some reason it doesn't work for me. Perhaps I have a compilier setting
wrong in the IDE??
I would surely appreciate help on this.
Thanks in advance!
 
Hi...

When you say the object is undefined, do you mean you cannot make calls into
it (call methods on it)?

Is the unmanaged constructor possibly throwing an exception? In C++ this
will cause the new method to return an "undefined" object. In addition, how
is the unmanaged code loaded? Are you using P/Invoke with DLLImport or is
the unmanaged code in its own EXE?

John
 
John,
What I mean by undefined is when I look at the variables that the
constructor was supposed to instantiate in the IDE debuger they are set
to <undefined>

If the constructor was throwing an exception, wouldn't the code assert?
The managed wrapper's constructor is calling the unmanaged
constructor when I call a "new" on it:

myMapInterface = new Map_Interface;

When it returns from this new call, the values of myMapInterface are
all undefined...meaning, they are just not set.

I don't know what you mean about how is the unmanaged code loaded? The
unmanaged code is a static library (.lib) and my managed portion is a
..dll and in the .dll I have a #include to the class that I am
referencing in the .lib. I am not using PInvoke or any DLLImport
calls. Should I be? And if so, where would I need ot do this? I've
seen other examples of this at my company and they are not using
PInvoke or DLLImport calls.

Thanks,
Janine
 
Back
Top