Marshal.ReadInt16 メソッド (IntPtr, Int32)
アンマネージ メモリから 16 ビット符号付き整数を読み取ります。
Overloads Public Shared Function ReadInt16( _
ByVal ptr As IntPtr, _ ByVal ofs As Integer _) As Short
[C#]
public static short ReadInt16(IntPtrptr,intofs);
[C++]
public: static short ReadInt16(IntPtrptr,intofs);
[JScript]
public static function ReadInt16(
ptr : IntPtr,ofs : int) : Int16;
パラメータ
- ptr
読み込み元となるアンマネージ メモリ内のベース アドレス。 - ofs
読み取る前に ptr パラメータに追加された、追加のバイト オフセット。
戻り値
ptr から読み込まれる 16 ビット符号付き整数。
例外
例外の種類 | 条件 |
---|---|
ArgumentException | ptr が認識された形式ではありません。 |
解説
ReadInt16 を使用すると、16 ビット符号付きのアンマネージ配列を直接操作できます。このとき、要素の値を読み取る前にアンマネージ配列全体を (Marshal.Copy を使用して) 別のマネージ配列にコピーする必要はありません。
メモ このメソッドは SecurityAction.LinkDemand を使用して、信頼関係のないコードからの呼び出しを防ぎます。 SecurityPermissionAttribute.UnmanagedCode アクセス許可は、直前の呼び出し元にのみ要求されます。信頼性が一部しか確認されていないコードから呼び出すことができるコードの場合、ユーザー入力を検証せずに Marshal クラスに渡すことは避けてください。 LinkDemand メンバの使用に関する重要な制約事項については、「 Demand と LinkDemand 」を参照してください。
使用例
[Visual Basic, C#] C スタイル Int16 のアンマネージ配列と対話する 2 つの方法を比較する例を次に示します。 ReadInt16 メソッドは、配列の要素の値に直接アクセスできるようにします。
Dim unmanagedArray As IntPtr = ...
Dim i As Integer
' One way to print the 10 elements of the C-style unmanagedArray
Dim newArray As Int16(9)
Marshal.Copy(unmanagedArray, newArray, 0, 10)
For i = 0 To newArray.Length
Console.WriteLine(newArray(i))
Next i
' Another way to print the 10 elements of the C-style unmanagedArray
For i = 0 To 10
Console.WriteLine(Marshal.ReadInt16(unmanagedArray, i))
Next i
[C#]
IntPtr unmanagedArray = ...;
// One way to print the 10 elements of the C-style unmanagedArray
byte [] newArray = new Int16[10];
Marshal.Copy(unmanagedArray, newArray, 0, 10);
for (int i = 0; i < newArray.Length; i++)
Console.WriteLine(newArray[i]);
// Another way to print the 10 elements of the C-style unmanagedArray
for (int i = 0; i < 10; i++)
Console.WriteLine(Marshal.ReadInt16(unmanagedArray, i));
[C++, JScript] C++ および JScript のサンプルはありません。Visual Basic および C# のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET
参照
Marshal クラス | Marshal メンバ | System.Runtime.InteropServices 名前空間 | Marshal.ReadInt16 オーバーロードの一覧 | Copy | ReadByte