IntPtr.ToPointer メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
重要
この API は CLS 準拠ではありません。
このインスタンスの値を、指定されていない型へのポインターに変換します。
public:
void* ToPointer();
[System.CLSCompliant(false)]
public void* ToPointer();
[<System.CLSCompliant(false)>]
member this.ToPointer : unit -> nativeptr<unit>
返品
Voidへのポインター。つまり、指定されていない型のデータを含むメモリへのポインター。
- 属性
例
次の例では、マネージド ポインターを使用して配列内の文字を反転します。 String オブジェクトを初期化し、その長さを取得すると、次の処理が実行されます。
Marshal.StringToHGlobalAnsi メソッドを呼び出して、Unicode 文字列を ANSI (1 バイト) 文字としてアンマネージ メモリにコピーします。 このメソッドは、アンマネージ文字列の先頭を指す IntPtr オブジェクトを返します。
Marshal.AllocHGlobal メソッドを呼び出して、アンマネージ文字列が占有するのと同じバイト数を割り当てます。 このメソッドは、アンマネージ メモリ ブロックの先頭を指す IntPtr オブジェクトを返します。
ToPointer メソッドを呼び出して、文字列の開始アドレスとメモリのアンマネージ ブロックへのアンマネージ ポインターを取得し、文字列の長さより 1 つ小さい値を ANSI 文字列の開始アドレスに追加します。 アンマネージ文字列ポインターが文字列の末尾を指すようになったため、コピー操作では文字列の末尾からメモリ ブロックの先頭に文字がコピーされます。
ループを使用して、文字列からアンマネージ メモリ ブロックに各文字をコピーします。 各コピー操作の後、アンマネージ ANSI 文字列内の次の場所のアドレスへのポインターをデクリメントし、アンマネージ ブロック内の次のアドレスへのポインターをインクリメントします。
コピーした ANSI 文字列を含むアンマネージ メモリ ブロックをマネージド Unicode Marshal.PtrToStringAnsi オブジェクトに変換するStringを呼び出します。
元の文字列と反転された文字列を表示した後、 Marshal.FreeHGlobal メソッドを呼び出して、アンマネージド ANSI 文字列に割り当てられたメモリとアンマネージ メモリ ブロックを解放します。
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
#nowarn "9"
open System.Runtime.InteropServices
open FSharp.NativeInterop
[<EntryPoint>]
let main _ =
let stringA = "I seem to be turned around!"
let mutable copylen = stringA.Length
// Allocate HGlobal memory for source and destination strings
let sptr = Marshal.StringToHGlobalAnsi stringA
let dptr = Marshal.AllocHGlobal(copylen + 1)
let mutable src: byte nativeptr = sptr.ToPointer() |> NativePtr.ofVoidPtr
let mutable dst: byte nativeptr = dptr.ToPointer() |> NativePtr.ofVoidPtr
if copylen > 0 then
// set the source pointer to the end of the string
// to do a reverse copy.
src <-
NativePtr.toNativeInt src + nativeint (copylen - 1)
|> NativePtr.ofNativeInt
while copylen > 0 do
copylen <- copylen - 1
NativePtr.read src |> NativePtr.write dst
dst <- NativePtr.toNativeInt dst + 1n |> NativePtr.ofNativeInt
src <- NativePtr.toNativeInt src - 1n |> NativePtr.ofNativeInt
NativePtr.write dst 0uy
let stringB = Marshal.PtrToStringAnsi dptr
printfn $"Original:\n{stringA}\n"
printfn $"Reversed:\n{stringB}"
// Free HGlobal memory
Marshal.FreeHGlobal dptr
Marshal.FreeHGlobal sptr
0
// The progam has the following output:
//
// Original:
// I seem to be turned around!
//
// Reversed:
// !dnuora denrut eb ot mees I