B4R Question [inline C] possible to return value from C-function ?

peacemaker

Expert
Licensed User
Longtime User
HI, All

If you use separate functions in the C-code, that returning some error-code values - how to return it into B4R, without global variables ?
 
  • Like
Reactions: RJB

Daestrum

Expert
Licensed User
Longtime User
Can you use this ?
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Can you use this ?
I know very little about C-programming, but this variant is much less elegant than just the B4R's public variable...
No any other elegant solution ?
 
Upvote 0

RJB

Active Member
Licensed User
Longtime User
Hi,
Like you I know little C so have to get by as necessary!
I found this: https://www.b4x.com/android/forum/threads/using-progmem-to-save-memory.71114/#content and modified the code to:
B4X:
#if C

B4R::Object returnvalue;

B4R::Object* somesub(B4R::Object* o) {
    return returnvalue.wrapNumber(o->toLong());
}

#end if

Private Sub AppStart
    Serial1.Initialize(115200)
    Log(CRLF, ">>>>>>>>>>>>>>>>>>>AppStart<<<<<<<<<<<<<<<<<<<<<<<")

    Dim t As Boolean = RunNative("somesub", False)
    Log("Boolean = ", t)
    Dim u As Boolean = RunNative("somesub", True)
    Log("Boolean = ", u)
    Dim v As Byte= RunNative("somesub", 3)
    Log("Byte = ", v)
    Dim w As Int = RunNative("somesub", 4)
    Log("Int = ", w)
    Dim x As Long = RunNative("somesub", 5)
    Log("Long = ", x)
    Dim y As Double = RunNative("somesub", 6)
    Log("Double = ", y)
    Dim z As Float = RunNative("somesub", 7)
    Log("Float = ", z)

end sub

Log
----

Boolean = 0
Boolean = 1
Byte = 3
Int = 4
Long = 5
Double = 6
Float = 7

Seems to work, possibly someone who understands C could say if it makes any sense or will cause problems?
 
Upvote 0
Top