text2music.com > クジラ式Delphi資料 > DLLの作り方


DLLの作り方

 Delphi で、DLL作るのは簡単です。

手順(Delphi 7の場合)

  1. Delphiの起動 ・・・ 当然ですが
  2. メニューの[ファイル]-[新規作成]-[その他]をクリック|一覧から、DLLウィザードを選択
  3. とりあえず、作りたいDLLの名前でプロジェクト保存
  4. begin 〜 end. の上にエクスポートしたい関数を書く
  5. 以下のように、exports の下に関数の名前を並べる
    exports
     関数の名前1, 関数の名前2, ...;
  6. コンパイルしたら完了

DLLを作る例

library Project1;

uses
  Windows;

// 簡単な足し算の関数
function tasu(a, b: Integer): Integer; stdcall;
begin
  Result := a + b;
end;

// 簡単なメッセージボックスを表示する関数
function MsgYesNo(Text: PChar; Title: PChar): BOOL; stdcall;
begin
  Result := (MessageBox(0, Text, Title, MB_YESNO or MB_ICONQUESTION) = IDYES);
end;

exports
  tasu, MsgYesNo;

begin
end.

ポイントは、関数を定義したときの、stdcall 。これをつけると、他の言語からの呼び出しが楽になります。
他にも、string 型など、Delphi 特有の型を使うとよくないので、PCharやIntegerなどの基本的な型を使います。


DLLを使う例

宣言:
function tasu(a, b: Integer): Integer; stdcall; external 'Project1.dll';
function MsgYesNo(Text: PChar; Title: PChar): BOOL; stdcall; external 'Project.1.dll';

利用例:
procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(IntToStr(
    tasu(10, 5)
  ));
end;
宣言さえしっかりやっていれば、あとは、普通の関数と同じです。


戻る