What is managed and unmanaged code?

Managed code

Managed code is created by the compailer from C# and VB.NET code that compails it to IL (Intermediate Language). This code is executed directly by CLR (Common Language Runtime) not by  the Operating System. Managed code that is runs on the CLR provide services such as memory management (garbage collection), run-time type checking, cross-language integration and code access security. This code doesn’t depend on machine configurations – it’s mean that can be executed on different machines. Managed code is  executes under the CLI (Common Language Infrastructure) execution environment.

Managed code is only produced only by the C# and Visual Basic .NET languages.

You can produce managed code in C++.NET but it is optional and not set by default.

Unmanaged code

Unmanaged code is the code that is executed by the Operating System and is compiles straight to machine code. This code depend on machine configuration. All features provided by the CLR are not available.

All code compiled by the C/C++ compailers is and “unmanaged code”.

Mixing with Managed and Unmanaged codes

When you write in C++ unter the CLI you can decide if use managed or unmanaged code by using the directives:

#using <mscorlib.dll>
using namespace System;
#include "stdio.h"

void ManagedFunction()
    printf("Managed function\n");

#pragma unmanaged
void UnmanagedFunction()
    printf("Unmanaged function!\n");

#pragma managed
int main()
    return 0;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s