QB64 Phoenix Edition
how to pass parameters to H file? - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10)
+---- Thread: how to pass parameters to H file? (/showthread.php?tid=1611)



how to pass parameters to H file? - Petr - 04-11-2023

I am confused. What is the compiler trying to do?

I try to pass a parameter of type INT or Float and it keeps telling me that the conversion from type INT* to INT and from Float to Float* cannot be performed - what does this mean? Here is a sample code to QB64PE and below that the contents of the H file. It should be fine (but it would have to work) - what did I do wrong?

QB64 code:
Code: (Select All)
Declare Library "plus"
    Function calc_plus% (a As Integer, b As Integer)
End Declare

c% = calc_plus(5, 6)
Print c%

plus.h file code:
Code: (Select All)
int calc_plus (int a, int b)
{
    int c = a + b;
    return c;
}


I'm not big friends with the C language...


RE: how to pass parameters to H file? - Jack - 04-11-2023

Petr, try this
Code: (Select All)
Function calc_plus& (Byval a As Long byval b As Long)
because QB64 passes by reference by default


RE: how to pass parameters to H file? - Jack - 04-11-2023

note
QB64 Integer is 16-bit, so either change the QB64 declaration to long or change the C int to short


RE: how to pass parameters to H file? - Petr - 04-11-2023

The point is that no matter what data types I put there, I tried changing it on both sides, nothing compiles, it always ends up with an error message.


RE: how to pass parameters to H file? - Jack - 04-11-2023

Petr, I get the impression that you didn't try my suggestion
Code: (Select All)
Declare Library "plus"

    Function calc_plus% (byval a As Integer, byval b As Integer)

End Declare



c% = calc_plus(5, 6)

Print c%
Code: (Select All)
short calc_plus (short a, short b)

{

    short c = a + b;

    return c;

}



RE: how to pass parameters to H file? - SMcNeill - 04-11-2023

Declare Library "plus"
    Function calc_plus% (a As Integer, b As Integer)
End Declare


In the above, QB64 will attempt to pass the _OFFSETs of the two parameters for you, and I don't think that's what you're looking for.   To pass the value of those Integers, use BYVAL as Jack as suggested above.

Declare Library "plus"
    Function calc_plus% (BYVAL a As Integer, BYVAL b As Integer)
End Declare




Point 2 that you might try is to DECLARE DYNAMIC LIBRARY or DECLARE CUSTOMTYPE LIBRARY and see if that corrects the issue.  You're trying to send and receive INTEGER values (16-bit) and the C-code you've written is actually looking to use 32-bit LONG values.  DECLARE DYNAMIC and DECLARE CUSTOMTYPE aren't as particular about 100% type matching, so swapping to those might solve the issue for you.  I'll test exactly what all is needed later, when I get back home from physical therapy and give you a 100% working example then.  Wink


RE: how to pass parameters to H file? - Jack - 04-11-2023

Petr
your original declaracion
Code: (Select All)
Function calc_plus% (a As Integer, b As Integer)

passes a and b by reference or in C-speak as pointers, you need to specify Byval otherwise it's passed as reference


RE: how to pass parameters to H file? - SMcNeill - 04-11-2023

From some quick testing via remote connection to my Windows desktop, all you really seem to need is the BYVAL for both those a and b parameters.  Try that and then see if it won't work for you.  If not, then give it a shot with DECLARE CUSTOMTYPE and see how it goes.  Wink


RE: how to pass parameters to H file? - Petr - 04-11-2023

Thank you very much. I feel like an ox. But I'm glad to have a working example to lean on. Actually yes, yes, the biggest mistake was the missing word BYVAL. I tried to figure it out myself first....everything seemed logical....Thanks a lot guys!  Rolleyes