LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

C string to LabView string



@nmxnmx wrote:
Thanks you very much for your explanation daijoubu, and yes It's for sure I will read the links you gave me... Thanks very much again Smiley Happy

Daijoubu is indeed right about his findings. One other approach to solve this problem would be to declare your variable static.
This makes the variable to be allocated on the heap instead of the stack and therefore it will survive the function call. The nasty thing about this solution is that it is in fact a global and therefore not multithreading safe anymore. So you would either have to configure your CLN to run in the UI thread or face consequences such that the returned string can suddenly change its value because you call the same function in another thread in parallel.
In this simple example since you always copy a constant string this wouldn't be the case but then this function makes little sense in a real world shared library, so concerns about multithreading definitely are at its place.

char *test_string()
{
    static char string_data[MAXDATA]; /*MAXDATA previously defined*/
    strcpy(string_data, "string_test");
    return string_data;
}

This is equivalent to:

static char string_data[MAXDATA]; /*MAXDATA previously defined*/

char *test_string()
{
    strcpy(string_data, "string_test");
    return string_data;
}

and as you see creates in principle a global variable, which makes this function multithreading unsafe.

Rolf Kalbermatter
Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 11 of 14
(1,252 Views)
Thanks too rolfk, and well as you know a function such as :

char *test_string()
{
    static char string_data[MAXDATA]; /*MAXDATA previously defined*/
    strcpy(string_data, "string_test");
    return string_data;
}

it was just a test to do the real function, so now I will thing the best is do it like:

char *test_string(char *lstring)
{
    /* Code of function */
    one_funtion_that_use_string(lstring);
    return lstring;
}

where lstring is a LabView String coming from a LabView String control, and the return value is goin to be placed in a LabView String indicator.
0 Kudos
Message 12 of 14
(1,243 Views)


@nmxnmx wrote:
Thanks too rolfk, and well as you know a function such as :

char *test_string()
{
    static char string_data[MAXDATA]; /*MAXDATA previously defined*/
    strcpy(string_data, "string_test");
    return string_data;
}

it was just a test to do the real function, so now I will thing the best is do it like:

char *test_string(char *lstring)
{
    /* Code of function */
    one_funtion_that_use_string(lstring);
    return lstring;
}

where lstring is a LabView String coming from a LabView String control, and the return value is goin to be placed in a LabView String indicator.


In your example lstring is not really a LabVIEW string in terms of datatype but a C string instead. As such it is very important that you preallocate the memory in LabVIEW for your C function to write into the string. If you wire up a LabVIEW control to the CLN and configure the datatype as C string it will only pass a pointer to the data contained in the LabVIEW string control. If the string control contains an empty string you will basically pass a pointer to a one byte memory area to the DLL. Any function trying to write data into that string will write past the end of that one byte buffer and overwrite data that might and usually is vital to LabVIEW. LabVIEW might immediately display a Protection Fault dialog, crash after the call to the function, crash later on maybe only when you try to close it, or just corrupt your VI when saving it to disk.

In order to allocate enough space for the string you have to know the longest possible string the function can return (or pass the actual buffer length as an extra parameter to the function) and allocate that much of data. To allocate the data you best can use an Initialize Array function with a datatype input of an U8 numeric, the desired length and then wiring the resulting array through the Byte Array to String node.

Rolf Kalbermatter
Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
Message 13 of 14
(1,236 Views)
Thanks you for your explanation and sorry for answer you too late Smiley Sad
0 Kudos
Message 14 of 14
(1,220 Views)