Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / April 2006

Tip: Looking for answers? Try searching our database.

Managed Wrapper for unmanaged Constructor with inputs

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
bidalah@yahoo.com - 27 Apr 2006 15:53 GMT
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.
Marcus Heege - 27 Apr 2006 17:00 GMT
> Hi all,
>
[quoted text clipped - 28 lines]
> 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?
bidalah@yahoo.com - 27 Apr 2006 19:00 GMT
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?
Marcus Heege - 27 Apr 2006 19:21 GMT
> Hi Marcus,
>
[quoted text clipped - 7 lines]
> 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; }
};
Tamas Demjen - 27 Apr 2006 19:20 GMT
> 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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.