LoginSignup
3
1

More than 3 years have passed since last update.

Delphiでポインタをストリームとして扱う

Last updated at Posted at 2019-10-20

何をしようとしたの

普段ポインタを直接使うことはほぼないのですが、
最近 .NETアプリ等からバイナリデータの
ポインタを受け取るようなDLLを作ってて
ストリームにラッピングしたら楽じゃね?
ってことでやってみた。

コード

Unit1.pas
unit Unit1;

interface
uses
  System.Classes;

type
  TPointerStream = class(TCustomMemoryStream)
  public
    constructor Create(ptr: Pointer; const size: Int64);
  end;

implementation
  constructor TPointerStream.Create(ptr: Pointer; const size: Int64);
  begin
    inherited SetPointer(ptr, size);
  end;

end.

.NETのMemoryStreamクラスと違い、
最初からポインタ前提の基底クラスがあるので
実装もアホかというくらいシンプルです。
クラス名が適切かどうかも無視。

使い方

コンストラクタに確保済みのメモリアドレスとサイズを指定するだけ。
読み取り専用ですが、書き込みに対応させても良いですし
TBinaryReaderクラスでラッピングして使っても良いですね。
私はTBitmap.CreateFromStreamに渡したりしてます。

総評

もう既に誰かがやってそう。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1