1. unsafe與unmanaged的區別
managed code是在CLR監管下啟動並執行程式。以下任務由CLR來執行:管理對象記憶體,型別安全檢測和冗餘處理。從另一方面來說,unmanaged code也就是能由程式員直接進行記憶體操作的程式。而unsafe是介於managed和unmanaged之間的橋樑,它使得managed code也能使用指標來控制和操作記憶體。
2. managed, unmanaged
Unmanaged code is the good old C++ with no CLR support, therefore unmanaged code does not have a garbage collector and you will have to keep track of all your memory allocations to avoid memory leaks. Also when you build an unmanaged project in Visual Studio, the resulting library or executable is written directly on machine code, therefore it doesn't need the .NET Framework to run.
The managed C++ has CLR support and its code is written in an extension of the C++ language called C++/CLI, this extension allows you to use the Garbage Collector and the .NET Framework classes.
Also when you build a managed project in Visual Studio, the resulting library or executable is written in CLR code (which is then translated to machine code by the CLR), therefore it needs the .NET Framework to run.
Usually you can choose to write managed or unmanaged C++ code when you create a Visual Studio project, but you can also add or remove CLR support from your project whenever you wish.
Finally if you choose to use unmanaged code (and are using Visual Studio 2005 or above), keep in mind that when you distribute your application you will also need to include the Visual C++ Redistributable Package, otherwise your application will not work (this is not required if you intend to run your application on a computer that already has Visual Studio installed).
3. Enable function-level control for compiling functions as managed or unmanaged.
#pragma managed
#pragma unmanaged
#pragma managed([push,] on | off)
#pragma managed(pop)
4. Remarks
The /clr compiler option provides module-level control for compiling functions either as managed or unmanaged.
An unmanaged function will be compiled for the native platform, and execution of that portion of the program will be passed to the native platform by the common language runtime.
Functions are compiled as managed by default when /clr is used.
Use the following guidelines when applying these pragmas:
Add the pragma preceding a function but not within a function body.
Add the pragma after #include statements (do not use these pragmas before #include statements).
The compiler ignores the managed and unmanaged pragmas if /clr is not used in the compilation.
When a template function is instantiated, the pragma state at the time of definition for the template determines if it is managed or unmanaged.
For more information, see Initialization of Mixed Assemblies.
5. Example
託管、非託管的混合代碼
1. 建立C++ CLR新項目
2. 更改項目屬性(Common Language Runtime 支援 (/clr))
// pragma_directives_managed_unmanaged.cpp// compile with: /clr#include <stdio.h>// func1 is managedvoid func1() { System::Console::WriteLine("In managed function.");}// #pragma unmanaged// push managed state on to stack and set unmanaged state#pragma managed(push, off)// func2 is unmanagedvoid func2() { printf("In unmanaged function.\n");}// #pragma managed#pragma managed(pop)using namespace System;// main is managedint main() { func1(); func2(); Console::ReadKey();}