473,396 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

nested native class in managed class in VC8 not allowed??

Hi everyone..

I was trying to implement callback fucntions in native codes in my
VC8 class, and referred to some online codes which use native class
nested within managed class. basically of the form,

__gc class CManagedClass
{
__nogc CNativeClass
{
//blah blah blah
};
};

I tried to replocate the same thing using C++/CLI, but failed. I force
the compiler to use /clr::oldsyntax and was able to compile with
problem. So it seems the problem only appear in VC8 and worked on VC7.
my C++/CLI equivalent of the same code is as follows,

public ref class CManagedClass
{
class CNativeClass
{
//blah blah blah
};
};

setting to value also does not work. And I need it in ref as this is a
..NET wrapper for the native codes, and it compiled into a .NET assembly.
The compilation failed with the error, C2814: A native type cannot be
nested within a managed type. So obviously, by not putting the __nogc,
it is treating the CNativeClass as a native class indeed and the
compiler is complaining about it. So, in VC8, what is the work around
for this issue? It cannot be that the change to VC8 prevents callback
mechanism from native codes to managed codes. Doing so will break many
old codes which use things like event sinks and even the win32 API
EnumWindow which is often used as an example of native callback in .NET.
Can someone please enlighten me ?
Dec 30 '05 #1
9 3109
Lonewolf wrote:
Hi everyone..

I was trying to implement callback fucntions in native codes in my
VC8 class, and referred to some online codes which use native class
nested within managed class. basically of the form,

__gc class CManagedClass
{
__nogc CNativeClass
{
//blah blah blah
};
};
[...]


Try the following:

class CNativeClass {};
ref class CManagedClass{
CNativeClass* a;
//CNativeClass a; -> fails
};

AFAIK embedding a native class is not (yet) possible. IIRC it will be
supported in the next version of VC. Meanwhile you have to create and
destroy the native objects manually.

Hope that helps.

Andre
Dec 30 '05 #2
Andre Kaufmann wrote:
Lonewolf wrote:
Hi everyone..

I was trying to implement callback fucntions in native codes in my
VC8 class, and referred to some online codes which use native class
nested within managed class. basically of the form,

__gc class CManagedClass
{
__nogc CNativeClass
{
//blah blah blah
};
};
[...]

Try the following:

class CNativeClass {};
ref class CManagedClass{
CNativeClass* a;
//CNativeClass a; -> fails
};

AFAIK embedding a native class is not (yet) possible. IIRC it will be
supported in the next version of VC. Meanwhile you have to create and
destroy the native objects manually.

Hope that helps.

Andre

hi,
thanx for taking the trouble to enlighten me. I have indeed done just
that. However, as i mentioned, this method can't work for callback
fucntions and event sinks to native class which I need to propagate to
the managed class via delegate. I knew this is possible and supported in
VC7 as I already have the code running using oldsyntax in VC8, However,
I would like to know the VC8 equivalent of this technique. It has to be
possible, otherwise, native class' event sink can never propagate to
managed side and the whole thing would be useless. You can refer to this
link to see the VC7 implementation.
http://www.codeproject.com/managedcpp/cbwijw.asp

What is MS answer to this? no more callback in VC8?? it cannot be that
idiotic.


Dec 30 '05 #3
Lonewolf wrote:
hi,
thanx for taking the trouble to enlighten me. I have indeed done just
that. However, as i mentioned, this method can't work for callback
fucntions and event sinks to native class which I need to propagate to
the managed class via delegate. I knew this is possible and supported
in VC7 as I already have the code running using oldsyntax in VC8,
However, I would like to know the VC8 equivalent of this technique.
It has to be possible, otherwise, native class' event sink can never
propagate to managed side and the whole thing would be useless. You
can refer to this link to see the VC7 implementation.
http://www.codeproject.com/managedcpp/cbwijw.asp

What is MS answer to this? no more callback in VC8?? it cannot be that
idiotic.


Why require the native class to be nested? Just un-nest it and you should
be fine. You might have to make a function public that you otherwise
wouldn't have, but it should work.

It does seem like a rather pointless restriction in C++/CLI though.
Embedding a native class in a ref class is one thing (clearly not
supported), but why not support the lexical nesting? That has no runtime
significance at all since the nested class would not be visible to the CLR,
and nesting of is no consequence past compile time in the native case.

-cd
Dec 30 '05 #4
"Lonewolf" <an*******@mozilla.org> wrote in message
news:uu**************@tk2msftngp13.phx.gbl...
thanx for taking the trouble to enlighten me. I have indeed done just
that. However, as i mentioned, this method can't work for callback
fucntions and event sinks to native class which I need to propagate to the
managed class via delegate. I knew this is possible and supported in VC7
as I already have the code running using oldsyntax in VC8, However, I
would like to know the VC8 equivalent of this technique. It has to be
possible, otherwise, native class' event sink can never propagate to
managed side and the whole thing would be useless. You can refer to this
link to see the VC7 implementation.
http://www.codeproject.com/managedcpp/cbwijw.asp

