Sign in to follow this  
Followers 0
ctl3d32

[Help] Getting returns from a function in a DLL.

8 posts in this topic

#1 ·  Posted (edited)

Hi all!

I need a little help on getting return values(more than one) from a DLL function, like in the example function bellow:

double __stdcall __declspec(dllexport) quadrado(double i, double j){
    double k;
    double l;
    l = i*j;
    k = sqrt(pow(i,2) + pow(j,2));
    return k,l;
}

How can i get variables k and l that are being returned from the DLL?

Using:

$r = DllCall($dll, "double", "quadrado", "double", $i, "double", $j)

Returns only one variable in $r[0].

Thanks in advance,

ctl3d32

Edited by ctl3d32

Share this post


Link to post
Share on other sites



If the function call fails then @error is set to 1. Otherwise an arrayis returned that contains the function return value and a copy of allthe parameters (including parameters that the function may havemodified when passed by reference).

$return[0] = function return value

$return[1] = param1

$return[2] = param2

...

$return[n] = paramn

http://www.autoitscript.com/autoit3/docs/functions/DllCall.htm

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Hi,

Got it working, but i don't know if it's the right solution. I have changed the DLL function, as seen bellow:

void __stdcall __declspec(dllexport) quadrado(double i, double j, double& k, double& l){
    l = i*j;
    k = sqrt(pow(i,2) + pow(j,2));
}

Where k and l are the returned values i want.

And the autoit code to execute the DLL function is:

$i = 6
$j = 4
$m = 0
$p = 0

$dll = DllOpen(@scriptdir & "\quadrado.dll")

$t = TimerInit()
$r = DllCall($dll, "double", "quadrado", "double", $i, "double", $j, "double*", $m, "double*", $p)
$k = TimerDiff($t) / 1000

If @error == 0 Then
    MsgBox(0,"No error :-)", "Returned values are:" & @CRLF & "-> " & $r[3] & @CRLF & "-> " & $r[4] & @CRLF & "It took " & StringFormat("%.6f ",$k) & "seconds")
ElseIf @error == 1 Then
    MsgBox(0,"error", "@error = 1 unable to use the DLL file")
ElseIf @error == 2 Then
    MsgBox(0,"error", "@error = 2 unknown 'return type'")
ElseIf @error == 3 Then
    MsgBox(0,"error", "@error = 3 'function' not found in the DLL file")    
EndIf

DllClose($dll)

The returned values are in $r[3] and $r[4].

Is this the correct or best solution?

Thanks,

ctl3d32

quadrado.dll

Edited by ctl3d32

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Why do you need to call it from a dll if you can modify our dll? Why not just make a function in AutoIt? Is there some reason you need a dll?

Edited by omikron48

Share this post


Link to post
Share on other sites

Why do you need to call it from a dll if you can modify our dll? Why not just make a function in AutoIt? Is there some reason you need a dll?

I just want to understand the DllCall function to maybe use it later in a more complex DLL function that may contain more complex mathematics like matrix inversion, and so on.

Just for learning purposes.

Thanks,

ctl3d32

Share this post


Link to post
Share on other sites

Functions can only return one value. I have no idea what you're trying to do there.

The only reason the return k,l compiled is that the comma is a valid operator.

To return multiple values, you just need to pass pointers.

Share this post


Link to post
Share on other sites

Functions can only return one value. I have no idea what you're trying to do there.

The only reason the return k,l compiled is that the comma is a valid operator.

To return multiple values, you just need to pass pointers.

I thought i could return more than one value without the need to pass the parameters as pointers.

Like: i send the values of "i" and "j", and it returns "k" and "l".

I were wrong. I have to send "i" and "j", and by reference "k" and "l". The function will modify "k" and "l" with the values that i want.

Then, "k" and "l" will be my returned values.

Thanks,

ctl3d32

Share this post


Link to post
Share on other sites

Functions can only return one value. I have no idea what you're trying to do there.

The only reason the return k,l compiled is that the comma is a valid operator.

To return multiple values, you just need to pass pointers.

You should really force yourself to use "Reply" button.

This way you are making things more confusing. You have very nice answers, no doubt about that, but just read the whole thread to see what I mean, even if it bores you.

Quote me not.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0