how to call a Dll and pass parameters to it in delphi 7
I am a delphi noob so please help me out with this. I have created a DLL with the following code:
library PRdll;
uses
ExceptionLog, SysUtils,Classes,Dialogs;
{$R *.res}
function DllMessage(var a:integer):Integer;stdcall;export;
begin
Showmessage('GHelloa');//this is displayed
ShowMessage(IntToStr(a));//I get the error at this point
Result:=5;
end;
exports DllMessage;
begin
end.
The corresponding call to the DLL is given by this code:
var
FDll: TFDll;
function DllMessage(var a:integer):integer;stdcall;external 'PRDll.dll';
implementation
{$R *.dfm}
procedure TFDll.btnCallDllClick(Sender: TObject);
var
i:integer;
s1:string;
begin
i:=5;
s1:=IntToStr(DllMessage(i));
//ShowMessage(s1);
end;
I get an access error. Why does this happen . ANybody ? he开发者_如何学Golp!!! Thanks in Advance
Have you added sharemem unit?
See the help.
Second option: Comment the ExceptionLog line and try again.
It work fine.
Regards.
The correct code is posted below; i am surprised why no one could tell me this simple thing
//Dll
library ProDll;
uses
ExceptionLog,
SysUtils,
Classes,Dialogs;
function showSqRoot(var a:Double):Double; stdcall; export;
begin
Result:=sqrt(a);
end;
exports showSqRoot;
{$R *.res}
begin
end.
//The program
function showSqRoot(var a:Double):Double;stdcall external 'ProDll.dll'//Notice 'stdcall external' here
procedure TFcallDLL.btnCallDLLClick(Sender: TObject);
var
numInput,numRes:Double; {Input and Result}
begin
numInput:=StrToFloat(edtInput.Text);
numRes:=showSqRoot(numInput); {call the function in DLL with the Parameter}
edtResult.Text:=FloatToStr(numRes); {Display the Result}
end;
This code has worked fine for me.
精彩评论