次の方法で共有


Marshal.WriteByte メソッド (IntPtr, Int32, Byte)

1 バイト値をアンマネージ メモリに書き込みます。

Overloads Public Shared Sub WriteByte( _
   ByVal ptr As IntPtr, _   ByVal ofs As Integer, _   ByVal val As Byte _)
[C#]
public static void WriteByte(IntPtrptr,intofs,byteval);
[C++]
public: static void WriteByte(IntPtrptr,intofs,unsigned charval);
[JScript]
public static function WriteByte(
   ptr : IntPtr,ofs : int,val : Byte);

パラメータ

  • ptr
    書き込み元となるアンマネージ メモリ内のベース アドレス。
  • ofs
    書き込む前に ptr パラメータに追加された、追加のバイト オフセット。
  • val
    書き込む値。

例外

例外の種類 条件
ArgumentException ptr が認識された形式ではありません。

解説

WriteByte を使用すると、C スタイルのアンマネージ バイト配列を直接操作できます。このとき、要素の値を設定する前にアンマネージ配列全体を (Marshal.Copy を使用して) 別のマネージ配列にコピーする必要はありません。

メモ   このメソッドは SecurityAction.LinkDemand を使用して、信頼関係のないコードからの呼び出しを防ぎます。 SecurityPermissionAttribute.UnmanagedCode アクセス許可は、直前の呼び出し元にのみ要求されます。信頼性が一部しか確認されていないコードから呼び出すことができるコードの場合、ユーザー入力を検証せずに Marshal クラスに渡すことは避けてください。 LinkDemand メンバの使用に関する重要な制約事項については、「 Demand と LinkDemand 」を参照してください。

使用例

[Visual Basic, C#] C スタイルのアンマネージ バイト配列と対話する 2 つの方法を比較する例を次に示します。 WriteByte メソッドを使用すると、配列の要素の値に直接アクセスでき、その 10 の要素に値 1 ~ 10 を設定します。

 
Dim unmanagedArray As IntPtr = ...
Dim i As Integer
' One way to set the 10 elements of the C-style unmanagedArray
Dim newArray As Byte(9)
Marshal.Copy(unmanagedArray, newArray, 0, 10)
For i = 0 To newArray.Length
  newArray(i) = i+1
Next i
Marshal.Copy(newArray, 0, unmanagedArray, 10)
' Another way to set the 10 elements of the C-style unmanagedArray
For i = 0 To 10
  Marshal.WriteByte(unmanagedArray, i, i+1)
Next i
   
[C#] 
IntPtr unmanagedArray = ...;
// One way to set the 10 elements of the C-style unmanagedArray
byte [] newArray = new byte[10];
Marshal.Copy(unmanagedArray, newArray, 0, 10);
for (int i = 0; i < newArray.Length; i++)
  newArray[i] = i+1;
Marshal.Copy(newArray, 0, unmanagedArray, 10);
// Another way to set the 10 elements of the C-style unmanagedArray
for (int i = 0; i < 10; i++)
  Marshal.WriteByte(unmanagedArray, i, i+1);
   

[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.WriteByte オーバーロードの一覧 | Copy