delphi4arab منتديات دلفي للعرب

نسخة كاملة : كيف كتابة دالة بسيطة في مكتبة dll واستدعائها من برنامجي
أنت حالياً تتصفح نسخة خفيفة من المنتدى . مشاهدة نسخة كاملة مع جميع الأشكال الجمالية .
الصفحات : 1 2
بسم الله الرحمن الرحيم
عندى مشكلة عند كومبيل لكتابة دالة الكود بالكامل
كود :
library test; { 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 bsp-msg(caption :string);stdcall;export; begin ShowMessage(caption); End; exports bsp-msg; begin end.
الخطأ هنا
procedure bsp-msg(caption Confusedtring);stdcall;export;
تفاصيل[COLOR="red"]
[Error] test.dpr(19): ';' expected but '-' found
[Error] test.dpr(21): Undeclared identifier: 'caption'
[Error] test.dpr(24): ',' or ';' expected but '-' found
[/COLOR]
والبروجيكت فى المرفقات
السلام عليكم
غير bsp-msg الى bsp_msg أو bspmsg
لا يسمح استخدام - في التسمية...
بارك الله فيك أخى B.M.AbdelAziZ
ظهرتلى مشكلة اخرى مع الربط المكتبة اخى الكريم
كود :
http://www6.0zz0.com/2014/04/30/15/255725367.jpg
والكود بالكامل
كود :
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; implementation {$R *.dfm} procedure bsp_msg(caption :string);stdcall;external 'test.dll' procedure TForm1.Button1Click(Sender: TObject); begin bsp_msg('welcom'); end; end.
السبب أنك تحاول أن تقوم بعمل تنفيذ / تشغيل للمكتبة Dll و ليس التطبيق exe .
قم بالتحقق من المشروع الحالي .
+ هل قرأت ال comment أعلى المكتبة :

كود :
{ Important note about DLL memory management: [COLOR="Red"]ShareMem must be the first unit in your library's USES[/COLOR] clause AND your project's (select Project-View Source) USES clause[COLOR="red"] if your DLL exports any procedures or functions that pass strings as parameters or function results[/COLOR]. 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. [COLOR=Blue]To avoid using BORLNDMM.DLL, pass string information using PChar or ShortString parameters. [/COLOR]}


نصيحة : تجنب استخدام صنف String في ال dll ..
كبديل استخدم PChar .
أخى الكريم SMP3 شكرا لمرورك :.
أنا احاول ابتدى اكتب دالة من صنعى واستعنت بأحد الكتب وطبقت كما موجود بالشرح ممكن فضلا وليس امراّ تعمل مقارنة ما بين الشرح والمشروع الخاص بى لان الشرح وصل الى ربط dll بالمشروع عموما البروجيت المرفقات
هذا ملف المكتبة :
PHP كود :
library test;

uses
  SysUtils
,
  
Classes,Dialogs;

{
$R *.res}

procedure bsp_msg(caption: PChar); stdcall; export;
begin
  ShowMessage
(Caption);
end;

exports bsp_msg;

begin

end
. 
كما قلت لك سابقا لا تستخدم الصنف String مع dll ..
----------------------------
هذا التطبيق الذي يستدعي المكتبة :
PHP كود :
procedure bsp_msg(Caption: PChar); stdcall;external 'test.dll' name 'bsp_msg';

procedure TForm1.FormCreate(Sender: TObject);
begin
  bsp_msg
('Hi..');
end; 
-----------------------------------------
عندما تعمل على ربط مكتبة dll مع تطبيق خارجي عليك مراعاة عدة أشياء , أهمها :
-أن تتجنب استخدام الصنف String,AnsiString,WideString.
-أن تحترم ال calling convention . في مثالك : stdcall : يجب أن تكون نفسها الموجودة في تعريف الدالة .
-يجب أن يكون اسم الدالة Valid أي لا يحتوي على نقاط و فواصل و - ؟ / ...
-تحقق من أن المكتبة موجودة في نفس مجلد التطبيق exe .
[color="red"]
ممكن ترسلى مشروع بسيط
[/color]
المثال مكتوب فوق..
بارك الله فيك أخى B.M.AbdelAziZ
الصفحات : 1 2