473,832 Members | 2,118 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Native classes in managed class interfaces

Dear VS Team,

using the Beta 2 of VS 2005 I've encontered the following problem.

Let's assume threre are three Dll's, one unmanaged and two managed. In the
unmanaged we put a simple unmanged struct "A" which is exported in the usual
way.

The first managed assembly defines a managed class "B" using the unmanaged
class "A" defined in the unmanaged Dll. This class "B" has got a public
member variable "a" of type "A*" and a function "get" returning "a".

The second managed assembly defines a managed class "C" having a member "b"
of type "B^". See the code below.

// Unamanaged DLL

struct A
{
int data;
};

// First Managed DLL (references unmanaged dll)

public ref class B
{
public:
A* a;

B()
{
a = new A();
}

A* get()
{
return a;
}
};

// Second Managed DLL (references unmanaged dll and first managed dll)

public ref class C
{
private:
B^ b;

public:
C()
{
b = gcnew B();

A* a0 = b->a;
A* a1 = b->get();
}
};

Compiling this code in VS 2003 (with the corresponding syntax changes of
course), everything goes well. In VS 2005 I get the following errror messages:

'B::a' : cannot access private member declared in class 'B'

and

'B::get': candidate function(s) not accessible

Does this mean, that a managed class can not have a function returning a
pointer to an unmanaged class??

Does the compiler declare the public member "a" in the managed class "B"
private on its own in order to make acessing to it from outside the assembly
impossible??

Best regards,

Martin Zenkel
Nov 17 '05 #1
2 2330


--
This posting is provided "AS IS" with no warranties, and confers no
rights."Use of included script samples are subject to the terms specified at
http://www.microsoft.c om/info/cpyright.htm"

"Martin Zenkel" <Martin Ze****@discussi ons.microsoft.c om> wrote in message
news:F6******** *************** ***********@mic rosoft.com...
Dear VS Team,

using the Beta 2 of VS 2005 I've encontered the following problem.

Let's assume threre are three Dll's, one unmanaged and two managed. In the
unmanaged we put a simple unmanged struct "A" which is exported in the
usual
way.

The first managed assembly defines a managed class "B" using the unmanaged
class "A" defined in the unmanaged Dll. This class "B" has got a public
member variable "a" of type "A*" and a function "get" returning "a".

The second managed assembly defines a managed class "C" having a member
"b"
of type "B^". See the code below.

// Unamanaged DLL

struct A
{
int data;
};

// First Managed DLL (references unmanaged dll)

public ref class B
{
public:
A* a;

B()
{
a = new A();
}

A* get()
{
return a;
}
};

// Second Managed DLL (references unmanaged dll and first managed dll)

public ref class C
{
private:
B^ b;

public:
C()
{
b = gcnew B();

A* a0 = b->a;
A* a1 = b->get();
}
};

Compiling this code in VS 2003 (with the corresponding syntax changes of
course), everything goes well. In VS 2005 I get the following errror
messages:

'B::a' : cannot access private member declared in class 'B'

and

'B::get': candidate function(s) not accessible

Does this mean, that a managed class can not have a function returning a
pointer to an unmanaged class??

Does the compiler declare the public member "a" in the managed class "B"
private on its own in order to make acessing to it from outside the
assembly
impossible??

Best regards,

Martin Zenkel


Thanks for the very clear post. We do allow a managed class to have a
function returning a pointer to an unmanaged class. The only thing
disallowed is embedding native
types in managed types. You are missing __declspec(dlle xport) and
__declspec(dlli mport) on your class declarations. I have copied the correct
working code.
cl /EHsc /LD native.cpp
cl /clr m.cpp /link native.lib

///////////////// native.cpp

struct __declspec(dlle xport) A
{
int data;
};

///////////////// m.cpp

using namespace System;
struct __declspec(dlli mport) A
{
int data;
};

public ref class B
{
public:
A* a;

B()
{
a = new A();
a->data = 4;
}
A* get() // returning a pointer to a native struct.
{
return a;
}
};

int main()
{
B obj;
A* a = obj.get();
Console::WriteL ine(a->data);
}

Does that help?
Thanks,
Kapil


Nov 17 '05 #2
I think Martin meant that a managed dll can't export an unmanaged member
pointer, such as:

///////////////// native.h (a native DLL project)

struct __declspec(dlle xport) A
{
int data;
};

///////////////// managed1.h (a managed DLL project)

struct __declspec(dlli mport) A
{
int data;
};

public ref class B
{
public:
B()
{
a = new A();
a->data = 4;
}
A* get() // returning a pointer to a native struct.
{
return a;
}
private:
A* a;
};

