Fournit une collection de méthodes pour l'allocation de mémoire non managée, la copie de blocs de mémoire non managée et la conversion de types managés en types non managés, ainsi que diverses autres méthodes utilisées lors de l'interaction avec du code non managé.
Espace de noms : System.Runtime.InteropServices
Assembly : mscorlib (dans mscorlib.dll)

Syntaxe
Visual Basic (Déclaration)
Public NotInheritable Class Marshal
Visual Basic (Utilisation)
Il est possible d'accéder aux membres d'une classe statique directement sans une instance de la classe.
public static class Marshal
public ref class Marshal abstract sealed
public final class Marshal
public final class Marshal

Notes
Les méthodes static définies sur la classe Marshal sont essentielles pour le traitement du code non managé. La plupart des méthodes définies dans cette classe sont généralement utilisées par des développeurs devant constituer une passerelle entre des modèles de programmation managés et non managés. Par exemple, la méthode StringToHGlobalAnsi copie des caractères ANSI d'une chaîne spécifiée (dans le tas managé) dans une mémoire tampon du tas non managé. Elle alloue également le tas cible de la taille correcte.
Le Common Language Runtime fournit des fonctionnalités de marshaling spécifiques. Pour plus d'informations sur le comportement du marshaling, consultez Marshaling d'interopérabilité.

Exemple
L'exemple de code suivant montre comment utiliser différentes méthodes définies par la classe Marshal.
using System;
using System.Text;
using System.Runtime.InteropServices;
public struct Point
{
public Int32 x, y;
}
public sealed class App
{
static void Main()
{
// Demonstrate the use of public static fields of the Marshal class.
Console.WriteLine("SystemDefaultCharSize={0}, SystemMaxDBCSCharSize={1}",
Marshal.SystemDefaultCharSize, Marshal.SystemMaxDBCSCharSize);
// Demonstrate the use of the SizeOf method of the Marshal class.
Console.WriteLine("Number of bytes needed by a Point object: {0}",
Marshal.SizeOf(typeof(Point)));
Point p = new Point();
Console.WriteLine("Number of bytes needed by a Point object: {0}",
Marshal.SizeOf(p));
// Demonstrate how to call GlobalAlloc and
// GlobalFree using the Marshal class.
IntPtr hglobal = Marshal.AllocHGlobal(100);
Marshal.FreeHGlobal(hglobal);
// Demonstrate how to use the Marshal class to get the Win32 error
// code when a Win32 method fails.
Boolean f = CloseHandle(new IntPtr(-1));
if (!f)
{
Console.WriteLine("CloseHandle call failed with an error code of: {0}",
Marshal.GetLastWin32Error());
}
}
// This is a platform invoke prototype. SetLastError is true, which allows
// the GetLastWin32Error method of the Marshal class to work correctly.
[DllImport("Kernel32", ExactSpelling = true, SetLastError = true)]
static extern Boolean CloseHandle(IntPtr h);
}
// This code produces the following output.
//
// SystemDefaultCharSize=2, SystemMaxDBCSCharSize=1
// Number of bytes needed by a Point object: 8
// Number of bytes needed by a Point object: 8
// CloseHandle call failed with an error code of: 6
using namespace System;
using namespace System::Runtime::InteropServices;
public value struct Point
{
public:
property int X;
property int Y;
};
extern bool CloseHandle(IntPtr h);
int main()
{
// Demonstrate the use of public static fields of the Marshal
// class.
Console::WriteLine(
"SystemDefaultCharSize={0},SystemMaxDBCSCharSize={1}",
Marshal::SystemDefaultCharSize,
Marshal::SystemMaxDBCSCharSize);
// Demonstrate the use of the SizeOf method of the Marshal
// class.
Console::WriteLine("Number of bytes needed by a Point object: {0}",
Marshal::SizeOf(Point::typeid));
Point point;
Console::WriteLine("Number of bytes needed by a Point object: {0}",
Marshal::SizeOf(point));
// Demonstrate how to call GlobalAlloc and
// GlobalFree using the Marshal class.
IntPtr hglobal = Marshal::AllocHGlobal(100);
Marshal::FreeHGlobal(hglobal);
// Demonstrate how to use the Marshal class to get the Win32
// error code when a Win32 method fails.
bool isCloseHandleSuccess = CloseHandle(IntPtr(-1));
if (!isCloseHandleSuccess)
{
Console::WriteLine(
"CloseHandle call failed with an error code of: {0}",
Marshal::GetLastWin32Error());
}
};
// This is a platform invoke prototype. SetLastError is true,
// which allows the GetLastWin32Error method of the Marshal class
// to work correctly.
[DllImport("Kernel32", ExactSpelling = true, SetLastError = true)]
extern bool CloseHandle(IntPtr h);
// This code produces the following output.
//
// SystemDefaultCharSize=2, SystemMaxDBCSCharSize=1
// Number of bytes needed by a Point object: 8
// Number of bytes needed by a Point object: 8
// CloseHandle call failed with an error code of: 6

Hiérarchie d'héritage

Sécurité des threads
Les membres statiques publics (
Shared en Visual Basic) de ce type sont thread-safe. Il n'est pas garanti que les membres d'instance soient thread-safe.

Plates-formes
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile pour Pocket PC, Windows Mobile pour Smartphone, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition
Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.

Informations de version
.NET Framework
Prise en charge dans : 2.0, 1.1, 1.0
.NET Compact Framework
Prise en charge dans : 2.0, 1.0

Voir aussi