What is managed or unmanaged code in programming?
Asked Answered
N

13

178

I am using a specific command in in my C# code, which works well. However, it is said to misbehave in "unmanaged" code.

What is managed or unmanaged code?

Nat answered 2/12, 2008 at 15:31 Comment(1)
T
80

Here is some text from MSDN about unmanaged code.

Some library code needs to call into unmanaged code (for example, native code APIs, such as Win32). Because this means going outside the security perimeter for managed code, due caution is required.

Here is some other complimentary explication about Managed code:

  • Code that is executed by the CLR.
  • Code that targets the common language runtime, the foundation of the .NET Framework, is known as managed code.
  • Managed code supplies the metadata necessary for the CLR to provide services such as memory management, cross-language integration, code access security, and automatic lifetime control of objects. All code based on IL executes as managed code.
  • Code that executes under the CLI execution environment.

For your problem:

I think it's because NUnit execute your code for UnitTesting and might have some part of it that is unmanaged. But I am not sure about it, so do not take this for gold. I am sure someone will be able to give you more information about it. Hope it helps!

Thanos answered 2/12, 2008 at 15:32 Comment(1)
I appreciate the effort going into your answer. but folks who don't know what "managed code is" are going to struggle with what you mean in your answer: they won't know: "library code", "unmanaged code", "native code API", CLR, the foundation of the .net framework, IL, CLI execution environment etc etcColt
M
230

This is a good article about the subject.

To summarize,

  1. Managed code is not compiled to machine code but to an intermediate language which is interpreted and executed by some service on a machine and is therefore operating within a (hopefully!) secure framework which handles dangerous things like memory and threads for you. In modern usage this frequently means .NET but does not have to.

An application program that is executed within a runtime engine installed in the same machine. The application cannot run without it. The runtime environment provides the general library of software routines that the program uses and typically performs memory management. It may also provide just-in-time (JIT) conversion from source code to executable code or from an intermediate language to executable code. Java, Visual Basic and .NET's Common Language Runtime (CLR) are examples of runtime engines. (Read more)

  1. Unmanaged code is compiled to machine code and therefore executed by the OS directly. It therefore has the ability to do damaging/powerful things Managed code does not. This is how everything used to work, so typically it's associated with old stuff like .dlls.

An executable program that runs by itself. Launched from the operating system, the program calls upon and uses the software routines in the operating system, but does not require another software system to be used. Assembly language programs that have been assembled into machine language and C/C++ programs compiled into machine language for a particular platform are examples of unmanaged code.(Read more)

  1. Native code is often synonymous with Unmanaged, but is not identical.
Midships answered 2/12, 2008 at 15:39 Comment(5)
you mean that in the hacking we can't use .net langauge(C#, C++), right?Astronautics
@H_wardak what do you define as 'hacking'? it's a very general term, it's like saying hacking into NORAD and hacking some registers are the same.Scheider
I have been once asked by interviewer ,can we run/write unmanaged code in C#? Can anyone help on this?Hypochondria
@RSB: You cannot write unmanaged code in C# (although, you can directly call unmanaged code from C#). Theoretically, with the right compiler and framework, you could do it, I guess. In practice, that means you'd need a compiler that could compile C# to machine code directly. Not sure how that would work.Mastoiditis
@JamesHaug You can now using the unsafe keyword.Bigelow
T
80

Here is some text from MSDN about unmanaged code.

Some library code needs to call into unmanaged code (for example, native code APIs, such as Win32). Because this means going outside the security perimeter for managed code, due caution is required.

Here is some other complimentary explication about Managed code:

  • Code that is executed by the CLR.
  • Code that targets the common language runtime, the foundation of the .NET Framework, is known as managed code.
  • Managed code supplies the metadata necessary for the CLR to provide services such as memory management, cross-language integration, code access security, and automatic lifetime control of objects. All code based on IL executes as managed code.
  • Code that executes under the CLI execution environment.

For your problem:

I think it's because NUnit execute your code for UnitTesting and might have some part of it that is unmanaged. But I am not sure about it, so do not take this for gold. I am sure someone will be able to give you more information about it. Hope it helps!

Thanos answered 2/12, 2008 at 15:32 Comment(1)
I appreciate the effort going into your answer. but folks who don't know what "managed code is" are going to struggle with what you mean in your answer: they won't know: "library code", "unmanaged code", "native code API", CLR, the foundation of the .net framework, IL, CLI execution environment etc etcColt
A
70

When you think of unmanaged, think machine-specific, machine-level code. Like x86 assembly language. Unmanaged (native) code is compiled and linked to run directly on the processor it was designed for, excluding all the OS stuff for the moment. It's not portable, but it is fast. Very simple, stripped down code.

Managed code is everything from Java to old Interpretive BASIC, or anything that runs under .NET. Managed code typically is compiled to an intermediate level P-Code or byte code set of instructions. These are not machine-specific instructions, although they look similar to assembly language. Managed code insulates the program from the machine it's running on, and creates a secure boundary in which all memory is allocated indirectly, and generally speaking, you don't have direct access to machine resources like ports, memory address space, the stack, etc. The idea is to run in a more secure environment.

To convert from a managed variable, say, to an unmanaged one, you have to get to the actual object itself. It's probably wrapped or boxed in some additional packaging. UNmanaged variables (like an 'int', say) - on a 32 bit machine - takes exactly 4 bytes. There is no overhead or additional packaging. The process of going from managed to unmanaged code - and back again - is called "marshaling". It allows your programs to cross the boundary.

Artiste answered 4/9, 2011 at 6:57 Comment(1)
How then does mashalling interact with value and reference types? I remember seeing something about MarshalByRefObject, for example.Sd
N
25

In as few words as possible:

  • managed code = .NET programs
  • unmanaged code = "normal" programs
