Company Events Academic NI Developer Zone Support Solutions Products & Services Contact NI MyNI

Creating a CIN That Concatenates Two Strings

LabVIEW 8.5 Help
August 2007

NI Part Number:
371361D-01

»View Product Info

In this example, the CIN concatenates two strings and uses an input-output terminal. The top left terminal of the CIN takes in the first string as an input parameter to the CIN. The top right terminal of the CIN returns the result of the concatenation. This example shows only the block diagram and the code.

Complete the following steps to create the CIN.

Note  LabVIEW must be installed on the computer you use to create the CIN.
  1. Create the CIN. The following illustration shows the block diagram for this CIN.

  2. Save the VI as lstrcat.vi.
  3. Create a .c file for the CIN and name it lstrcat.c. LabVIEW creates the following .c file:

    /*
    * CIN source file
    */
    #include "extcode.h"
    CIN MgErr CINRun(
            LStrHandle var1,
            LStrHandle var2);
    CIN MgErr CINRun(
            LStrHandle var1,
            LStrHandle var2) {
        /* ENTER YOUR CODE HERE */
        return noErr;    
        }
  4. Fill in the CINRun function with the following example code:

    CIN MgErr CINRun(
            LStrHandle strh1,
            LStrHandle strh2) {
        int32 size1, size2, newSize;
        MgErr err;
        size1 = LStrLen(*strh1);
        size2 = LStrLen(*strh2);
        newSize = size1 + size2;
        if(err = NumericArrayResize(uB, 1L,
                (UHandle*)&strh1, newSize))
            goto out;
        /* append the data from the second string to first string */
        MoveBlock(LStrBuf(*strh2),
        LStrBuf(*strh1)+size1, size2);
        /* update the dimension (length) of the first string */
        LStrLen(*strh1) = newSize;
    out:
        return err;
        }

In this example, CINRun is the only routine that performs substantial operations. CINRun concatenates the contents of strh2 to the end of strh1, with the resulting string stored in strh1. Before performing the concatenation, NumericArrayResize resizes strh1 to hold the additional data.

If NumericArrayResize fails, it returns a nonzero value of type MgErr. In this example, NumericArrayResize could fail if LabVIEW does not have enough memory to resize the string. Returning the error code gives LabVIEW a chance to handle the error. If CINRun reports an error, LabVIEW aborts the calling VIs. Aborting the VIs might free up enough memory so LabVIEW can continue running.

After resizing the string handle, MoveBlock copies the second string to the end of the first string. MoveBlock is a support manager routine that moves blocks of data. Finally, this example sets the size of the first string to the length of the string.

Refer to the Concatenate strings VI in the labview\examples\cins\lstrcat directory for an example of creating a CIN that concatenates strings.


Resources


 

Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality? 
 Answered Your Question? 
Add Comments 1 2 3 4 5 submit