LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

how do we assign C Structure char value to labview LStrHandle data type

I am building .lsb file from CIN code in VI file. In CIN code I have structure with char[32] datatype. I have LStrHandle String data type in Labview to which I need to assign the value of char[32] of structure variable. When I do strcpy(lstrHAndle data variable,structure variable) .I try to run the VI it crashes and gives memory default error.
Can anyone guide me how to assign structure char[32] value to LStrHandle data type.
0 Kudos
Message 1 of 4
(6,700 Views)
It's difficult to tell from the limited amount of detail given, but you will most propably have to use a pointer. If you assing values to a structure member you can either use structure.member notation or a pointer notation structure->member.

What does the structure look like and how is it initialised and declared?
Jattie van der Linde
Engineering Manager, Software & Automation
TEL Magnetic Solutions Ltd
0 Kudos
Message 2 of 4
(6,697 Views)
Hi,

The LStrHandle data type is a pointer to a pointer to a structure that contains two elements: a numeric that indicates the size of the string; and the string. This structure resides in the LabVIEW memory space, so you must have LabVIEW initialize that string before you can assign any valu in your C code.

Here is some general pointer to what you can do:

1.-Initialize an array of 32 U8s in LabVIEW.
2.-Flatten the array to string.
3.-Pass this initialized array to your CIN.
4.- In your C code dereference the string handle until you get to the actual string.
5.-Do a memcpy() of your buffer to the buffer contained in the structure.
6.-Return the LStrHandle.

You can also initialize the LabVIEW string in your C code, but it requires the use of the me
mory management functions and it's much more complicated.

I hope this gives you a starting point. Please let me know if you have any further questions.

Regards,

Juan Carlos
N.I.
0 Kudos
Message 3 of 4
(6,697 Views)

I had that problem too, the thing you have to understand before is that a LStrHandle DATA, is composed by two sectors, the characters chain, and the size of it.

 

For example:

#include "extcode.h"

#include "hosttype.h"

 

#include <string.h>

 

MgErr CINRun(int16 *Value, int16 *Numeric, LStrHandle labviewstring,

            LVBoolean *findFIRSTerr, int16 *SizeFindFirst);

 

MgErr CINRun(int16 *Value, int16 *Numeric, LStrHandle labviewstring,

            LVBoolean *findFIRSTerr, int16 *SizeFindFirst)

            {

Char value[32];    // or  char *value;

 

Short size;

 

Size=strlen(value);

 

 

memcpy(LStrBuf(*labviewstring),value,32)

 

or

 

memcpy(LStrBuf(*labviewstring),value,size)

 

or

 

strncpy(LStrBuf(*labviewstring),value,size)

 

 

and after that you should do this too.

 

LStrLen(*labviewstring)=size;

 

This works good, but lately in a random way I get the memory crash, what I think is happening is that I have to initialize the LStrHandle before using it because its pointer get sometimes a reserved sector of memory and it crash, my question is how to create and initialize a LStrHandle data, in C?Smiley Wink

 

Ing. Jorge M. Gamboa
Departamento de Mecatrónica
Tec de Monterrey
e-mail: ing.gamboa@gmail.com

0 Kudos
Message 4 of 4
(6,355 Views)