إن لغة دلفى تعطيك بيئة قوية لتكوين الملفات المكتبية DLL , ولذلك سنحاول تكوينها خطوة خطوة ,
مرحلة التكوين
لنقم بتكوين ملف مكتبي بسيط يقوم بطباعة الرسالة ArabTeam2000
من قائمة دلفي اذهب إلى File|New تم اختار DLL , ستلاحظ تكون برنامج جديد تكون محتوياته كالتالي
[l]
library Project1;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
SysUtils,
Classes;
{$R *.RES}
begin
end.
[/l]
الآن أضف التالي :
- إلى جملة uses أضف Dialogs
-الإجراء التالي
[l]
procedure HelloWorld; stdcall;;
begin
ShowMessage('ArabTeam2000');
end;
[/l]
الكلمة Stdcall تخبر المترجم بأن يعاملها على أنها calling convention
calling convention هي الطرق القياسية التي تجعل الإجراءات في الوندوز تعاملها كما هي ,
- الكلمة الموالية هي Export تخبر الدلفي أننا سوف نستخدم هذه الدالة من برنامج أحر .
[l] exports ArabTeam2000; [/l]
البرنامج كاملا
[l]
library Dllmsg;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
SysUtils,
Classes,
Dialogs;
{$R *.RES}
procedure HelloWorld;stdcall;
begin
ShowMessage('ArabTeam2000');
end;
exports
HelloWorld;
begin
end.
[/l]
خزن البرنامج File|Save Project as و سمه مثلا Dllmsg
ثم تأتي مرحلة تكوين المكتبة و ذلك باستخدام Project|Compile أو Ctrl+F9
تم قم بإغلاق هدا البرنامج ,
الآن لو ذهبت إلى المجلد الذي خزنت به البرنامج ستجد الملف Dllmsg.dll
هده المكتبة تملك الإجراء HelloWorld يمكننا أن نستدعيه من أي برنامج في الوندوز ,
مرحلة الاستدعاء
توجد طريقتين للاستدعاء الأولى implicit linking الأخرى explicit linking
Implicit Linking
تعتبر هده الطريقة من اسهل الطرق للتعامل مع المكتبات , ولكن تعاب بأن برنامج لن يشتغل أبدا لم يستطع أن يجد هده المكتبة .
كون برنامج جديد ,
لكي تستدعي أي إجراء من أي مكتبة تسمى هده العملية استيراد import
أذهب إلى قطاع
وضع السطر التالي
[l] procedure HelloWorld; external 'Dllmsg.dll'; [/l]
هذا الامر يخبر الدلفي انك تريد استيراد اجراء خارجي external اسمه HelloWorld موجود في الملف Dllmsg.dll
ضع زر على الفورم وفي الحدث OnClick ضع
[l] procedure TForm1.Button1Click(Sender: TObject); begin HelloWorld; end; [/l]
قم بتخزين برنامجك , تم قم بنسخ المكتبة التي كونتها سابقا في نفس المجلد الذي خزنت به برنامجك .
Explicit Linking
هده الطريقة معقدة قليل و تحتاج إلى عناية اكثر , لكنها تتميز بان البرنامج لن يتوقف أبدا ادا لم يستطع أن يجد المكتبة ,
ضع زر أخر على الفورم واكتب التالي
[l]
procedure TForm1.Button2Click(Sender: TObject);
var
hMod : THandle;
HelloWorld : procedure;
begin
hMod := LoadLibrary('Dllmsg.dll');
if (hMod = 0) then Exit;
@HelloWorld := GetProcAddress(hMod, 'HelloWorld');
if @HelloWorld <> nil then
HelloWorld;
FreeLibrary(hMod);
end;
[/l]
هذا الشرط
if (hMod = 0) then Exit;
يعكس حالة عدم وجدود المكتبة ,
تستطيع استبدال الأمر Exit بأي أمر اخر تريده ,
عندما تنفد برنامجك فان ناتج الضغط على الزرين ستكون نفس النتيجة ,
وشكرا
مــــMADIــــدي