Get variables

GetVariables gets the list of the user variables. You need to create a space to store the retrieved variables, or use an array. This function is not required to use if you handle all your user variables in one application, because after you add variables, you can obtain the pointers to the variables, which allows you to access the variables. However, if you have other applications that need to access the variables, you must use GetVariable or GetVariables to access the variable in these applications. To demonstrate how to use GetVariables, we write it in CreateUserVariables.

The following code is written in the function CreateUserVariables.

  1. Declare the variables for the buffer length and the number of the variables. These variables will be used by GetVariables later.
  2. Copy
    /*------Get variables------*/

    //Declare the variables for the buffer length and the total of the variables.
    int BufferLength = 3;
    int Variables = 0;
  3. Declare an array to store the retrieved variables. The length of the array is determined by the number of user variables. To show the types of the user variables in strings, declare the logType and Type array. wstring is to be compatible with the wide char used by the Name member in UserVariable. Later we will display all the members together.
  4. NOTE:  Another way to store the retrieved variables is to use the malloc function to dynamically allocate a memory space and free to free the space. If you want to use them, uncomment the malloc-free code and comment out UserVariable Space[3].

    Copy
    /*There are two ways to allocate memory space to store the list of user variables.
      One is malloc() - free(). The other is array.*/

    //UserVariable *Space = (UserVariable*)malloc(BufferLength * sizeof(UserVariable));

    //Declare an array to store the list of user variables. Max is 64.
    UserVariable Space[3];

    //The types of a log.
    wstring logType[11] = { L"logBool", L"logByte", L"logSInt", L"logWord", L"logInt",
                            L"logDWord", L"logDInt", L"logFloat", L"logLWord", L"logLInt",
                            L"logDouble" };

    //The array used to store the types of user variables.
    wstring Type[3] = { L"NULL" };
  5. Use GetVariables to get the list of the variables and print the total of the variables to see whether all the user variables are retrieved.
  6. Copy
    GetVariables(BufferLength, &Variables, Space);
    RtPrintf("Total variables: %d\n\n", Variables);
  7. Log types are stored in the KsLogDataType type. By default, these types will be displayed as numbers, which is hard to identify. We convert them to strings so we can know each variable's type at a glance.
  8. Copy
    //Convert the Type value into string.
    for (int i = 0; i < 3; i++)   //For Type array.
    {
        for (int j = 0; j < 11; j++)   //For logType array.
        {
            if (Space[i].Type == j)
                Type[i] = logType[j];   //Use Type elements to save the result.
        }
    }
  9. Use wprintf to display each variable's attributes.
  10. Copy
    //Display each user variable's attributes.
    wprintf(L"Variables details: ID: %d, Name: %s, Value: %f, Type: %s\n\n",
        Space[0].Id, Space[0].Name, *((double*)Space[0].Value), Type[0].c_str());
    wprintf(L"Variables details: ID: %d, Name: %s, Value: %f, Type: %s\n\n",
        Space[1].Id, Space[1].Name, *((double*)Space[1].Value), Type[1].c_str());
    wprintf(L"Variables details: ID: %d, Name: %s, Value: %d, Type: %s\n\n",
        Space[2].Id, Space[2].Name, *((BOOL*)Space[2].Value), Type[2].c_str());
  11. Remember to free the space if you have used malloc to allocate the memory. Since we didn't use it, we comment out the free code.
  12. Copy
    //Free the space allocated to read user variables.
    //If you use malloc(), you need to use free(). 
    //free(Space);

Complete code

The complete code should be as follows:

Copy
//Declare the variables for the buffer length and the total of the variables.
int BufferLength = 3;
int Variables = 0;

/*There are two ways to allocate memory space to store the list of user variables.
  One is malloc() - free(). The other is array.*/

//UserVariable *Space = (UserVariable*)malloc(BufferLength * sizeof(UserVariable));

//Declare an array to store the list of user variables. Max is 64.
UserVariable Space[3];

//The types of a log.
wstring logType[11] = { L"logBool", L"logByte", L"logSInt", L"logWord", L"logInt",
                        L"logDWord", L"logDInt", L"logFloat", L"logLWord", L"logLInt",
                        L"logDouble" };

//The array used to store the types of user variables.
wstring Type[3] = { L"NULL" };

GetVariables(BufferLength, &Variables, Space);
RtPrintf("Total variables: %d\n\n", Variables);

//Convert the Type value into string.
for (int i = 0; i < 3; i++)   //For Type array.
{
    for (int j = 0; j < 11; j++)   //For logType array.
    {
        if (Space[i].Type == j)
            Type[i] = logType[j];   //Use Type elements to save the result.
    }
}

//Display each user variable's attributes.
wprintf(L"Variables details: ID: %d, Name: %s, Value: %f, Type: %s\n\n",
    Space[0].Id, Space[0].Name, *((double*)Space[0].Value), Type[0].c_str());
wprintf(L"Variables details: ID: %d, Name: %s, Value: %f, Type: %s\n\n",
    Space[1].Id, Space[1].Name, *((double*)Space[1].Value), Type[1].c_str());
wprintf(L"Variables details: ID: %d, Name: %s, Value: %d, Type: %s\n\n",
    Space[2].Id, Space[2].Name, *((BOOL*)Space[2].Value), Type[2].c_str());

//Free the space allocated to read user variables.
//If you use malloc(), you need to use free(). 
//free(Space);