Use a function from an external DLL

Posted By: byakuren81

Use a function from an external DLL - 11/13/14 19:12

I am trying the simple example :

I created a dll containing the following code

#include "test_Z.h"

int basic_test(int a, int b)
{
return(a*b);
}


where the header contains :

#ifndef test_Z_H__
#define test_Z_H__

int basic_test(int a, int b);

#endif

I placed the dll into the Zorro folder and the header into the include folder of Zorro
I tested with the following code :

int __stdcall basic_test(int a, int b);
API(basic_test,test_Z)

function main()
{
int c = basic_test(5, 2);

printf("Result = %d", c);
}

It returns me the good result 10 but an error message then appears :

Error 111 : Crash in script

I am using Zorro 1.26 under windows 7 64x and the dll has been created using minGW, could you please tell me why do I get this error message ?
Posted By: byakuren81

Re: Use a function from an external DLL - 11/13/14 19:22

ok I realise I do not need to include the header as we use the line :
int __stdcall basic_test(int a, int b);

I have cancelled the line #include "test_Z.h" but I still get the same error message...
Posted By: byakuren81

Re: Use a function from an external DLL - 11/14/14 18:05

I have changed the variable type and it's getting worse, for example if my function is now :

double basic_test(double a, double b)
{
return(a*b);
}

and my zorro code :

double __stdcall basic_test(double a, double b);
API(foo,bar)

function main()
{
double c = basic_test(5.25, 5.25);

printf("Result = %f", c);
}

I get :

Result = 27.562500
Result = 27.562500

It appears twice !!! with the same error message than before :

Error111: Crash in script

Pleeeeeease could someone help me ?
Posted By: byakuren81

Re: Use a function from an external DLL - 11/14/14 18:51

OK I have read in details the manual (script conversion) and programmed my function the same way :

#include "stdlib.h"

__declspec(dllexport) void basic_test (double a, double b, double *c)
{
double *out;

out = c;

*out = a + b;
}

I created the dll and placed it in the zorro folder, my zorro script is :

int __stdcall basic_test(double a, double b, double *c);
API(basic_test,bar)

function main()
{
double c;

basic_test(5.25, 5.25, &c);

printf("\nResult = %f", c);
}

when compiling I got the correct answer :

Result = 10.500000

but once again the same error message !!!!

Error111: Crash in script

this is driving me mad I cannot believe no one never encountered the same issue !
Posted By: Ch40zzC0d3r

Re: Use a function from an external DLL - 11/14/14 21:46

Stop using stdcall in your typedef. The lite-c compiler may not know any other callingconventions then cdecl so stack is cleaned up 2 times (by caller and callee) and will crash you.
Try cdecl.
EDIT: Im also not sure if lite-c can handle double o.o Try float sicne double is 8byte
Posted By: byakuren81

Re: Use a function from an external DLL - 11/14/14 23:25

stdcall was indeed the issue, it works fine with cdecl instead ! many thanks to you !
Posted By: Ch40zzC0d3r

Re: Use a function from an external DLL - 11/15/14 11:01

No problem - your welcome laugh
© 2024 lite-C Forums