Accessing and Unmanaged Class from Managed Code

B

Brett Styles

Hi Guys,
I am trying to access a class in an unmanaged dll. I have
created a wrapper managed class to access the functions I need but no matter
what I try from the MSDN samples I can not get it to work with my code. I
have a VB Net front end which need the access the unmanaged functions. The
wrapper I wrote can be accessed but the wrapper will not compile when I
refer to the unmanaged class. I have tried using header file for definitions
with and #include in the managed code, __dllExport and all my attempts do
not work. I am escentially a VB programmer but have done enough C years ago
to allow me to get the code working stand alone. I am not up to the task at
hand and would really appreciate some ideas from the C++ experts out there.

Thanks
Brett Styles.

snippet:

managed wrapper:

namespace CameraSDK
{
__gc public class CameraManaged
{
public:
CameraManaged(void)
{
}

~CameraManaged(void)
{
}

ConnectCamera()
{
CProg.Connect(); //Error here every time.
}

private:
CameraU CProg; //ERROR here every time
}:
}


Unmanaged Code

class CameraU
{
CameraU::CameraU(void)
{
}

~CameraU::CameraU(void)
{
}

Connect()
{
.......code here
}
};
 
L

Lev

Hi,

You should use a pointer to the unmanaged class instead of embedding the
unmanaged type in the managed class. Embedding only work in the most trivial
cases, and should you find yourself in such a situation, you are most likely
better off making the class to be embedded either __gc or __value. The
problem is that when you embed an unmanaged object in a managed class, all
the members of the unmanged class will be created on the GC heap (think of
the disastrous effects of for instance a memcpy on the managed heap).
If possible, you should have a pointer to the unmanaged class in the
managed class and new it up from the constructor (or other function) of the
managed class if possible.

Hope this helps,

Lev
 
B

Brett Styles

I did not make my example clear. I actually have two
projects one managed (which contains the class CameraManaged) and another
project which is unmanaged but refrenced by the managed one (this contains
the class CameraU). I tried embedding early on but that did not work. Both
operate independently prefectly ok. I just can not create a reference to
the unmanaged class from the managed one, in VB this is a simple task but in
C++ there are so many options I do not know what the errors reported mean
when I try different options as set out in the MSDN samples. One error that
comes up a lot is unreferenced exteral _dtor, when I search I cannot find
any thing that calls such a method.

Regards
Brett
 

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