473,320 Members | 2,052 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,320 software developers and data experts.

Managed method in Native Class ?

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);

};

I get an error when attempting to compile above about #pragma managed
must be used at global scope!

Is there some way to achieve this?
If not, why not?

Thanks in advance.

Dec 9 '05 #1
9 2032
Its interesting to note that given

//myClass.h

class myClass
{
public:
void myMethod(void);
};

And the following module compiled as \clr
// myClass.cpp
#pragma unmanaged
void myClass::myMethod(void)
{
}

works correctly, if i put any managed data in here, will generate
errors.

But if i compile the module unmanaged with a \clr project and then

// myClass.cpp
#pragma managed
void myClass::myMethod(void)
{
}

Will not work. Big shame for me!
Why cannot this be made to work?

Please help...

Dec 9 '05 #2

"Herby" <pr********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Its interesting to note that given

//myClass.h

class myClass
{
public:
void myMethod(void);
};

And the following module compiled as \clr
// myClass.cpp
#pragma unmanaged
void myClass::myMethod(void)
{
}

myMethod is a member of a native C++ class, the code generated for the
method is managed code (IL), the object data members (there aren't any) is
native data.
The class instance is on the process heap just like a regular C++ objects,
the members occupy a slot in the objects vtable laid-out by the compiler.
So this is quite normal that it works.
works correctly, if i put any managed data in here, will generate
errors.

But if i compile the module unmanaged with a \clr project and then

// myClass.cpp
#pragma managed
void myClass::myMethod(void)
{
}

Will not work. Big shame for me!
Why cannot this be made to work?

This can't work, a managed method cannot be a member of a native class.
Managed methods must be members of a managed class, stored on the GC heap
and laid-out by the CLR.
Please read more about the difference between object models in msdn.

Please help...

Dec 9 '05 #3
Perhaps I am being misunderstood.

If i compile the module containing my native class with \clr, then
within my native methods i can create and use managed types, by
default. This gives me mixed mode type functionality, which is what i
require.
I only want to do this within one of the methods of my native class.
I want the remainder methods to remain native. They have no need to
access managed types.

So for all the other methods i would then have to precede them with
#pragma unmanaged to ensure they stay native given the module has been
compiled \clr.
This is alot of editing, i just want to do the inverse.
Default to compiling them native unless you hit #pragma managed.

I dont see the point in #pragma managed if it can only be used within a
module compiled with \clr?

Dec 9 '05 #4

"Herby" <pr********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Perhaps I am being misunderstood.

If i compile the module containing my native class with \clr, then
within my native methods i can create and use managed types, by
default. This gives me mixed mode type functionality, which is what i
require.
I only want to do this within one of the methods of my native class.
I want the remainder methods to remain native. They have no need to
access managed types.

But a method in a native class must be a native method, it can't be a
managed method. You aren't fully understanding the meaning of mixed mode I
guess. Mixed mode means that you can have native classes and managed classes
in the same compilation unit, but you can't have native member functions in
managed classes, nor can you have managed methods as members of native
classes. The reason is the fundamental difference between the managed object
model and the unmanaged object model.
So for all the other methods i would then have to precede them with
#pragma unmanaged to ensure they stay native given the module has been
compiled \clr.
The pragma can be a pplied anywhere except inside a class definition.

#pragma unmanaged
class MyClass
{
public:
void MyNativeMethod(); //must be a native function
int i;
...
}

#pragma managed
ref class MyRefClass
{

public:
void MyManagedMethod(); //must be a managed function
int i;

};

In above sample MyNativeMethod will be compiled as native code, the class
instance will be stored in the process heap.
MyManagedMethod will be compiled as managed code and the instance of the
class will end on the GC heap.
This is alot of editing, i just want to do the inverse.
Default to compiling them native unless you hit #pragma managed.

Not sure why you wan't some classes to be unmmanaged while others should be
managed, anyway if you compile your program with /clr without any pragma
unmanaged in the compiland, all code will be compiled to IL, will all data
members an the object instances will be treated as native objects.
And this is what is basically mixed mode.
I dont see the point in #pragma managed if it can only be used within a
module compiled with \clr?


Don't get this one, sorry.
Willy.
Dec 9 '05 #5
Thanks Willy.

You know my misunderstanding may be in the use of the #pragma
directive.
Iv not had much need for them in the past, other than now.

#pragma unmanaged
Will remain in effect until until it hits
#pragma managed
within the scope of a module

So if i compile my native class with \clr then the code contained
within the methods will be IL and
within these methods i could create and use managed types.

So i could have

#pragma unmanaged

void MyClass::method1(){
}

void MyClass::method2(){
}

#pragma managed
void MyClass::NewMethod3(){

// Can now access BCL here

String^ str = new String;
}

Correct?


}