Neustria answered 2/12, 2008 at 15:42 Comment(2)
a .NET program isn't "normal"?Chacha
@Chacha - That's dumbing it down a bit, yes. :) I was trying to make it more intuitive. Anyway, that was over 8 years ago now. Today with the myriad of programming languages in common everyday usage, this distinction is indeed even more imprecise, yes.Neustria
L
16

Managed code is what C#.Net, VB.Net, F#.Net etc compilers create. It runs on the CLR, which among other things offers services like garbage collection, reference checking, and much more. So think of it as, my code is managed by the CLR.

On the other hand, unmanaged code compiles straight to machine code. It's not managed by the CLR.

Lawabiding answered 22/12, 2018 at 9:4 Comment(0)
Q
4

Basically unmanaged code is code which does not run under the .NET CLR (aka not VB.NET, C#, etc.). My guess is that NUnit has a runner/wrapper which is not .NET code (aka C++).

Queenstown answered 2/12, 2008 at 15:36 Comment(0)
D
4
  • Managed Code: code written in .NET language like C#, VB.NET.
  • UnManaged Code: code not written in .NET language and MSIL does not understand what it is and can not run under CLR; like third-party controls we used in our .NET applications which is not created in .NET languages.
Desmond answered 19/12, 2013 at 2:1 Comment(0)
U
4

Managed Code:
Code that runs under a "contract of cooperation" with the common language runtime. Managed code must supply the metadata necessary for the runtime to provide services such as memory management, cross-language integration, code access security, and automatic lifetime control of objects. All code based on Microsoft intermediate language (MSIL) executes as managed code.

Un-Managed Code:
Code that is created without regard for the conventions and requirements of the common language runtime. Unmanaged code executes in the common language runtime environment with minimal services (for example, no garbage collection, limited debugging, and so on).

Reference: http://www.dotnetspider.com/forum/11612-difference-between-managed-and-unmanaged-code.aspx

Undergraduate answered 28/5, 2014 at 19:43 Comment(0)
I
3

NUnit loads the unit tests in a seperate AppDomain, and I assume the entry point is not being called (probably not needed), hence the entry assembly is null.

Intemerate answered 2/12, 2008 at 15:40 Comment(0)
K
2

Managed code runs inside the environment of CLR i.e. .NET runtime.In short all IL are managed code.But if you are using some third party software example VB6 or VC++ component they are unmanaged code as .NET runtime (CLR) does not have control over the source code execution of the language.

Klehm answered 9/10, 2010 at 6:9 Comment(0)
V
1

Managed Code :- Code which MSIL (intermediate language) form is developed after the language compiler compilation and directly executed by CLR called managed code. eg:- All 61 language code supported by .net framework

Unmanaged Code:- code that developed before .net for which MSIL form is not available and it is executed by CLR directly rather CLR will redirect to operating system this is known as unmanaged code.

eg:-COM,Win32 APIs

Versus answered 24/7, 2013 at 14:46 Comment(1)
There are a number of mistakes in this post. Most obviously MISL (you mean MSIL).Madalene
H
0

From Pro C# 5 and the .NET 4.5 Framework:

Managed vs. Unmanaged Code: Perhaps the most important point to understand about the C# language is that it can produce code that can execute only within the .NET runtime (you could never use C# to build a native COM server or an unmanaged C/C++ application). Officially speaking, the term used to describe the code targeting the .NET runtime is managed code. The binary unit that contains the managed code is termed an assembly (more details on assemblies in just a bit). Conversely, code that cannot be directly hosted by the .NET runtime is termed unmanaged code.

Hexastyle answered 11/4, 2016 at 9:36 Comment(0)
M
-1

First of all understand this, before .NET framework, Microsoft were providing the stand-alone products like MFC (Visual C++), VB, FoxPro etc.

In 2002, Microsoft combined its products and made .NET framework. Now there is a difference between how code was executed before and how code is managed and executed in .NET framework. Microsoft introduced concept of CLR with .NET framework which compiles the code coming from any supported lanugague of .NET framework and provide additional functionalities like memory mangement, garbage collection etc. But, such CLR features weren't available directly before.

So if you are creating library/code in .NET framework (compiled with CLR) then that is called Managed code. You can use this library further in other .NET application/project, and there too, CLR will understand how it was compiled before, and that's why it remains your manage code.

OTOH if you want to use the libraries that were written prior to .NET framework then you can do with certain limitations, but remember, since CLR wasn't there at that time, so now, CLR won't understand and compile this code again. And this will be called unmanaged code. Please note that, libraries/assembilies created by some third party to provide certain features/tool may also be considered as unmanage code if is not CLR compatiblie.

In laymen terms, Manage code is something which your CLR understands and can compile it on its own for further execution. In .NET framework, (from any language thats work on .NET framework) When code goes to CLR then code supply some meta data information, so that CLR can provide you features specified here. Few of them are Garbage collection, Performance improvements, cross-language integration, memory management etc.

OTOH, unmanged code is something specific to machine and ready to use, no need to process it further.

Moskowitz answered 24/8, 2015 at 6:8 Comment(1)
Lots and lots and lots of uninformed opinion, I'm afraid. This may come as a surprise, but the CLR can execute unmanaged code (usually written in C++/CLI) as well. There's also no requirement for managed code to be available as IL. .NET Native has been around for a while now, and it comes with pre-compiled assemblies. What you called "CLR compatible" is probably meant to be "CLS compliant". Failure to meet CLS compliance doesn't make managed code unmanaged, all of a sudden. Consuming unmanaged code - despite your description - is pretty easy as well (RCW over COM, P/Invoke, C++/CLI, etc.).Layette

© 2022 - 2024 — McMap. All rights reserved.