Read array Harbour from C

HMG en Español

Moderator: Rathinagiri

Post Reply
jparada
Posts: 433
Joined: Fri Jan 23, 2009 5:18 pm

Read array Harbour from C

Post by jparada »

Hi,
I am trying to read a harbour array from a function in C, but I have problems trying to read the strings, the function in C is displaying rare characters when the data type is a string.
img.png
img.png (3.25 KiB) Viewed 2474 times
I also need to know and display the string length,
I don't know how to do it either.

This is the code:

Code: Select all

#pragma -w2

PROCEDURE Main()
    LOCAL aValues := { .F., 'string 1', .T., .F., 5689, 6.593, 'text', 126, 0.3,9 }
    CLS
    
    TestArray( aValues ) 
 
RETURN

#pragma BEGINDUMP
#include "hbapi.h"

HB_FUNC_STATIC( TESTARRAY ) 
{
    #define btoa(x) ((x)?"true":"false")

    PHB_ITEM aValues = hb_param( 1, HB_IT_ARRAY );
    HB_ULONG size = hb_arrayLen( aValues );
    HB_ULONG i;
    int *tmpInt; 
    long *tmpLong;
    float *tmpFloat;    
    char *tmpString;
    HB_BOOL *tmpBool;     
     
    for( i = 1; i <= size; ++i ) 
    {
        switch ( hb_arrayGetType( aValues, i ) )
        {
            case HB_IT_STRING:
                tmpString = ( char* ) malloc( sizeof( char ) );
                
                *tmpString = hb_arrayGetC( aValues, i );      // hb_arrayGetC - Retrieves the string contained on an array element
                                                              // hb_arrayGetCPtr - Retrieves the string pointer on an array element
                printf( "valor cadena %c\n", *tmpString );
                //printf( "valor cadena \n" );
                break;
            
            case HB_IT_INTEGER:             
                tmpInt = ( int* ) malloc( sizeof( int ) ); 
                *tmpInt = hb_arrayGetNI( aValues, i ); 
                printf( "valor entero %d\n", *tmpInt );
                free( ( int* ) *tmpInt );
                break;
            case HB_IT_LONG:
                tmpLong = ( long* ) malloc( sizeof( long ) ); 
                *tmpLong = hb_arrayGetNL( aValues, i );
                printf( "valor entero largo");
                break;
            
            case HB_IT_DOUBLE:
                tmpFloat = ( float* ) malloc( sizeof( float ) );
                *tmpFloat = ( float ) hb_arrayGetND( aValues, i ); 
                printf( "valor double %f\n", *tmpFloat );  // decimal
                break;
            case HB_IT_LOGICAL: 
                tmpBool = ( HB_BOOL* ) malloc( sizeof( HB_BOOL ) );
                *tmpBool = hb_arrayGetL( aValues, i );                
                free( ( HB_BOOL* ) *tmpBool );
                //printf("%s\n", btoa( *tmpBool ));             // it works
                printf("%s\n", *tmpBool ? "verdadero" : "falso");
                break;             
            default: break; 
            } 
        } 
        
} 
#pragma ENDDUMP
Any idea?, I appreciate your help.

Regards,
Javier
User avatar
AUGE_OHR
Posts: 2093
Joined: Sun Aug 25, 2019 3:12 pm
DBs Used: DBF, PostgreSQL, MySQL, SQLite
Location: Hamburg, Germany

Re: Read array Harbour from C

Post by AUGE_OHR »

hi,

i'm not a C Guru so forgive me if i'm wrong.
it seems me you want to "transfer" Data "direct" :?:

i just know when try to access a C Function from xBase it most use a Windows Structure.

as harbour seems to be able to create Windows Structure i would try to store Array (convert to BIN ?) into a Member of that Structure. close Structure and send your own User Event to your C App to get data.
have fun
Jimmy
KDJ
Posts: 243
Joined: Mon Sep 05, 2016 3:04 am
Location: Poland

Re: Read array Harbour from C

Post by KDJ »

Javier

Try in this way:

Code: Select all

...
        switch ( hb_arrayGetType( aValues, i ) )
        {
            case HB_IT_STRING:
                printf( "valor cadena: \"%s\", length: %i\n", hb_arrayGetCPtr(aValues, i), (int) hb_arrayGetCLen(aValues, i) );
                break;
...
jparada
Posts: 433
Joined: Fri Jan 23, 2009 5:18 pm

Re: Read array Harbour from C

Post by jparada »

Hi Krzysztof,
Thanks for answering, I did the test and it works, one question, does the length also include the null character?

I appreciate your help.

Regards,
Javier
KDJ
Posts: 243
Joined: Mon Sep 05, 2016 3:04 am
Location: Poland

Re: Read array Harbour from C

Post by KDJ »

Javier

String length returned by hb_arrayGetCLen() is without NUL terminator.
jparada
Posts: 433
Joined: Fri Jan 23, 2009 5:18 pm

Re: Read array Harbour from C

Post by jparada »

Hi Krzysztof,
Thanks for the info.

Regards,
Javier
Post Reply