关于Delphi中DLL的使用(灌水篇之8)

ForestBen
下面,我向大家介绍一下Delphi中DLL的创建和使用方法。
首先,创建一个DLL。
1。File->new->DLL
2。在单元中键入如下代码:
library MsgDlgdll;
uses
Windows,SysUtils,Classes; //add windows的引用以能访问api函数
procedure MsgDlg(Caption,Context:PChar); stdcall; //使用pchar而不是string类型,这样可以不包括vcl库
begin
MessageBox(0,Context,Caption,MB_OKCANCEL);
end;
exports //导出函数
MsgDlg;
begin
end.
3。编译它
4。建一个Application。在它implementation下加上如下代码,它为应用程序从DLL中导入函数:
procedure MsgDlg(Caption,Context:PChar); stdcall;external '..\MsgDlgDLL\MsgDlgdll.dll'; //注意此处DLL文件所在的目录,后面具体
说明。
在窗体上加一个按钮button1,它的onclick事件里加入如下代码
procedure TForm1.Button1Click(Sender: TObject);
begin
MsgDlg('Hello,DLLWorld','I''m ForestBen from nwpu');
end;
现在你能看到那个对话框了吗?
//程序的目录结构是 ..\testdll\MsgDlgDLL\MsgDlgdll.dll
// ..\testdll\usedll\project1.ext
下面讲一个稍微复杂的例子。。。


<marquee behavior=alternate>

终于漫长岁月 现已仿佛像流水</marquee>

<marquee behavior=alternate>
一花一世界,一叶一如来</marquee>