How to get an unmanaged pointer within VC++ .Net?

O

Orbian

I am using an unmanaged pointer and need to take the address of this
and pass it to a function (a pointer to a pointer). If the pointer is
defined as a global variable it will work, however if I move it into a
class variable I get teh following error:

error C2440: 'type cast' : cannot convert from 'IDevMgr *__gc * ' to
'IDevMgr ** '

Basically I want to go from:

IDevMgr *gDev;

public __gc class CMyClass
{
};

to:



public __gc class CMyClass
{
private:
IDevMgr *m_Dev;
};


and then do a:

MyFunc(&m_Dev);

from a member function in CMyClass. MyFunc(&gDev) will work fine
though.
 
B

Ben Voigt [C++ MVP]

Orbian said:
I am using an unmanaged pointer and need to take the address of this
and pass it to a function (a pointer to a pointer). If the pointer is
defined as a global variable it will work, however if I move it into a
class variable I get teh following error:

error C2440: 'type cast' : cannot convert from 'IDevMgr *__gc * ' to
'IDevMgr ** '

Basically I want to go from:

IDevMgr *gDev;

public __gc class CMyClass
{
};

to:



public __gc class CMyClass
{
private:
IDevMgr *m_Dev;
};


and then do a:

MyFunc(&m_Dev);

from a member function in CMyClass. MyFunc(&gDev) will work fine
though.

You need a pinning pointer for that I think, and I only know how to do that
with C++/CLI. The syntax you are using is obsolete and the Managed
Extensions for C++ that used that syntax are buggy and have no future.

The reason what you are doing doesn't work is that when the data is inside a
managed class, it is in the garbage collected heap, and can move around at
any time.
 

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