【例子介绍】Delphi DLL 字符串传递例子
【相关图片】
【源码结构】
unit UnitMain; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TMyObj = class id: Integer; Name: string; address: Pchar; end; TForm1 = class(TForm) btn1: TButton; Memo1: TMemo; btn2: TButton; Button1: TButton; procedure btn1Click(Sender: TObject); procedure btn2Click(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} {$Warnings OFF} procedure Teststr(p: PChar); external 'test.dll'; procedure TestObj(p: TMyObj); external 'test.dll'; procedure TForm1.Btn1Click(Sender: TObject); var p: PChar; str: string; i: Integer; begin for i := 1 to 8000 do begin randomize(); str := '我来自Exe 程序' IntToStr(Random(100)) #$D#$A; p := StrAlloc(8192); StrCopy(p, PChar(str)); Teststr(p); // 调用 Dll 函数 memo1.Text := p ', 我们两个做游戏.'; StrDispose(p); Application.ProcessMessages; end; end; procedure TForm1.btn2Click(Sender: TObject); var p: TMyObj; str: string; begin p := TMyObj.Create; p.id := 0; p.Name := '我来自Exe 程序' IntToStr(Random(100)) #$D#$A; TestObj(p); // 调用 Dll 函数 memo1.Text := IntTostr(p.id) ', ' p.Name; FreeAndNil(p); end; procedure TForm1.Button1Click(Sender: TObject); var p: TMyObj; str: string; i: integer; begin for i := 1 to 8000 do begin p := TMyObj.Create; p.address := StrAlloc(8192); strcopy(p.address, 'yyuyuyuyu'); p.id := 0; p.Name := '我来自Exe 程序' IntToStr(Random(100)) #$D#$A; TestObj(p); // 调用 Dll 函数 memo1.Text := IntTostr(p.id) ', ' p.address; FreeAndNil(p); Application.ProcessMessages; end; end; {$Warnings ON} end.
评论