BORHAN TEC
سه شنبه 30 شهریور 1389, 17:59 عصر
سلام:قلب:
من یک فایل DLL ساخته ام که کد آن به شکل زیر است:
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
ShareMem,
Windows,
Forms,
Dialogs,
SysUtils,
Classes;
{$R *.res}
procedure SayHello(AForm: TForm);
begin
MessageBox(AForm.Handle, 'Hello World!', 'From DLL',
MB_OK or MB_ICONINFORMATION);
end;
procedure SayHelloFunc(var Dest: String);
begin
Dest := 'Hello World!';
end;
exports SayHello, SayHelloFunc;
begin
end.یک برنامه میزبان هم ساخته ام که کد مربوط به فرم اصلی آن به شکل زیر است:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IOUtils;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type
TSayHello = procedure(AForm: TForm);
TSayHelloFunc = procedure(var Dest: string);
procedure TForm1.Button1Click(Sender: TObject);
var
LibHandle: THandle;
SayHello: TSayHello;
begin
LibHandle := LoadLibrary('Project1.dll');
try
if LibHandle = 0 then
raise Exception.Create('Unable to load dll');
@SayHello := GetProcAddress(LibHandle, 'SayHello');
if not(@SayHello = nil) then
SayHello(Self)
else
RaiseLastOSError;
finally
FreeLibrary(LibHandle);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
LibHandle: THandle;
SayHelloFunc: TSayHelloFunc;
str1: string;
begin
LibHandle := LoadLibrary('Project1.dll');
try
if LibHandle = 0 then
raise Exception.Create('Unable to load dll');
@SayHelloFunc := GetProcAddress(LibHandle, 'SayHelloFunc');
if not(@SayHelloFunc = nil) then
begin
SayHelloFunc(str1);
Label1.Caption := str1;
end
else
RaiseLastOSError;
finally
FreeLibrary(LibHandle);
end;
end;
end.موقعی که من بر روی Button 2 کلیک می کنم مدام با یک استثنا مواجه می شوم. :اشتباه:
باید بگویم که من فایل Borlndmm.dll را هم در کنار DLL کپی کرده ام.
راستی نسخه فایل Borlndmm.dll من 3.0.3.70 است.
در ضمن فایل مربوط به هر دو پروژه هم در زیر قرار داده ام:
لطفاً دوستان راهنمایی کنند. :قلب:
من یک فایل DLL ساخته ام که کد آن به شکل زیر است:
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
ShareMem,
Windows,
Forms,
Dialogs,
SysUtils,
Classes;
{$R *.res}
procedure SayHello(AForm: TForm);
begin
MessageBox(AForm.Handle, 'Hello World!', 'From DLL',
MB_OK or MB_ICONINFORMATION);
end;
procedure SayHelloFunc(var Dest: String);
begin
Dest := 'Hello World!';
end;
exports SayHello, SayHelloFunc;
begin
end.یک برنامه میزبان هم ساخته ام که کد مربوط به فرم اصلی آن به شکل زیر است:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IOUtils;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type
TSayHello = procedure(AForm: TForm);
TSayHelloFunc = procedure(var Dest: string);
procedure TForm1.Button1Click(Sender: TObject);
var
LibHandle: THandle;
SayHello: TSayHello;
begin
LibHandle := LoadLibrary('Project1.dll');
try
if LibHandle = 0 then
raise Exception.Create('Unable to load dll');
@SayHello := GetProcAddress(LibHandle, 'SayHello');
if not(@SayHello = nil) then
SayHello(Self)
else
RaiseLastOSError;
finally
FreeLibrary(LibHandle);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
LibHandle: THandle;
SayHelloFunc: TSayHelloFunc;
str1: string;
begin
LibHandle := LoadLibrary('Project1.dll');
try
if LibHandle = 0 then
raise Exception.Create('Unable to load dll');
@SayHelloFunc := GetProcAddress(LibHandle, 'SayHelloFunc');
if not(@SayHelloFunc = nil) then
begin
SayHelloFunc(str1);
Label1.Caption := str1;
end
else
RaiseLastOSError;
finally
FreeLibrary(LibHandle);
end;
end;
end.موقعی که من بر روی Button 2 کلیک می کنم مدام با یک استثنا مواجه می شوم. :اشتباه:
باید بگویم که من فایل Borlndmm.dll را هم در کنار DLL کپی کرده ام.
راستی نسخه فایل Borlndmm.dll من 3.0.3.70 است.
در ضمن فایل مربوط به هر دو پروژه هم در زیر قرار داده ام:
لطفاً دوستان راهنمایی کنند. :قلب: