Windows Dev Center

IntPtr Structure

A platform-specific type that is used to represent a pointer or a handle.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

[SerializableAttribute]
[ComVisibleAttribute(true)]
public struct IntPtr : ISerializable

The IntPtr type exposes the following members.

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryIntPtr(Int32)Initializes a new instance of IntPtr using the specified 32-bit pointer or handle.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryIntPtr(Int64)Initializes a new instance of IntPtr using the specified 64-bit pointer.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryIntPtr(Void*)Initializes a new instance of IntPtr using the specified pointer to an unspecified type.
Top

  NameDescription
Public propertyStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySizeGets the size of this instance.
Top

  NameDescription
Public methodStatic memberSupported by Portable Class LibraryAddAdds an offset to the value of a pointer.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryEqualsReturns a value indicating whether this instance is equal to a specified object. (Overrides ValueType.Equals(Object).)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetHashCodeReturns the hash code for this instance. (Overrides ValueType.GetHashCode().)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodStatic memberSupported by Portable Class LibrarySubtractSubtracts an offset from the value of a pointer.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryToInt32Converts the value of this instance to a 32-bit signed integer.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryToInt64Converts the value of this instance to a 64-bit signed integer.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryToPointerConverts the value of this instance to a pointer to an unspecified type.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryToString()Converts the numeric value of the current IntPtr object to its equivalent string representation. (Overrides ValueType.ToString().)
Public methodSupported by Portable Class LibraryToString(String)Converts the numeric value of the current IntPtr object to its equivalent string representation.
Top

  NameDescription
Public operatorStatic memberSupported by Portable Class LibraryAdditionAdds an offset to the value of a pointer.
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryEqualityDetermines whether two specified instances of IntPtr are equal.
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryExplicit(Int32 to IntPtr)Converts the value of a 32-bit signed integer to an IntPtr.
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryExplicit(Int64 to IntPtr)Converts the value of a 64-bit signed integer to an IntPtr.
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryExplicit(IntPtr to Void*)Converts the value of the specified IntPtr to a pointer to an unspecified type.
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryExplicit(IntPtr to Int64)Converts the value of the specified IntPtr to a 64-bit signed integer.
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryExplicit(IntPtr to Int32)Converts the value of the specified IntPtr to a 32-bit signed integer.
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryExplicit(Void* to IntPtr)Converts the specified pointer to an unspecified type to an IntPtr.
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryInequalityDetermines whether two specified instances of IntPtr are not equal.
Public operatorStatic memberSupported by Portable Class LibrarySubtractionSubtracts an offset from the value of a pointer.
Top

  NameDescription
Public fieldStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryZeroA read-only field that represents a pointer or handle that has been initialized to zero.
Top

  NameDescription
Explicit interface implemetationPrivate methodISerializable.GetObjectDataPopulates a SerializationInfo object with the data needed to serialize the current IntPtr object.
Top

The IntPtr type is designed to be an integer whose size is platform-specific. That is, an instance of this type is expected to be 32-bits on 32-bit hardware and operating systems, and 64-bits on 64-bit hardware and operating systems.

The IntPtr type can be used by languages that support pointers, and as a common means of referring to data between languages that do and do not support pointers.

IntPtr objects can also be used to hold handles. For example, instances of IntPtr are used extensively in the System.IO.FileStream class to hold file handles.

The IntPtr type is CLS-compliant, while the UIntPtr type is not. Only the IntPtr type is used in the common language runtime. The UIntPtr type is provided mostly to maintain architectural symmetry with the IntPtr type.

This type implements the ISerializable interface.

The following example uses managed pointers to reverse the characters in an array. After it initializes a String object and gets its length, it does the following:

  1. Calls the Marshal.StringToHGlobalAnsi method to copy the Unicode string to unmanaged memory as an ANSI (one-byte) character. The method returns an IntPtr object that points to the beginning of the unmanaged string. The Visual Basic example uses this pointer directly; in the C++ and C# examples, it is cast to a pointer to a byte.

  2. Calls the Marshal.AllocHGlobal method to allocate the same number of bytes as the unmanaged string occupies. The method returns an IntPtr object that points to the beginning of the unmanaged block of memory. The Visual Basic example uses this pointer directly; in the C++ and C# examples, it is cast to a pointer to a byte.

  3. The Visual Basic example defines a variable named offset that is equal to the length of the ANSI string. It is used to determine the offset into unmanaged memory to which the next charter in the ANSI string is copied. Because its starting value is the length of the string, the copy operation will copy a character from the start of the string to the end of the memory block.

    The C# and C++ examples call the IntPtr method to get an unmanaged pointer to the starting address of the string and the unmanaged block of memory, and they add one less than the length of the string to the starting address of the ANSI string. Because the unmanaged string pointer now points to the end of the string, the copy operation will copy a character from the end of the string to the start of the memory block.

  4. Uses a loop to copy each character from the string to the unmanaged block of memory.

    The Visual Basic example calls the Marshal.ReadByte(IntPtr, Int32) method to read the byte (or one-byte character) at a specified offset from the managed pointer to the ANSI string. The offset is incremented with each iteration of the loop. It then calls the Marshal.WriteByte(IntPtr, Int32, Byte) method to write the byte to the memory address defined by the starting address of the unmanaged block of memory plus offset. It then decrements offset.

    The C# and C++ examples perform the copy operation, then decrement the pointer to the address of the next location in the unmanaged ANSI string and increment the pointer to the next address in the unmanaged block.

  5. All examples call the Marshal.PtrToStringAnsi to convert the unmanaged memory block containing the copied ANSI string to a managed Unicode String object.

  6. After displaying the original and reversed strings, all examples call the FreeHGlobal method to free the memory allocated for the unmanaged ANSI string and the unmanaged block of memory.

using System;
using System.Runtime.InteropServices;

class NotTooSafeStringReverse
{
    static public void Main()
    {
        string stringA = "I seem to be turned around!";
        int copylen = stringA.Length;

        // Allocate HGlobal memory for source and destination strings
        IntPtr sptr = Marshal.StringToHGlobalAnsi(stringA);
        IntPtr dptr = Marshal.AllocHGlobal(copylen + 1);

        // The unsafe section where byte pointers are used. 
        unsafe
        {
            byte *src = (byte *)sptr.ToPointer();
            byte *dst = (byte *)dptr.ToPointer();

            if (copylen > 0)
            {
                // set the source pointer to the end of the string 
                // to do a reverse copy.
                src += copylen - 1;

                while (copylen-- > 0)
                {
                    *dst++ = *src--;
                }
                *dst = 0;
            }
        }
        string stringB = Marshal.PtrToStringAnsi(dptr);

        Console.WriteLine("Original:\n{0}\n", stringA);
        Console.WriteLine("Reversed:\n{0}", stringB);

        // Free HGlobal memory
        Marshal.FreeHGlobal(dptr);
        Marshal.FreeHGlobal(sptr);
    }
}

// The progam has the following output: 
// 
// Original: 
// I seem to be turned around! 
// 
// Reversed: 
// !dnuora denrut eb ot mees I

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library

This type is thread safe.

Show:
© 2015 Microsoft