///////////////// managed2.h (another managed .exe or DLL project)

#using "managed1.d ll"

void test()
{
B b;
b.get(); // error: 'B::get' candidate function(s) not accessible
}

So a managed assembly (DLL) doesn't seem to be able to export unmanaged
pointers. There are two choices:
1. You either #include "managed1.h " and add managed1.cpp to the second
project, and then get() is accessible and is working, or
2. you are #using "managed1.d ll" and the get() function simply can't be
called. I couldn't mix #using and #include.

Sometimes it would be nice to have access from one assembly to unmanaged
members in another assembly. Just like Graphics::GetHd c(), which returns
the underlying HDC handle. Similarly, it would be nice to add a get()
function to my managed classes that wrap an underlying native C/C++ API.
Sometimes it's inevitable, that's why WinForms has GetHdc too. As I see
it, Graphics::GetHd c returns a managed type called IntPtr, so that's how
it works internally. I don't think a managed assembly is able to return
pointers to unmanaged C++ types, but if it's possible, I would really
like to know how.

Perhaps returning IntPtr and casting it to our native C++ type is the
only way. I actually tried it and it worked:

///////////////// managed1.h

public ref class B
{
[...]
IntPtr get() { return static_cast<Int Ptr>(a); }
[...]
};

///////////////// managed2.h

A* a = reinterpret_cas t<A*>(static_ca st<void*>(b.get ()));

This is a little bit nasty, in my opinion, if it's correct at all. I
wish there was a more user friendly way of doing this, with better
unmanaged type safety.

Any comments? I'm still in the early experimenting phase with Beta 2.

Tom
Nov 17 '05 #3

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

Similar topics

4
4132
by: David Kantowitz | last post by:
I am trying to wrap a native-C++ DLL in managed C++, to use in a .NET project. The native code is compiled into a DLL, and I have created a .def file that exports the mangled names of the symbols I am going to use from the wrapper library. The wrapper library has the code written that uses the native classes, and wraps them to present an equivalent of their interface for .NET.
2
7580
by: Jacob Cohen | last post by:
Under VC7.1, I am trying to wrap a native-C++ DLL that contains C++ objects in a Managed-C++ class library for use in a C# project. I created and compiled the native DLL under VC7.1 as a Win32 DLL C++ (unmanaged) project. I found the decorated names of the symbols I wanted to export and created a .def file, so that it generated an import library. I took this import library and had my Managed-C++ class library link against it. The...
4
9883
by: Tim Menninger | last post by:
Just started working on this and have not found any real good resources out there. We have a lot of native C++ Dll code that we use for our app. We want to share the code so that C# ASP.net code can use the same business logic as our C++ client. Here are some questions: 1) Is there a way to call into native C++ classes directly? Meaning we do not have C API. I believe we could use a C API using the in C#. 2) If can't do #1, do we need...
3
1429
by: Adam | last post by:
I can't seem to find one spot on the net that specifies exactly what I need to do. Situation: Native dll needs to hold a static reference to a managed class in .net 2.0 (whidbey) which needs to hold a static reference to managed classes. 1. In 2003 I needed special initialization because of the loader lock issue. In 2005 it seems I dont need this, however I am calling into a managed dll from a native dll, and I read somewhere there can...
9
2076
by: Herby | last post by:
Is possible to have a managed method within a Native(un-managed) class within a \clr project? E.g. class myClass { public: #pragma managed void myMethod(void);
8
2623
by: news.chi.sbcglobal.net | last post by:
Hi. I am a little confused about the difference between a "value class" and a "ref class". I have the following code sample that shows the definition of a value class and of a ref class and each is instantiated twice - one on the stack and one on the managed heap. I see no difference between the two, at least not in the way they are used. Can someone explain the difference, please? Thank you.
2
1169
by: Edward Diener | last post by:
Specifying 'public class' or 'private class' for C++ class definitions is not part of the C++ standard. In a topic in the VS 2005 docs 'How to: Declare public and private on Native classes', it is suggested that this syntax is supported with VC8 for interaction with CLR ref classes and for specifying in a CLR assembly. However in the specific C++ language documentation for native classes, there is no mention of the possibility of...
5
7274
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?
3
1433
by: dragonslayer008 | last post by:
I recently added some new code files to a C++/CLI project. The added code is all native. I simply added the code to the project and built it. It compiled fine but I am getting the linker error: LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (IDirect3DDevice9): (0x0200015b) I get this in three .obj files. Each file has a different class, but each class holds a member to an...
0
9795
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
9642
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
10781
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10499
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...
0
10212
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7753
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6951
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();...
1
4421
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
3972
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.