21.4 Importing Metadata with #using

21.4 Importing Metadata with #using

Visual Studio .NET 2003

A program can directly import metadata contained via the #using directive. Files that contain metadata include:

  • A common language runtime assembly (Section 21).
  • An .exe file from another .NET language.
  • An .obj file from a compilation with the /clr option.
  • A .netmodule file.


// mcpp_import.cpp
// compile with: /clr
// import the common language runtime base class assembly
#using <mscorlib.dll>
using namespace System;

int main() {
   Console::WriteLine("Hello, World"); // defined in mscorlib.dll 


Hello, World


  • #using has the same syntax as #include. Both the <file-name-spec> and "file-name-spec" forms are allowed and have the same effect.
  • The preprocessor searches for file-name-spec in the following order.
    1. If file-name-spec is a fully qualified path name, it will search for the assembly via this path name.
    2. The current working folder, that is, the folder from which the user is running the compiler.
    3. The common language runtime system folder.
    4. The folder specified by the /AI compiler option.
    5. The folders specified by the LIBPATH environment variable.

    This order is followed also if a header file of the program contains a #using directive.


  • There shall be a #using <mscorlib.dll> in any source file that declares or uses a managed class.
  • At runtime, the common language runtime will raise an exception if an assembly is not present in the current working folder, or registered in the Global Assembly Cache (GAC). An assembly can be registered in the GAC using Gacutil.exe.
© 2016 Microsoft