text2music.com > クジラ式Delphi資料 > DLLの作り方
Delphi で、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などの基本的な型を使います。
宣言:
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;