IntPtr
IntPtr
IntPtr
IntPtr
Struct
Definition
A platform-specific type that is used to represent a pointer or a handle.
public value class IntPtr : System::Runtime::Serialization::ISerializable
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public struct IntPtr : System.Runtime.Serialization.ISerializable
type nativeint = struct
interface ISerializable
Public Structure IntPtr
Implements ISerializable
- Inheritance
- Attributes
- Implements
Examples
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:
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.
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.
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.
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 decrementsoffset
.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.
All examples call the Marshal.PtrToStringAnsi to convert the unmanaged memory block containing the copied ANSI string to a managed Unicode String object.
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 namespace System;
using namespace System::Runtime::InteropServices;
class NotTooSafeStringReverse
{
public:
static 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);
char *src = (char *)sptr.ToPointer();
char *dst = (char *)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);
}
};
int main()
{
NotTooSafeStringReverse::Main();
}
// The progam has the following output:
//
// Original:
// I seem to be turned around!
//
// Reversed:
// !dnuora denrut eb ot mees I
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
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
Remarks
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.
Constructors
IntPtr(Int32) IntPtr(Int32) IntPtr(Int32) IntPtr(Int32) |
Initializes a new instance of IntPtr using the specified 32-bit pointer or handle. |
IntPtr(Int64) IntPtr(Int64) IntPtr(Int64) IntPtr(Int64) |
Initializes a new instance of IntPtr using the specified 64-bit pointer. |
IntPtr(Void*) IntPtr(Void*) IntPtr(Void*) IntPtr(Void*) |
Initializes a new instance of IntPtr using the specified pointer to an unspecified type. |
Fields
Zero Zero Zero Zero |
A read-only field that represents a pointer or handle that has been initialized to zero. |
Properties
Size Size Size Size |
Gets the size of this instance. |
Methods
Add(IntPtr, Int32) Add(IntPtr, Int32) Add(IntPtr, Int32) Add(IntPtr, Int32) |
Adds an offset to the value of a pointer. |
Equals(Object) Equals(Object) Equals(Object) Equals(Object) |
Returns a value indicating whether this instance is equal to a specified object. |
GetHashCode() GetHashCode() GetHashCode() GetHashCode() |
Returns the hash code for this instance. |
Subtract(IntPtr, Int32) Subtract(IntPtr, Int32) Subtract(IntPtr, Int32) Subtract(IntPtr, Int32) |
Subtracts an offset from the value of a pointer. |
ToInt32() ToInt32() ToInt32() ToInt32() |
Converts the value of this instance to a 32-bit signed integer. |
ToInt64() ToInt64() ToInt64() ToInt64() |
Converts the value of this instance to a 64-bit signed integer. |
ToPointer() ToPointer() ToPointer() ToPointer() |
Converts the value of this instance to a pointer to an unspecified type. |
ToString(String) ToString(String) ToString(String) ToString(String) |
Converts the numeric value of the current IntPtr object to its equivalent string representation. |
ToString() ToString() ToString() ToString() |
Converts the numeric value of the current IntPtr object to its equivalent string representation. |
Operators
Explicit Interface Implementations
Applies to
Thread Safety
This type is thread safe.
See also
Feedback
We'd love to hear your thoughts. Choose the type you'd like to provide:
Our feedback system is built on GitHub Issues. Read more on our blog.
Loading feedback...