I have read a lot on managed code and unmanaged code,now what I could understand is that any code that targets the .net framework is Managed code (right ),which inturn means its under the control of the CLR @ runtime.
Assuming the above as true, I suppose unmanaged code does not target the framework
or it does not come under the control of CLR @ runtime
Can somebody explain this if possible with code example so that I can undersand the difference...thx.

Managed Code Vs Unmanaged Code?
help-linux
Aleniko29139
Unmanaged Code example like C++, Win32, and COM which compiled into native so they not managed by .net runtime
but as you spend alot of time and money to build them .Net provide Unmanaged Interoperability
so you can use them into your application and here the word Managed are came into the picture
ie the runtime allow to use them but it said i will not managed them its your job to manage them
one service that runtime provide to managed code is Garbage Collector its know how to control the lifetime of .net object and memory
but it did not know how to clear resource allocated by unmanaged code. so this called unmanaged code
and this your responsibility to clear the resource allocated by unmanaged code via implementing IDisposable interface for example
TheViewMaster
Most of time you use Unmanaged Code already exist because you do not want to waste your money and time
Another reason you may write Unmanaged code if you need a feature that is not avilabe through .Net Framework
and it is avilable in windows API for example
steveylevi
Hi,
If you want to understand managed code, you'd first understand what is the .net framework, here is a good reference for you: http://www.codeproject.com/dotnet/DotNetWhitePaper.asp
Managed code: The .NET framework provides several core run-time services to the programs that run within it - for example exception handling and security. For these services to work, the code must provide a minimum level of information to the runtime. Such code is called managed code.
Managed data: This is data that is allocated and freed by the .NET runtime's garbage collector.
Managed classes: This is usually referred to in the context of Managed Extensions (ME) for C++. When using ME C++, a class can be marked with the __gc keyword. As the name suggests, this means that the memory for instances of the class is managed by the garbage collector, but it also means more than that. The class becomes a fully paid-up member of the .NET community with the benefits and restrictions that brings. An example of a benefit is proper interop with classes written in other languages - for example, a managed C++ class can inherit from a VB class. An example of a restriction is that a managed class can only inherit from one base class.
Thanks
0xDEAD
Hi,
For .net interop you can reference to: http://www.dotnetinterop.com/
Otherwise, I recommend you separate further questions in new threads, in that way, more people can see and answer it.
Thank you for understanding.
JWest
Can you give a small example on how this is implemented
And is it possible to find out if the code your currently executing is running as managed or un-managed (Some method in System.Reflection perhaps )
thanks,
mpco
thanks for the replies,
I get the difference now! but this has made me wonder a little, how is it possible to write unmanged code in .net
When ever you write down some code in any .NET IDE ( i use Visual C# Express Edition) it will eventually target the .Net framework when you build it right
And I have never seen any code in VB6 or C in any .Net IDE (not possible at all )
So my question is 'Why really bother about unmanaged code when you cant write it in c# ' ,I guess you can only reference a project to a vb or a c file
Ryan Tessier
Managed code is code that is written to target the services of the managed runtime execution environment (like Common Language Runtime in .NET Framework). The managed code is always executed by a managed runtime execution environment rather than the operating system directly. Managed refers to a method of exchanging information between the program and the runtime environment. Because the execution of code is governed by the runtime environment, the environment can guarantee what the code is going to do and provide the necessary security checks before executing any piece of code. Because of the same reason the managed code also gets different services from the runtime environment like Garbage Collection, type checking, exception handling, bounds checking, etc. This way managed code does not have to worry about memory allocations, type safety, etc. Applications written in Java, C#, VB.NET, etc target a runtime environment which manages the execution and the code written using these types of languages is known as Managed Code. Managed code is always compiled into an Intermediate Language (MSIL in case of .NET Framework). The compiler used by .NET framework to compile managed code compiles it into Intermediate Language and generates the necessary metadata, symbolic information that describes all of the entry points and the constructs exposed in the Intermediate Language (e.g., methods, properties) and their characteristics. The Common Language Infrastructure (CLI) Standard describes how the information is to be encoded, and programming languages that target the runtime emit the correct encoding.
In .NET Framework Managed Code runs within the .Net Framework’s CLR and benefits from the services provided by the CLR. When we compile the managed code, the code gets compiled to an intermediate language (MSIL) and an executable is created. When a user runs the executable the Just In Time Compiler of CLR compiles the intermediate language into native code specific to the underlying architecture. Since this translation happens by the managed execution environment (CLR), the managed execution environment can make guarantees about what the code is going to do, because it can actually reason about it. It can insert traps and sort of protection around, if it's running in a sandboxed environment, it can insert all the appropriate garbage collection hooks, exception handling, type safety, array bounce, index checking and so forth.
Managed code also provides platform independence. As the managed code is first compiled to intermediate language, the CLR’s JIT Compiler takes care of compiling this intermediate language into the architecture specific instructions.
Code that is directly executed by the Operating System is known as un-managed code. Typically applications written in VB 6.0, C++, C, etc are all examples of unmanaged code. Unmanaged code typically targets the processor architecture and is always dependent on the computer architecture. Unmanaged code is always compiled to target a specific architecture and will only run on the intended platform. This means that if you want to run the same code on different architecture then you will have to recompile the code using that particular architecture. Unmanaged code is always compiled to the native code which is architecture specific. When we compile unmanaged code it gets compiled into a binary X86 image. And this image always depends on the platform on which the code was compiled and cannot be executed on the other platforms that are different that the one on which the code was compiled. Unmanaged code does not get any services from the managed execution environment.
In unmanaged code the memory allocation, type safety, security, etc needs to be taken care of by the developer. This makes unmanaged code prone to memory leaks like buffer overruns and pointer overrides and so forth.
Unmanaged executable files are basically a binary image, x86 code, loaded into memory. The program counter gets put there and that’s the last the Operating System knows. There are protections in place around memory management and port I/O and so forth, but the system doesn’t actually know what the application is doing.
Hope to make it clearer, if you have any further problem, pls feel free to let us know, thank you.