IntPtr Structure
TOC
Collapse the table of content
Expand the table of content

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 Structure IntPtr
	Implements ISerializable

NameDescription
System_CAPS_pubmethodIntPtr(Int32)

Initializes a new instance of IntPtr using the specified 32-bit pointer or handle.

System_CAPS_pubmethodIntPtr(Int64)

Initializes a new instance of IntPtr using the specified 64-bit pointer.

System_CAPS_pubmethodIntPtr(Void*)

Initializes a new instance of IntPtr using the specified pointer to an unspecified type.

NameDescription
System_CAPS_pubpropertySystem_CAPS_staticSize

Gets the size of this instance.

NameDescription
System_CAPS_pubmethodSystem_CAPS_staticAdd(IntPtr, Int32)

Adds an offset to the value of a pointer.

System_CAPS_pubmethodEquals(Object)

Returns a value indicating whether this instance is equal to a specified object.(Overrides ValueType.Equals(Object).)

System_CAPS_pubmethodGetHashCode()

Returns the hash code for this instance.(Overrides ValueType.GetHashCode().)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodSystem_CAPS_staticSubtract(IntPtr, Int32)

Subtracts an offset from the value of a pointer.

System_CAPS_pubmethodToInt32()

Converts the value of this instance to a 32-bit signed integer.

System_CAPS_pubmethodToInt64()

Converts the value of this instance to a 64-bit signed integer.

System_CAPS_pubmethodToPointer()

Converts the value of this instance to a pointer to an unspecified type.

System_CAPS_pubmethodToString()

Converts the numeric value of the current IntPtr object to its equivalent string representation.(Overrides ValueType.ToString().)

System_CAPS_pubmethodToString(String)

Converts the numeric value of the current IntPtr object to its equivalent string representation.

NameDescription
System_CAPS_pubfieldSystem_CAPS_staticZero

A read-only field that represents a pointer or handle that has been initialized to zero.

NameDescription
System_CAPS_puboperatorSystem_CAPS_staticAddition(IntPtr, Int32)

Adds an offset to the value of a pointer.

System_CAPS_puboperatorSystem_CAPS_staticEquality(IntPtr, IntPtr)

Determines whether two specified instances of IntPtr are equal.

System_CAPS_puboperatorSystem_CAPS_staticNarrowing(Int32 to IntPtr)

Converts the value of a 32-bit signed integer to an IntPtr.

System_CAPS_puboperatorSystem_CAPS_staticNarrowing(Int64 to IntPtr)

Converts the value of a 64-bit signed integer to an IntPtr.

System_CAPS_puboperatorSystem_CAPS_staticNarrowing(IntPtr to Int32)

Converts the value of the specified IntPtr to a 32-bit signed integer.

System_CAPS_puboperatorSystem_CAPS_staticNarrowing(IntPtr to Int64)

Converts the value of the specified IntPtr to a 64-bit signed integer.

System_CAPS_puboperatorSystem_CAPS_staticNarrowing(IntPtr to Void*)

Converts the value of the specified IntPtr to a pointer to an unspecified type.

System_CAPS_puboperatorSystem_CAPS_staticNarrowing(Void* to IntPtr)

Converts the specified pointer to an unspecified type to an IntPtr.

System_CAPS_puboperatorSystem_CAPS_staticInequality(IntPtr, IntPtr)

Determines whether two specified instances of IntPtr are not equal.

System_CAPS_puboperatorSystem_CAPS_staticSubtraction(IntPtr, Int32)

Subtracts an offset from the value of a pointer.

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_privmethodISerializable.GetObjectData(SerializationInfo, StreamingContext)

Populates a SerializationInfo object with the data needed to serialize the current IntPtr object.

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 ToPointer 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.

Imports System
Imports System.Runtime.InteropServices

Public Module Example
    Public Sub Main()
        Dim stringA As String = "I seem to be turned around!"
        Dim copylen As Integer = stringA.Length

        ' Allocate HGlobal memory for source and destination strings
        Dim sptr As IntPtr = Marshal.StringToHGlobalAnsi(stringA)
        Dim dptr As IntPtr = Marshal.AllocHGlobal(copylen)
        Dim offset As Integer = copylen - 1

         For ctr As Integer = 0 To copylen - 1
            Dim b As Byte = Marshal.ReadByte(sptr, ctr)
            Marshal.WriteByte(dptr, offset, b)
            offset -= 1
         Next

        Dim stringB As String = Marshal.PtrToStringAnsi(dptr)

        Console.WriteLine("Original:{1}{0}{1}", stringA, vbCrLf)
        Console.WriteLine("Reversed:{1}{0}{1}", stringB, vbCrLf)

        ' Free HGlobal memory
        Marshal.FreeHGlobal(dptr)
        Marshal.FreeHGlobal(sptr)
    End Sub
End Module
' The example displays the following output:
'       Original:
'       I seem to be turned around!
'
'       Reversed:
'       !dnuora denrut eb ot mees I

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1

This type is thread safe.

Return to top
Show:
© 2016 Microsoft