A file compiled with /clr is mixed mode: every function is compiled to managed code by default, and the functions that should be native machine code are marked with a pragma. The directives below are the ones that belong to the CLR side of the preprocessor.
Quick reference
| Directive | Meaning |
|---|---|
#using <file>, #using "file" | import the metadata of a .dll, .exe, .netmodule or .obj |
#using <file> as_friend | import with access to the internal types of the assembly (a friend assembly) |
#pragma managed | compile the functions that follow as managed |
#pragma unmanaged | compile the functions that follow as native |
#pragma managed(push, on), (push, off) | save the current state and switch: on is managed, off is unmanaged |
#pragma managed(on), (off) | switch without saving |
#pragma managed(pop) | restore the saved state |
#pragma make_public(type) | give a native type from a header public accessibility in the assembly, so that public managed types can use it |
#pragma init_seg | the order of native or of managed initialisation (within each group only) |
#using
#using <System.dll>
#using <System.Xml.dll>
#using "MyComponent.dll" as_friend
- The directive makes the compiler read the metadata of the file, so that the types in it can be used. If the DLL has an assembly manifest, every DLL that the manifest references is imported as well, and the assembly being built lists the file as a reference.
mscorlib.dllis referenced automatically.- An
.exepassed to#usingmust have been built by a .NET compiler such as C# or Visual Basic: importing an.exethat was itself compiled with/clrfails with a file-load exception. - An alternative to the directive is the
/FUcompiler option, which forces the same import for the whole compilation. - The compiler looks for the file in: the path in the directive; the current directory; the .NET Framework system directory; the directories given with
/AI; and the directories of theLIBPATHenvironment variable. - The component that runs may be a different version from the one imported at compile time, which can give a client unexpected results.
- Types of an assembly (not of a module) are resolved when the compiler needs them: use of a type, or inheriting from it, makes the name known.
- All imported types, managed and native, are available; native types are only declarations.
Managed and unmanaged code in one file
#include <stdio.h>
void func1() { System::Console::WriteLine("managed"); } // managed by default
#pragma managed(push, off)
void func2() { printf("native\n"); } // native machine code
#pragma managed(pop)
int main() { func1(); func2(); } // managed
- With
/clr, functions are managed by default. An unmanaged function is compiled for the processor, and the runtime passes control to it through a thunk. - The pragma goes before a function, never inside one, and after the
#includelines, not before them. - The compiler ignores the pragmas when
/clris not used. - A template function is managed or unmanaged according to the state at the place where the template was defined, not where it is instantiated.
- Managed and native types are chosen by the keyword (
ref classorclass), not by the pragma.
Macros the compiler defines
| Macro | Value |
|---|---|
_MANAGED | 1 when /clr is used |
__cplusplus_cli | defined under /clr (200406) |
_M_CEE | defined under /clr; the older /clr:pure and /clr:safe had _M_CEE_PURE and _M_CEE_SAFE |
A header that is shared with native code tests #ifdef _MANAGED (or __cplusplus_cli) to hide the managed parts.
Not described
Known, not yet covered
- Reference: the macro values and the
_M_CEE_*names are written from memory of the Microsoft predefined-macros page. The rules formake_public(which types it accepts and its effect on the metadata), the/clrrestrictions (options that cannot be combined with it) and the initialization of mixed assemblies were not read.