Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling DLL problem 1

Status
Not open for further replies.

Glutus

Programmer
Jul 13, 2001
27
0
0
SE
Hi all,

I've successfully managed to call a dll in VB.NET but can't for the life of me figure out how to do it in Delphi. The VB code is:

Function foo(ByVal inputArgument As VariantType) As VariantType
Dim myClass As test5.test5class
Dim answer = New VariantType
Dim noArguments = New VariantType
noArguments = 1
On Error GoTo Handle_Error
myClass = New test5.test5class
Call myClass.TestFunction(noArguments, answer, inputArgument)
foo = answer
Exit Function
Handle_Error:
foo = Err.Description
End Function


The dll is created in Matlab. The function is a very simple test:

Function answer = TestFunction(inputArgument)
TestFunction = inputArgument * inputArgument;


However, it seems that Matlab converts the function to:

function(number of arguments to output, the answer, input 1, input 2 ... input n)

I'm lost, please help me...
 
You need to figure out how to call the DLL.

In your Delphi program you need to have something like this for each of your functions:

function MessageBox(HWnd: Integer; Text, Caption: PChar; Flags: Integer): Integer;
stdcall; external 'user32.dll' name 'MessageBoxA';

This takes the "MessageBoxA" function out of "User32.DLL" and encapsulates it into the "MessageBox" function.

Of course, the stdcall can be one of: register, pascal, cdecl, stdcall, and safecall. This will be dependent on the calling conventions of the DLL you are trying to interface with.

You will need to do this with "TestFunction", if this is something that's coming from the DLL you're calling.
 
Thanks for your reply. I've tried all different variations of the static calls:

function TF(a:integer;b:variant;c:variant):variant;stdcall; extern 'test5.dll' name 'TestFunction';

Whatever I seem to try, the reply is that the procedure address cannot be found. I am not really sure of if it's a procedure or a funtion when the function is converted from:

function x(a)

to:

function x(answer,a)

When I try to load it dynamically:

procedure TForm1.Button1Click(Sender: TObject);
var
answer, a: variant;
Hbar: Thandle;
Foo: procedure(X: Integer; no: Variant; a: Variant); {$IFDEF WIN32} stdcall; {$ENDIF}
begin
Hbar := LoadLibrary('test5_1_0.dll');
if Hbar >= 32 then { success }
begin
a:=3;
Foo := GetProcAddress(HBar, 'TestFunction');
showmessage('got the address');
Foo(1,answer,a);
showmessage(answer);
FreeLibrary(HBar);
end
else
MessageDlg('Error: could not find the dll', mtError, [mbOk], 0);
end;


It displays 'got the address' and then crashes because of some access issue. I have tried both:

Foo: procedure(X: Integer; no: Variant; a: Variant);

Foo: function(X: Integer; no: Variant; a: Variant):Variant;


Do you have any ideas?
 
I finally worked it out! I had to use the reference and then add the class to the form, it is a class.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top