Dec 9 '05 #6

"Herby" <pr********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Thanks Willy.

You know my misunderstanding may be in the use of the #pragma
directive.
Iv not had much need for them in the past, other than now.

#pragma unmanaged
Will remain in effect until until it hits
#pragma managed
within the scope of a module

So if i compile my native class with \clr then the code contained
within the methods will be IL and
within these methods i could create and use managed types.

So i could have

#pragma unmanaged

void MyClass::method1(){
}

void MyClass::method2(){
}

#pragma managed
void MyClass::NewMethod3(){

// Can now access BCL here

String^ str = new String;
}

Correct?


}


Exactly, but it is not that restrictive though, you can also access non
managed types from managed types or unmanaged types from managed types.

ref class A {
public:
...
void ManagedFunction();
...
private:
MyClass *unm;
}

A::ManagedFunction()
{
unm->method1(); // call your native class function
}
Only thing you can't do is 'embed' mixed types (not yet, maybe in some later
release).

Willy.

Dec 9 '05 #7
Thanks Willy.

My specific problem is I want to serialize a parallel .NET object graph
derived from my current MFC objects.
This will then be used in a new .NET application which compiles with
\clr safe
I want to keep the original source application native. Only selectively
using managed code where absolutely necessary.

E.g.

compile UnManagedClass.cpp as \clr

#pragma unmanaged

// All methods here compile to pure native ... As they always were

// Each class participating in the conversion to .NET will implement
the following new method(interface)
// which must be managed for reasons already discussed.
#pragma managed
UnManagedClass::Convert2DotNet( DotNetConversionContext& context )
{
ManagedClass^ pMC = gcnew ManagedClass;
/// set properties on pMc from 'this' native data members
context.Add( pMC );
}

Its only this method i want compiled as managed everything else stays
the same.
So, i do not require mixed type classes as i have no need to maintain
state.

Dec 9 '05 #8
Herby wrote:
Thanks Willy.

My specific problem is I want to serialize a parallel .NET object graph
derived from my current MFC objects.
This will then be used in a new .NET application which compiles with
\clr safe
I want to keep the original source application native. Only selectively
using managed code where absolutely necessary.

E.g.

compile UnManagedClass.cpp as \clr

#pragma unmanaged

// All methods here compile to pure native ... As they always were

// Each class participating in the conversion to .NET will implement
the following new method(interface)
// which must be managed for reasons already discussed.
#pragma managed
UnManagedClass::Convert2DotNet( DotNetConversionContext& context )
{
ManagedClass^ pMC = gcnew ManagedClass;
/// set properties on pMc from 'this' native data members
context.Add( pMC );
}

Its only this method i want compiled as managed everything else stays
the same.
So, i do not require mixed type classes as i have no need to maintain
state.

Hi,

What you said in this thread is correct. #pragma managed has no meaning
in a file not compiled with the /clr switch. The compiler needs to know
ahead of time (before it starts processing the file) that it will
encounter managed blocks since tha implies it needs to generate metadata
. That happens even in #pragma unmanaged blocks.

E.g. in your example the compiler needs metadata (even just the opaque
valuetype definitions it generates for native classes) for class
UnManagedClass in order to generate the method correctly in managed code.

So there is a clear difference between a file that is not compiled /clr
and one that is compiled /clr but has #pragma unmanaged as its first
line: the first does not generate any metadata, the second does.

Ronald Laeremans
Visdual C++ team
Dec 9 '05 #9
Thanks Ronald, im clear now.

Dec 9 '05 #10

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

Similar topics

6
by: Shai Levi | last post by:
Hi, I'm trying to migrate native c++ class to managed c++ class. The native class header definition looks as: class NativeClass { public: typedef void (CbFunc1)(int n,void* p);
2
by: mats | last post by:
Hi! This is a quite involved question concerning two dll's that are consumed from a console application. The first dll is called base.dll and is compiled in mixed mode (managed and unmanaged...
1
by: Bern McCarty | last post by:
What do you make of this? I cannot tell for sure but it almost seems as the the transition thunk to get back from the native bool method to the managed caller is looking at eax and, if any bit is...
3
by: Dave | last post by:
I've seen at least one article on this: How to access classes, etc. managed-to-unmanaged. I can't find it. Any tricks or pointers? I guess the class name won't get munged but I'm not clear about...
9
by: Lonewolf | last post by:
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...
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...
2
by: Dave Burns | last post by:
I have the following situation: There are two dlls in question: a c++ dll (native) and a c++/cli dll (managed) with /clr option enabled. Managed dll links with native dll. Native dll...
16
by: pkoniusz | last post by:
Hello everybody, Been just thinking how actually one could convert the following unmanaged code to the managed C++: struct JustAnExample { char value1; int value2; // etc ....
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.