How to pass a string back to labview using node call library function

I want to use the LabVIEW Node Call Library function to access a DLL function, and this function returns the string displayed on my VI. How can I do it? I am very happy to return numbers from my DLL, but I am really trying to find examples of how to return a string.

+4
source share
2 answers

I assume from your question that you already have a DLL that can return numbers in Labview. To return a string from a DLL, I created a DLL with the following C ++ function

void returnString(char myString[]) { const char *aString = "test string"; memcpy(myString, aString, 12); } 

In Labview, I then use the Node Call Library function and configure it as follows

  Library Name or Path: c: \ path \ to \ my \ custom.dll
            Function Name: returnString
            Calling Convention: C

     Parameters: 
            Parameter: return type
                 type: void

            Parameter: arg1
                 type: String
                 string format: C String Pointer

     Function prototype:
         void returnString (CStr arg1);

After connecting the output arg1 on the block diagram to the line and start indicator. The line "test line" should appear on the front panel.

I tried to use a returnString function of type CStr, as in

 CStr returnString() { ... } 

but I got build errors when compiling a dll project.

Refresh

Thanks to the comment @ bk1e do not forget to pre-allocate space in Labview for the line.

+2
source

There are at least a few ways to return a string from the Node Call Library function:

  • Return the C string pointer from your DLL function and configure the Node Call Library function to get the return type of β€œC String Pointer”. Note that the returned string must be valid after the function returns, so it cannot be a pointer to a string allocated on the stack. This should be one of the following: allocated on the heap, statically allocated, constant string literal, etc.

    It looks like the examples/dll/regexpr/Regular Expression Solution/VIs/Get Error String.vi in the LabVIEW directory takes this approach.

  • Select the string in your VI, pass it to the Node Call Library function using the "String String" parameter proposed by Azim, and overwrite its contents in the DLL. One way to select a string is to use Initialize Array to create the u8 array of the desired size, and then use Byte Array To String to convert it to a string.

    Make sure that the string you are passing is large enough to hold the contents of your string and remember to pass the string length to the DLL so that it knows how big the buffer is. I believe the default parameter is an empty string. Finding out the correct string length may require a double call in the DLL if your first assumption VI is not large enough.

  • Pass the string to the Node Call Library function using the "String Handle" parameter and use the LabVIEW functions in your DLL to resize the string as needed. This requires your DLL to be specifically designed to interact with LabVIEW and requires linking to the static library provided by LabVIEW.

    An example of this method comes with LabVIEW as examples/dll/hostname/hostname.vi .

+3
source

All Articles