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++ / August 2006

Tip: Looking for answers? Try searching our database.

Giving one managed wrapper class access to the unmanged part of another managed wrapper class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mschuck - 25 Aug 2006 19:21 GMT
Here is the scenario I'm trying to make work.

I've got 2 managed C++ classes, each of which wrappes an unmanaged C++
class, kind of like so:

__nogc class UnmanagedClassA
{
  public:
     void SetUnmanagedB( UnmangedClassB* val );
...
};
__gc class ManagedClassA
{
  public:
    MangedClassA() :
      m_unmanagedA(__nogc new UnmanagedClassA() )
    { }

  private:
    __nogc UnmanagedClassA* m_unmanagedA;
};

__nogc class UnManagedClassB
{
...
};
__gc class ManagedClassB
{
  public:
    MangedClassB() :
      m_unmanagedB(__nogc new UnmanagedClassB() )
    { }

  private:
    __nogc UnmanagedClassB* m_unmanagedB;
};

The managed wrapper classes were created to allow using the unmanaged
classes in C#.

I need to be able to hand an instance of ManagedClassB to ManagedClassA
and have it pass the m_unmanagedB pointer to a method of m_unmanagedA.
Something like this:

void ManagedClassA::SetManagedB( ManagedClassB* b )
{
  m_unmanagedA->SetUnmanagedB( b->m_unmanagedB );
}

My first question is: what is the best way to accomplish this task
(idealy without making m_unmangedB public)? and my second question is
why doesn't my solution below work?

Below is my solution that doesn't quite work:

In traditional C++ I would simply declare ManagedClassA as a private
friend of ManagedClassB and everything would be good, but I have come
to the understanding that friends aren't allowed in managed C++ so I've
created a unmanged nested class to solve the problem, something like
this

__gc class ManagedClassB
{
  public:
    ManagedClassB() :
      m_unmanagedB(__nogc new UnmanagedClassB() )
    { }

  private:
    __nogc UnmanagedClassB* m_unmanagedB;

  public:
  __nogc class NestedFriendClass
  {
    public:
       NestedFriendClass( ManagedClassB* owner)
       {
          m_owner = gcroot<ManagedClassB*>(owner);
       }
       UnmanagedClassB* GetUnmanagedB()
       {
          return m_owner->m_unmanagedB;
       }
    private:
       gcroot<ManagedClassB*> m_owner;
  }
};

And the MangedClassA::SetManagedB() method becomes something like.

void ManagedClassA::SetManagedB( ManagedClassB* b )
{
  ManagedClassB::NestedFriendClass temp( b );
  m_unmanagedA->SetUnmanagedB( temp.GetUnmanagedB() );
}

Now this all compiles fine but when I run it I get a
'System.FieldAccessException' exception in
ManagedClassB::NestedFriendClass::GetUnmanagedB().

Now this implies to me that the public, protected, and private access
specifiers are being enforced at run time for unmanaged code which is
surprising to me, can anyone tell me what's going on here.

I will admit that I'm relatively new to the managed C++ and C# worlds
so I may be missing something simple here. I do consider myself a very
advanced traditional C++ programmer.
Also I'm currently using MS Visual Studio 2003.NET and we are upgrading
to 2005.NET next week.
Matt Schuckmann - 31 Aug 2006 17:27 GMT
I can't believe that no one out there has had to do something like this
or even thought about it. Is there noone out there that can comment on
this?

This is such a common thing in C++ it seems that it would have been
addressed by the VC++/VC# development teams.

I'm really troubled by the absence of the friend feature in C# &
C++/CLI etc, not having it eliminates so may good OO practices.

Matt S.

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.