B4R Question [SOLVED] how to pass array object to inline C?

dragonguy

Active Member
Licensed User
Longtime User
I want to pass object array to inline c, how to do that?

example:
B4X:
Public Sub test
    Dim o(2) As Object
    o(0)="testing 1".GetBytes
    o(1)="testing 2".GetBytes
    RunNative("readFile", o)
End Sub

#if c
void writeFile(B4R::Object* o){
    B4R::Array* b = (B4R::Array*)B4R::Object::toPointer(o);
    char* t1 = (char*)b(0)->data;
    Serial.printf("Create Dir: %s\n", t1);
   
    char* t2 = (char*)b(1)->data;
    Serial.printf("Create Dir: %s\n", t2);
}
#End If

when run the program i got error messages below:
B4X:
In function 'void test1(B4R::Object*)':
b4r_main.cpp:9:26: error: 'b' cannot be used as a function
     char* t1 = (char*)b(0)->data;
                          ^
exit status 1

how to fix this problem?

i found the solution
B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    Public Serial1 As Serial
    Type myDataType(a() As Byte, b As Byte, c As Byte)
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    Dim myData As myDataType
    myData.a = "123".GetBytes
    myData.b = 2
    myData.c = 3
    RunNative("MyFunction",myData)
End Sub

#if C

void MyFunction(B4R::Object* o)
{
    _mydatatype* tmp = (_mydatatype*)B4R::Object::toPointer(o) ;
    
    ::Serial.println(tmp->c);
    
    B4R::Array* b = tmp->a;
    char* c = (char*)b->data;
      ::Serial.println(c);
}
#End If
 
Last edited:

Daestrum

Expert
Licensed User
Longtime User
In C (and java) array subscripts are identified by brackets [ ] not parenthesis ( ) as in B4x.
 
Upvote 0
Top