Delphi中Create and call DLL
来源:互联网 发布:乐视2移动数据上不了网 编辑:程序博客网 时间:2024/04/30 15:04
1. create DLL (方法1):
library MyDLL;
uses
SysUtils,
Classes,
DLL in 'DLL.pas';
...{$R *.res}
exports
AddIt, SubIt;
begin
end.
unit DLL; // 新增一個單元文件
interface
function AddIt(x1, x2: Integer): Integer; stdcall;
function SubIt(x1, x2: Integer): Integer; stdcall;
implementation
function AddIt(x1, x2: Integer): Integer; stdcall;
begin
result := x1 + x2; // or AddIt := x1 + x2;
end;
function SubIt(x1, x2: Integer): Integer; stdcall;
begin
result := x1 - x2; // or SubIt := x1 - x2;
end;
end.
create DLL (方法2):
library MyDLL;
uses
SysUtils,
Classes,
function AddIt(x1, x2: Integer): Integer; export;
begin
result := x1 + x2; // or AddIt := x1 + x2;
end;
function SubIt(x1, x2: Integer): Integer; export;
begin
result := x1 - x2; // or SubIt := x1 - x2;
end
...{$R *.res}
exporTs
AddIt, SubIt;
begin
end.
2. Static Call DLL
unit MainForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
...{ Private declarations }
public
...{ Public declarations }
end;
var
Form1: TForm1;
implementation
function AddIt(x1, x2: Integer): Integer; stdcall; external 'MyDLL.dll';
function SubIt(x1, x2: Integer): Integer; stdcall; external 'MyDLL.dll';
...{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if (Edit1.Text<>'') and (Edit2.Text<>'') then
begin
try
Label1.Caption := IntToStr( AddIt(StrToInt(Edit1.Text), StrToInt(Edit2.Text)) );
Label2.Caption := IntToStr( SubIt(StrToInt(Edit1.Text), StrToInt(Edit2.Text)) );
except
ShowMessage('Input an integer number!');
Edit1.SetFocus;
end;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Edit1.Text := IntToStr(Random(Width));
Edit2.Text := IntToStr(Random(Width));
end;
end.
3. Automatic Call DLL
unit AutoDLL;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TMyDLL = function(x, y: Integer): Integer; stdcall;
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Button2: TButton;
Label2: TLabel;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
...{ Private declarations }
public
...{ Public declarations }
end;
var
Form1: TForm1;
Gv_DLLHandle: THandle;
Gv_DLLPointer: Pointer;
Gv_MyDLLFunc: TMyDLL;
implementation
...{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if (Edit1.Text='') or (Edit2.Text='') then
Exit;
Gv_DLLHandle := LoadLibrary('MyDLL.dll');
if Gv_DLLHandle>0 then //判斷句柄是否有值﹐有值為載入成功
try
Gv_DLLPointer := GetProcAddress(Gv_DLLHandle, PChar('AddIt')); //根據函數名稱獲得函數指針
if Gv_DLLPointer<>nil then //判斷Gv_DLLPointer是否為空指針﹐不為空則為得到了函數地址
begin
Gv_MyDLLFunc := TMyDLL(Gv_DLLPointer); //將指針強制轉換為函數指針
try
Label1.Caption := IntToStr(Gv_MyDLLFunc( StrToInt(Edit1.Text), StrToInt(Edit2.Text) ));
except
ShowMessage('Input an integer number!');
end;
end
else
ShowMessage('No found "AddIt"');
Gv_DLLPointer := GetProcAddress(Gv_DLLHandle, PChar('SubIt'));
if Gv_DLLPointer<>nil then
begin
Gv_MyDLLFunc := TMyDLL(Gv_DLLPointer);
try
Label2.Caption := IntToStr(Gv_MyDLLFunc(StrToInt(Edit1.Text), StrToInt(Edit2.Text)));
except
ShowMessage('Input an integer number!');
end;
end
else
ShowMessage('No found "SubIt"');
finally
FreeLibrary(Gv_DLLHandle); //釋放DLL
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Edit1.Text := IntToStr(Random(Height));
Edit2.Text := IntToStr(Random(Height));
end;
end.
- Delphi中Create and call DLL
- Create and Call HttpHandler in SharePoint
- delphi中Create参数区别
- delphi中Create参数区别
- delphi 中 DLL 操作
- Delphi中Create(nil),Create(self),Create(Application)区别
- Delphi中Create(nil),Create(self),Create(Application)区别
- Delphi中Create(nil),Create(self),Create(Application)区别
- Delphi中Create(nil),Create(self),Create(Application) 区别
- How to Create and Use the DLL
- Create and Pulish web service, call in Asp.net(Demo).
- 在Delphi中调用DLL
- DELPHI中动态调用dll
- 在Delphi中使用DLL
- Delphi 中如何调试 DLL
- Delphi中从零开始写DLL
- Delphi中 .dll 及应用
- DELPHI中创建调用DLL
- 诸葛亮职称申请回函
- 一些消息,由GBA暂时更换为SFC
- 《高质量程序设计指南——C++/C》(第三版)最新修订
- 基于DCT变换的信息隐藏(数字水印)嵌入算法的设计(Matlab)
- VC6编译XVID
- Delphi中Create and call DLL
- DCT域自适应混沌加密的二值图像数字水印算法
- 引由Eclipse调试器中的Watch(观察)[00原创]
- TDBGrid+TCheckBox
- asp:UpdatePanel 中javascript的使用
- 基于构件开发的应用框架设计
- 清除SQLServer日志的两种方法
- DataView的使用
- 编程修养