PInvoke (Platform Invocation Services) lets you execute existing, unmanaged code, that lives in a regular DLL, from managed code.
Object
Marshal
[Visual Basic] NotInheritable Public Class Marshal [C#] public sealed class Marshal [C++] public __gc __sealed class Marshal [JScript] public class Marshal
The Marshal Class described here provides a collection of useful functions when working in this mixed, managed/unmanaged mode. Note that all of the methods in the Marshal Class are declared as static. So, you don't ever create an instance of a Marshal object. You just use the functions you need, straight off. For example, the StringToHGlobalAnsi method copies the Ansi characters from a specified String (in managed heap) to a buffer in unmanaged heap. It also allocates the target heap for you, of just the right size. Like this:
< p > String s = "Hello";
int p = Marshal.StringToHGlobalAnsi(s);
In general, Marshal methods return integers where you would normally expect a pointer. Simply cast the integer to the appropriate pointer type, and carry on.
Namespace: System.Runtime.InteropServices
Assembly: mscorlib.dll