Question about managed / unmanaged instructions

N

nicolas.hilaire

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
framework ? what is managed / unmanaged ?

- i've a winforms app, compiling with /clr:pure , i'm calling such a
function :
int add(int a, int b)
{
return a+b;
}
is this functions moved to CLR ? is this function managed / unmanaged ?

- In addition, in the winform app with /clr:pure, i'm creating a class
without the ref keyword, is this class unmanaged ? moved to clr anyway
?

- i'm creating a winforms app. I want to use a unmanaged class (without
the ref keyword), between #pragma unmanaged and #pragma managed . Is
this class unmanaged ? Is it in the CLR ? Was it useful to use this
pragma or would have been enough to only not use the ref keyword ?

Thanks a lot in advance for explaning me this better

Best regards,

Nicolas H.
 
G

Guest

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
framework ? what is managed / unmanaged ?
By compiling an MFC app with CLR you have created a mixed assembly of
managed and unmanaged code. MFC is MFC, it's native. There's no native MFC
and .NET MFC. However, mew code that you add to the app that is not on natove
classes will be managed. This means that code you add to MFC-derived classes
will still be native but you can add new managed classes by declaring them
"ref" or "value".
- i've a winforms app, compiling with /clr:pure , i'm calling such a
function :
int add(int a, int b)
{
return a+b;
}
is this functions moved to CLR ? is this function managed / unmanaged ?
If it's a Managed WinForms app and the function is in a managed class
(either the generated class or a new class you declared as ref or value) then
yes the code is managed/CLR.
- In addition, in the winform app with /clr:pure, i'm creating a class
without the ref keyword, is this class unmanaged ? moved to clr anyway
?
If it's not declared "value" then since it's also not "ref", it's native.
- i'm creating a winforms app. I want to use a unmanaged class (without
the ref keyword), between #pragma unmanaged and #pragma managed . Is
this class unmanaged ? Is it in the CLR ? Was it useful to use this
pragma or would have been enough to only not use the ref keyword ?
Not using "ref" and not using "value" makes the class native so the #pragmas
are not needed. The #pragmas seem to be mainly used for global/module scope
functions.
 
N

nicolas.hilaire

thanks for you answer,

If i want to mix managed code and MFC, for example like this :

class CMfcSerialisationApp : public CWinApp
{
public:
CMfcSerialisationApp();
gcroot<MyObjManaged ^> myObjManaged;

virtual BOOL InitInstance();
DECLARE_MESSAGE_MAP()
};

public ref class MyObjManaged
{
public:
System::Collections::ArrayList ^ list;
MyObjManaged (void);
};


I want to use a managed object in my unmanaged class, with the gcroot
keyword.
In this case, is my unmanaged class still unmanaged ? How can it use
the managed object (with a wrapper ?) ? Is this code in clr ?

Best,

Nicoas H.
 
J

Jochen Kalmbach [MVP]

Hi nicolas!
In this case, is my unmanaged class still unmanaged ? How can it use
the managed object (with a wrapper ?) ? Is this code in clr ?

The class itself is a unmanaged class (meaning: it is not managed by the
GC). But all your code in all your file is managed (or better MSIL).
So therefor you can access every managed object from everywhere in your
application.

If you really want "unmanaged" code (meaning x86/x64/IA64) you need to
use the
#pragma unmanaged
to force the generation of unmanaged code.
But inside such a block, you cannot call any managed functions.

You also can disable the /clr switch on a per-file basis in your project
settings.

But every file which is compiled with /clr contains MSIL code and no x86
code.


If you link against LIBs then the LIB defines if the method is managed
or unmanged.




--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
N

nicolas.hilaire

Thanks for your answer Jochen,

Let me know if i'm fully understanding :

- CLR manage object that use the ref keyword
- The other object are not managed, and are "native"

Should i think about managed / unmanaged only for object ? or for
statements too ?

Greetings,

Nicolas H.
 
J

Jochen Kalmbach [MVP]

Hi nicolas!
- CLR manage object that use the ref keyword

Nout really...
The CLR is the Common Language Runtime. It is fundamental to all managed
stuff.
The CLR also provides a garbage collection (GC). Managed objects which
are created on the heap are "managed" by the GC.
- The other object are not managed, and are "native"

No. objects without the "ref" keyword are just not managed by the GC.
But they might also use the CLR.
Should i think about managed / unmanaged only for object ? or for
statements too ?

There is a bit confusing about the word "managed". Some people only mean
the GC and some mean the CLR...

If you compile wour C++ files with /clr then all code will be managed
(MSIL). Only those function which were added via (native) LIBs will be
unmanaged.

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
N

nicolas.hilaire

Thanks Jochen for your answer.

This is much clearer for me now

Best regards,

Nicolas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top