Example Code

Using CVI to Create and Access a .NET Array

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

Download All

Overview
This example demonstrates how to allocate .NET memory in order to initialize an array of strings to a set of values.


Description
CVI provides an API to create and use .NET objects within the C environment.  This example demonstrates how to allocate .NET memory in order to initialize an array of strings to a set of values.


Requirements

  • LabWindows/CVI 2012 (or compatible)


Steps to Implement or Execute Code

  1. Open the .prj file and Run

 

Additional Information or References

 

C code 

#include <cvidotnet.h>

#include <ansi_c.h>

                      

void CreateDotNetArray (void);

 

/// HIFN  The main entry-point function.

/// HIPAR argc/The number of command-line arguments.

/// HIPAR argc/This number includes the name of the command-line tool.

/// HIPAR argv/An array of command-line arguments.

/// HIPAR argv/Element 0 contains the name of the command-line tool.

/// HIRET Returns 0 if successful.

int main (int argc, char *argv[])

{

       CreateDotNetArray();

      

       return 0;

}

 

//Create .NET array of Strings and read them back to be displayed

void CreateDotNetArray (void)

{

 

       int          i = 0;

       char       *pChannelsList[3] = {0};

       int          lookupIndex = 0;

 

       //The length of the list.  This parameter will be passed to the CDotNetCreateArray function

       ssize_t  iChannelLength = 3;

       CDotNetHandle dotNetArray = 0;

 

       //Variables that will hold the strings as they are read back

       char *stringEle0 = "";

       char *stringEle1 = "";

       char *stringEle2 = "";

 

       //Allocate the DotNet memory for strings

       for (i=0; i<3; i++) {

                     pChannelsList[i] = CDotNetAllocateMemory(250 * (sizeof(char)));

       }                                                                           

 

       //Provide a list of strings

       strcpy(pChannelsList[0],"This is test string #1");

       strcpy(pChannelsList[1],"This is test string #2");

       strcpy(pChannelsList[2],"This is test string #3");

             

       //numberOfDimensions: dims in the array to be created.  1 in our case

       //lowerBounds: array of lower bounds of each dimension.  0 is the default

       //lengths: a pointer to an array of lengths for the dimensions.  This is NOT the memory length, simply the number of elements in each dimension.

       //                   in our case this is the length of our string array which is 3, initialized above

       //array: a reference to the .NET array handle                    

       CDotNetCreateArray(CDOTNET_STRING, 1, 0, &iChannelLength, pChannelsList, &dotNetArray);

      

       //Retrieve the elements from the array to display them 

       CDotNetGetArrayElement(dotNetArray, CDOTNET_STRING, 1, &lookupIndex, &stringEle0);

       lookupIndex++;

       CDotNetGetArrayElement(dotNetArray, CDOTNET_STRING, 1, &lookupIndex, &stringEle1);

       lookupIndex++;

       CDotNetGetArrayElement(dotNetArray, CDOTNET_STRING, 1, &lookupIndex, &stringEle2);

      

       //Display the elements

       printf("%s\n%s\n%s", stringEle0, stringEle1, stringEle2);

      

       //Free the DotNet memory

       for (i=0; i < 3; i++) {

              CDotNetFreeMemory(pChannelsList[i]);

       }

      

       printf("\n\nPress Enter to Exit");

      

       //Display output before exiting

       getchar();

}

  **This document has been updated to meet the current required format for the NI Code Exchange.**

 

 

Justin D
Applications Engineer
National Instruments
http://www.ni.com/support/

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.