调用外部 DLL 中的函数(1. 早绑定)
2007-12-06 21:37
357 查看
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; //MB 函数的声明: function MB(hWnd: HWND; lpText, lpCaption: PChar; uType: UINT): Integer; stdcall; implementation {$R *.dfm} {调用外部 DLL 中的函数,譬如调用系统 user32.dll 中的 MessageBoxA} //function MB(hWnd: HWND; lpText, lpCaption: PChar; uType: UINT): Integer; // stdcall; external user32 name 'MessageBoxA'; {其中 user32 是 Delphi 定义的常量 'user32.dll',可以直接写成:} //function MB(hWnd: HWND; lpText, lpCaption: PChar; uType: UINT): Integer; // stdcall; external 'user32.dll' name 'MessageBoxA'; {name 后面说明函数的真实名字} {external 子句说明单元载入时就加载函数,也就是早绑定;如果晚绑定需要用 LoadLibrary} {stdcall 指令表示参数传递是从右到左(Pascal则反之),不通过CPU寄存器传递} {4个参数的数据类型可以使用对应的 Delphi 数据类型,譬如:} //function MB(hWnd: LongWord; lpText, lpCaption: PChar; uType: LongWord): Integer; // stdcall; external 'user32.dll' name 'MessageBoxA'; {或者是:} //function MB(hWnd: Cardinal; lpText, lpCaption: PChar; uType: Cardinal): Integer; // stdcall; external 'user32.dll' name 'MessageBoxA'; {如果函数在此单元声明后,需要给其他单元调用,需要先在 interface 区声明:} //function MB(hWnd: Cardinal; lpText, lpCaption: PChar; uType: Cardinal): Integer; // stdcall; {本例已经这样做了,如果要测试其他几种情况,需要先注释掉它} {然后在 implementation 区,说明函数的来源:} function MB; external 'user32.dll' name 'MessageBoxA'; //调用测试: procedure TForm1.Button1Click(Sender: TObject); var t,b: PChar; begin t := '标题'; b := '内容'; MB(0,b,t,0); end; end.
相关文章推荐
- 调用外部 DLL 中的函数(2. 晚绑定)
- 调用外部 DLL 中的函数(1. 早绑定)
- 调用外部 DLL 中的函数(2. 晚绑定)
- 调用外部 DLL 中的函数(隐式调用)
- 调用导出dll中的类成员函数 - error LNK2019: 无法解析的外部符号
- 调用外部 DLL 中的函数
- 调用外部 DLL 中的函数(显示调用)
- Delphi 调用DLL外部函数时的指针参数
- delphi中调用外部dll导出函数
- powerbuilder调用外部dll文件内函数
- Dll调用主程序(客户端、外部程序、宿主)函数的实现
- GameMonkey脚本语言编程基础——函数绑定和函数调用
- FLEX调用外部SWF的函数
- 调用dll动态库时,函数类型不匹配,可能的解决方案!
- 一个DELPHI程序要调用DLL,而DLL中的函数名,参数个数,参数类型为不定
- easyui 弹出框调用外部js函数 提示“Microsoft JScript 运行时错误: 缺少对象”
- JNI 简单例子小结 (调用外部DLL)
- Delphi中如何把函数代码调出主程序,写到“DLL”中并调用
- python中的函数调用绑定,静态方法和类方法
- C#加载dll(动态链接库)并调用其函数