ni.com is currently undergoing scheduled maintenance.

Some services may be unavailable at this time. Please contact us for help or try again later.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

LStrHandle C++ String conversion

Hi, I am wiring a cluster that holds a string into a Call External Library Node and following the "Adapt to Type" article and "Calling External Code in LabVIEW" manual, I have created a .cpp file with this code in it:

typedef struct {
double Numeric;
double Numeric2;
LStrHandle String;
} TD1;

However, I am using a function that requires a C++ string to be passed into it and it returns a C++ string that needs to be assigned to LStrHandle String. Is it possible to convert the LStrHandle format into a C++ string and vice versa? I found something regarding the LStrPrintf functions, but I'm fairly new with programming and I did not follow it very well. Thank you in advance.
0 Kudos
Message 1 of 5
(6,414 Views)

@abriggs8 wrote:
Hi, I am wiring a cluster that holds a string into a Call External Library Node and following the "Adapt to Type" article and "Calling External Code in LabVIEW" manual, I have created a .cpp file with this code in it:

typedef struct {
double Numeric;
double Numeric2;
LStrHandle String;
} TD1;

However, I am using a function that requires a C++ string to be passed into it and it returns a C++ string that needs to be assigned to LStrHandle String. Is it possible to convert the LStrHandle format into a C++ string and vice versa? I found something regarding the LStrPrintf functions, but I'm fairly new with programming and I did not follow it very well. Thank you in advance.




I'm not familiar enough with C++ to tell you exactly what to do. I specially find the C++ string operators quite hard to comprehend. But a LabVIEW string handle is basically as follows:

typedef struct
{
int32 len;
uInt8 buf[0];
} **LStrHandle;

The len integer tells you how many characters are there while the buf member points to the actual string but without a NULL termination character.

So in C you could do something like this:

MgErr MyFunction(LStrHandle handle)
{
char *string = malloc(LStrLen(*handle) + 1);
memcpy(string, LStrBuf(*handle), LStrLen(*handle));
string[LStrLen(*handle)] = 0;

use your string in whatever way you like. Then copy back a C string into the LabVIEW string.
Either use LStrPrintf or doing it yourself:

int32 len = strlen(string);
MgErr err = NumericArrayResize(uB, 1, &handle, len);
if (err) return err;
memcpy(LStrBuf(*handle), string, len);
LStrLen(*handle) = len;
}

You will need to adapt this to C++ strings somehow.

Rolf Kalbermatter

Message Edited by rolfk on 06-20-2005 05:15 PM

Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
Message 2 of 5
(6,395 Views)


@abriggs8 wrote:
Hi, I am wiring a cluster that holds a string into a Call External Library Node and following the "Adapt to Type" article and "Calling External Code in LabVIEW" manual, I have created a .cpp file with this code in it:

typedef struct {
double Numeric;
double Numeric2;
LStrHandle String;
} TD1;

However, I am using a function that requires a C++ string to be passed into it and it returns a C++ string that needs to be assigned to LStrHandle String. Is it possible to convert the LStrHandle format into a C++ string and vice versa? I found something regarding the LStrPrintf functions, but I'm fairly new with programming and I did not follow it very well. Thank you in advance.




I just had a look at my C++ textbook and it shouldn't be terribly difficult to do it for C++ as well:

MgErr MyFunction(LStrHandle handle)
{
MgErr err;

string s(LStrBuf(*handle), 0, LStrLen(*handle));

Now you can pass s as a C++ string.

Then copy back a C++ string into the LabVIEW string.

err = NumericArrayResize(uB, 1, &handle, s.length);
if (err) return err;
memcpy(LStrBuf(*handle), s.cstr, s.length);
LStrLen(*handle) = s.length;
return noErr;
}
Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 3 of 5
(6,388 Views)
Hi rolfk, that helps me out a great deal. It was much easier to understand than most explanations I've gotten. However, the line that reads:

string s(LStrBuf(*handle), 0, LStrLen(*handle));

is giving me a compile error. It says that it cannot convert parameter 1 from 'uChar*__w64' to 'const std::basic_string<_Elem,_Traits,_Ax>::_Myt'. I'm guessing the LStrBuf function is not returning the proper data type(which appears to be a C++ string) to be passed into the constructor there. I'm unfamiliar with that function so I'm not sure how to proceed to fix this. How might I proceed to do this? Again, thank you in advance for the advice.
0 Kudos
Message 4 of 5
(6,382 Views)


@abriggs8 wrote:
Hi rolfk, that helps me out a great deal. It was much easier to understand than most explanations I've gotten. However, the line that reads:

string s(LStrBuf(*handle), 0, LStrLen(*handle));

is giving me a compile error. It says that it cannot convert parameter 1 from 'uChar*__w64' to 'const std::basic_string<_Elem,_Traits,_Ax>::_Myt'. I'm guessing the LStrBuf function is not returning the proper data type(which appears to be a C++ string) to be passed into the constructor there. I'm unfamiliar with that function so I'm not sure how to proceed to fix this. How might I proceed to do this? Again, thank you in advance for the advice.




Well LStrBuf is only a macro accessing the string buffer in the handle but it is a little bit complicated so that the C++ compiler might get into trouble here seeing a type he doesn't know to interpret as C string.

Maybe you could try to use something like

s((*handle)->str, 0, LStrLen(*handle));

instead, or you might even have to cast the buffer explicitedly to a (char *) to make it work such as:

s((char *)LStrBuf(*handle), 0, LStrLen(*handle));
s((char *)(*handle)->str, 0, LStrLen(*handle));

Rolf Kalbermatter
Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 5 of 5
(6,369 Views)