473,769 Members | 5,900 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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->SetUnmanaged B( 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 NestedFriendCla ss
{
public:
NestedFriendCla ss( ManagedClassB* owner)
{
m_owner = gcroot<ManagedC lassB*>(owner);
}
UnmanagedClassB * GetUnmanagedB()
{
return m_owner->m_unmanagedB ;
}
private:
gcroot<ManagedC lassB*m_owner;
}
};

And the MangedClassA::S etManagedB() method becomes something like.

void ManagedClassA:: SetManagedB( ManagedClassB* b )
{
ManagedClassB:: NestedFriendCla ss temp( b );
m_unmanagedA->SetUnmanaged B( temp.GetUnmanag edB() );
}

Now this all compiles fine but when I run it I get a
'System.FieldAc cessException' exception in
ManagedClassB:: NestedFriendCla ss::GetUnmanage dB().

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.

Aug 25 '06 #1
1 1212
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.

Aug 31 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3551
by: lolomgwtf | last post by:
I have a managed C++ method that wraps unmanaged code and creates a managed object holding data retrieved form an unmanged one. I want create an instance of this managed class in C#, pass it to this method and have it set the instance to hold the right data. >From what I've read it seems I should be able to pass C# objects to managed C++ methods and it should just work; however, when I try it, my C# instance comes out null. If I step...
2
2214
by: JRoe | last post by:
Hello, I am beside myself with this problem. I need to access unmanged C++ code in a C# environment. I have created a managed C++ wrapper that wraps the unmanaged class. When I call the constructor, it is returning undefined. Here is an example: Unmanaged C++ class Map_Interface
2
2411
by: Brett Styles | last post by:
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...
1
1476
by: c guan | last post by:
I have a simple Managed C++ wrapper built around an *existing* C++ interactive application. I can access the Managed C++ application classes from a VB .NET or C# client and the GUI part works as expected. I am using VS .NET 2002 (.NET framework v1.0.3705) for building my Managed C++ application on a Windows XP Professional (version 2002) SP1. I do not have much of a choice with the way the C++ program is designed (e.g how it displays...
5
3418
by: Maxwell | last post by:
Hello, Newbie question here. I have a VS.NET 2003 MC++ (not C++/cli) project where I have a managed class reference in a unmanaged class...simple enough. To keep things short I am for the most part attempting to do what is this article by Nish: http://www.voidnish.com/articles/ShowArticle.aspx?code=cbwijw I have to hook up a unmanaged callback to a managed method using IJW NOT P\Invoke. So I am employing this "Thunk" or "Bridge" class...
6
1461
by: nicolas.hilaire | last post by:
Hi all, i'm not totally clear with some concepts about managed and unmanaged code. I'm asking myself some questions : - i've a MFC app, i want to use the framework dotnet by switching to /clr compilation mode. Is my app in CLR, or only the call to the
1
2078
by: ropo | last post by:
I am using .NET 2.0 I have a C#.Net App, which uses a .NET Class Library, which accesses a COM object through interop. The C#.Net App also uses a mixed mode C++.Net Class library which uses an unmanaged win32 DLL, which uses another unmanaged win32 DLL which accesses the same COM object The COM object is in another process.
5
7271
by: =?Utf-8?B?U2hhcm9u?= | last post by:
I have a class that is writen in unmanaged pure native C++. This class files (h and cpp) are inserted to a managed C++ (VC++ 2005, C++/CLI) DLL compoenet. This DLL compoenet is used in a C# application. My question is: Does the performance of the unmanaged pure native C++ class described above is the same if it was a in a pure unmanaged native C++ DLL component?
5
3899
by: ttc | last post by:
Hi All, I have a managed class that inherits from an unmanged class. The question is, if the object of the manged class get garbage collected, will the memory be free automatically for me or only the bit that is used by the managed code? What about the unmanaged bit? Does anyone know of any article that discuss about this? Any help is appreciated.
0
9586
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10043
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.