Calling a C# .Net function in LabVIEW

Are you working with a team having LabVIEW and C# developers? Do you have any measurements or reusable modules developed in C# and looking for possible approaches to reuse them in your LabVIEW application? Perfect! I hope this blog will share some useful and interesting information.

Let's get started!


Accessing the properties and methods from a C# class in LabVIEW

Let's consider a simple StandardCalculator class written in C# as shown below. Create a DLL(C# .Net assembly) for your C# code using Visual Studio.

namespace Calculator
{
    public class StandardCalculator
    {
        public double Add(double a, double b)
        {
            return a + b;
        }

        public double Subtract(double a, double b)
        {
            return a - b;
        }

        public double Multiply(double a, double b)
        {
            return a * b;
        }

        public double Divide(double a, double b)
        {
            return a / b;
        }
    }
}

Let's see how to invoke the methods from the StandardCalculator class in LabVIEW.

Step 1 - The first step in any programming language to access the methods of a class is to create an object instance for the class. The Object instance for the StandardCalculator class can be created using Constructor Node in LabVIEW.

Step 2 - Browse for the path of the DLL(.Net Assembly) created using Visual Studio that contains the required class library.

Step 3 - The "Select .Net Constructor" will display all the classes defined in the DLL(.Net assembly). Select the class and the required constructor using which you would like to create the object instance.

Step 4 - Now that we have access to the object instance of the class, we can use the LabVIEW invoke node to access the methods and the LabVIEW property node to access the properties of the class.

Parameters can be passed through this invoke node as shown below.

Step 5 - This is the last but the most important step. Close the .Net object reference using the Close Reference Node in LabVIEW.

Failing to close the reference will result in reference leaks while executing the LabVIEW VI. Desktop Execution Trace Toolkit can be used to capture these reference leaks as shown below.

This reference leak is addressed by closing the .Net object reference as shown below.


In this way, you can access the properties and methods of a C# class in LabVIEW.


Accessing the properties and methods from a C# Static class in LabVIEW

Following the procedure explained in the above section will not work for calling methods from a static class. This is because, we cannot create an object instance for a static class and if we attempt to create an instance of the .Net object using the .Net Constructor Node, then you will see the message "This class contains no public constructors".

Does that mean the properties and methods of a static class cannot be invoked from LabVIEW? No, we can. Let's see how to call a method from a static class.

Consider a simple static class code for example:

namespace Calculator
{
    public static class ScientificCalculator
    {
        public static double Log(double x)
        {
            return Math.Log10(x);
        }

        public static double Square(double x)
        {
            return x * x;
        }
    }
}

Step 1 - Drop an invoke node in the LabVIEW block diagram.

Step 2 - Right-click on the invoke node and goto Select Class -> .Net -> Browse... to select the path of the DLL(.Net Assembly).


Step 3 - Browse the object from the assembly.


Step 4 - Now we will be able to access all the methods available in the static class using this invoke node.

In this way, you can use the LabVIEW invoke node to access the static methods from a static class as well as the non-static class. You can use the LabVIEW property node to access the static properties from a static class and a non-static class. 


If you are thinking - hey Engineering Ranger, did you forget to close the .Net reference returned by the invoke node, it might result in reference leaks?

Not really required, since we are calling a static method, we have not created an object reference. So it's not required to use the Close Reference Node while invoking the properties/ methods from a static class. This can be verified using the Desktop Execution Track Toolkit as shown below.


Engineering Ranger is working on curating some more interesting topics to explain instrument session sharing between C# and LabVIEW, debugging a .Net assembly call, and a lot more. Stay tuned for updates.


Written by,
Venkatesh Perumal Pranay C

Post a Comment

0 Comments