What is MS answer to this? no more callback in VC8?? it cannot be that
idiotic.


Is this what you wanted?

http://msdn2.microsoft.com/en-us/library/367eeye0.aspx
Dec 30 '05 #5
James Park wrote:
Is this what you wanted?

http://msdn2.microsoft.com/en-us/library/367eeye0.aspx


Hi,
thanx for the succor. It's working now. However, I do find htis method
a lot more complicated compared to the nested native class method
allowed in VC7. Is there a reason why MS is making this breaking change?
btw, I also realize the String class behaves very differently from VC7
and VC8. I did this,

VC8
~~~~

String^ s;
int a=12;
s="This is a string displaying a number, a="+a;
Debug::Write(s);
//output >> This is a string displaying a number, a=12

VC7
~~~~
String* s;
int a=12;
s="This is a string displaying a number, a="+a; //not allowed ! why?
Debug::Write(s);

it's probably due to me not being familiar with MC++. I came from VC6
and prev experience with .NET was in C# using VS2003. Only now then I
play with VC8's C++/CLI which made things more C++ than MC did. However,
the function ptrs and delegate can be irritating when it broke.
Dec 30 '05 #6
Lonewolf wrote:
Andre Kaufmann wrote:
Lonewolf wrote:

[...]


Try the following:

class CNativeClass {};
ref class CManagedClass{
CNativeClass* a;
//CNativeClass a; -> fails
};
[...]
Andre

hi,
thanx for taking the trouble to enlighten me. I have indeed done
just that. However, as i mentioned, this method can't work for callback
fucntions and event sinks to native class which I need to propagate to
the managed class via delegate. [...]


Ok, I haven't dealt with member function pointers that much. When I
needed callback functions into native code I commonly used interface
pointers for that purpose. Surely they don't map to delegates directly,
as, if I understood it correctly, it could be done in managed C++ by
using a nested class declaration ?

Perhaps the restriction of nested class declaration is a side effect of
the embedded class restrictions introduced in C++/CLI.

You can read more about this issue in:

http://blogs.msdn.com/branbray/archi...20/441099.aspx

Andre
Dec 30 '05 #7
Carl Daniel [VC++ MVP] wrote:
It does seem like a rather pointless restriction in C++/CLI though.
Embedding a native class in a ref class is one thing (clearly not
supported), but why not support the lexical nesting?


The reason is accessibility. As of now, all the member functions for native
classes are implemented as global functions -- they're not really members of
the value class used to encodes the native class. Since they aren't members,
they don't have access to non-public members of the containing class.

As we build better technology, we may move towards emitting member functions
as members of the encoding class. When that happens, we'll lift the
restriction that native classes cannot nest in managed types.

Hope that helps!

--
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
Bugs? Suggestions? Feedback? http://msdn.microsoft.com/productfeedback/
Dec 30 '05 #8
"Brandon Bray [MSFT]" <br******@online.microsoft.com> wrote in message
news:eP**************@TK2MSFTNGP11.phx.gbl...
Carl Daniel [VC++ MVP] wrote:
It does seem like a rather pointless restriction in C++/CLI though.
Embedding a native class in a ref class is one thing (clearly not
supported), but why not support the lexical nesting?


The reason is accessibility. As of now, all the member functions for
native classes are implemented as global functions -- they're not really
members of the value class used to encodes the native class. Since they
aren't members, they don't have access to non-public members of the
containing class.

As we build better technology, we may move towards emitting member
functions as members of the encoding class. When that happens, we'll lift
the restriction that native classes cannot nest in managed types.


That makes sense - and I figured the answer would be something like that.
Personally, I'd rather have 'em supported but simply lacking accessibility -
afterall, the current C++ standard grants no special access to the enclosing
class to members of a nested class. Granted, the next standard (and/or next
TC) removes that oversight.

Thanks for the explanation.

-cd
Dec 30 '05 #9
Lonewolf wrote:
thanx for taking the trouble to enlighten me. I have indeed done
just that. However, as i mentioned, this method can't work for callback
fucntions and event sinks to native class which I need to propagate to
the managed class via delegate.


This is how I do it:

Managed sender, unmanaged receiver:
http://tweakbits.com/ManagedToUnmanagedCallback.cpp

Unmanaged sender, managed receiver:
http://tweakbits.com/UnmanagedToManagedCallback.cpp

Tom
Jan 3 '06 #10

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

Similar topics

4
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...
17
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html Why is C# 500% slower than C++ on Nested Loops ??? Will this problem be solved in...
2
by: Martin Zenkel | last post by:
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...
9
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);
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
3
by: Lonewolf | last post by:
Hi all, I'm having difficulties passing data back to managed class from my native class when the data is generated from within a native thread in the native class itself. I will give the following...
5
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#...
1
by: dogbert1793 | last post by:
Can a ref class have a definition for a native class nested in it? The ref class would also have a native pointer to an instance of the nested class (which I know is ok).
2
by: Bob Altman | last post by:
Hi all, We have a native class modeled after the System::Exception class, and all exceptions that we throw derive from this class. For now this class is quite simple: just Description and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.