Managed Wrapper for unmanaged Constructor with inputs

B

bidalah

Hi all,

Basically I need to create a managed wrapper for the class "A" below:

class A : public CModule
{
public:

A(B& bees);
virtual ~A();

.....etc. (it's actually a very big class)
}

As you can see, the constructor above takes an input which is a
reference to another unmanaged class called "B"

class B
{
public:

B();
virtual ~B();


.....etc. (also a big class)

}

I have successfully created a wrapper for 'B'. However, the
constructor for 'A' has me stymied. How do you create a managed
wrapper for an unmanaged class that requires an instance of another
unmanaged class in its constructor? Any help would be appreciated.
 
M

Marcus Heege

Hi all,

Basically I need to create a managed wrapper for the class "A" below:

class A : public CModule
{
public:

A(B& bees);
virtual ~A();

....etc. (it's actually a very big class)
}

As you can see, the constructor above takes an input which is a
reference to another unmanaged class called "B"

class B
{
public:

B();
virtual ~B();


....etc. (also a big class)

}

I have successfully created a wrapper for 'B'. However, the
constructor for 'A' has me stymied. How do you create a managed
wrapper for an unmanaged class that requires an instance of another
unmanaged class in its constructor? Any help would be appreciated.

Why don't you create a constructor that takes a B^ argument?
 
B

bidalah

Hi Marcus,

That's my problem. I can't find a way of coding the constructor that
doesn't error out. If I use the wrapper for B as the argument (m_B),
the error is "Cannot convert (managed value) to (unamanaged value). If
I try to use B directly the error is "Cannot instantiate nonstatic
members in a __gc class. I've tried all kinds of different
constuctors.

How do you create a constructor for a wrapper class when the unmanaged
class uses another unmanaged class as input?
 
T

Tamas Demjen

I have successfully created a wrapper for 'B'. However, the
constructor for 'A' has me stymied. How do you create a managed
wrapper for an unmanaged class that requires an instance of another
unmanaged class in its constructor? Any help would be appreciated.

Try something like this:

class B
{
};

class A
{
public:
A() { }
A(const B&) { }
};

ref class MB
{
public:
MB() : b(new B) { }
B& GetUnmanaged() { return *b; }
private:
B* b;
};

ref class MA
{
public:
MA() : a(new A) { }
MA(MB^ src) : a(new A(src->GetUnmanaged())) { }
private:
A* a;
};

Tom
 
M

Marcus Heege

Hi Marcus,

That's my problem. I can't find a way of coding the constructor that
doesn't error out. If I use the wrapper for B as the argument (m_B),
the error is "Cannot convert (managed value) to (unamanaged value). If
I try to use B directly the error is "Cannot instantiate nonstatic
members in a __gc class. I've tried all kinds of different
constuctors.

How do you create a constructor for a wrapper class when the unmanaged
class uses another unmanaged class as input?

Is that what you want?

#using <mscorlib.dll>

class A
{
};

class B
{
public:
B(A& a) {}
};

public __gc class WA
{
public private:
A* m_pA;
public:
WA() : m_pA(new A) {}
~WA() { if (m_pA) delete m_pA; }
};

public __gc class WB
{
B* m_pB;
public:
WB(WA __gc* pWA) : m_pB(new B(*pWA->m_pA)) {}
~WB() { if (m_pB) delete m_pB; }
};
 

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