473,796 Members | 2,875 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 2327


--
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
4129
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
7577
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
9882
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
1428
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
2075
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
2622
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
1168
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
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?
3
1432
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
10453
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
10223
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
10172
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
10003
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
7546
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
6785
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
5441
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
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
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

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.