Overview
The attached example VI calls the .NET DLL that accompanies it, which is a .NET class built in C# to emulate a very basic calculator.
Description
Although many shared libraries exist, and have the same extension of *.dll, you may or may not be aware that there are in fact two very different ways that DLLs are called from LabVIEW, depending on what language the Dynamic Linked Libraries (DLLs) were written and compiled in. DLLs written in both ANSI C and the .NET languages (like VB.NET and C#.NET) both have the extension *.dll when compiled, and may appear to be the same on the surface, but are inherently different in the way they are called from LabVIEW. Unlike a C-style DLL, which is called from LabVIEW using a Call Library Function Node, .NET DLLs are called using a .NET Constructor Node (Connectivity » .NET » Constructor Node in the Functions Palette). This function, when placed on the block diagram, will allow you to browse for the .NET assembly (or DLL) that you wish to use. Once you select the DLL, and the class from the DLL that you want to use, LabVIEW instantiates an object of that class, and passes a reference to the object out. With this object, you can then access all of the properties and methods of the class using Property Nodes and Invoke Nodes.
Steps to Implement or Execute Code
Requirements
LabVIEW 2012 (or compatible)
.NET 3.5 Framework (or compatible)
Additional Information or References
Block Diagram
Front Panel
**This document has been updated to meet the current required format for the NI Code Exchange. **
Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.
Thank you for the sample, but could you please add the C# source code (complete with solution and project file) ?
Thank you very much for your sample code. I am wondering whether both VC$ and LabView can call the same .net DLL?
George
There is no source code for the .Net assembly. Could you please post?
I got an error when I try to call the DLL got from other people. The error is saying "This assembly is built a runtime newer than the cirrently loaded runtime and cannot be loaded.
How do I know the current runtime version I have?
How do I update runtime loaded in my machine?
Black
I am not trying to hijack your post but am simply posting since it is relevant. I implemented a library in LabVIEW that uses Perforce's .NET API, which is a prototypical .NET API with lots of objects and collections. So the LabVIEW library is a non-trivial example of how to properly handle multiple constructed .NET objects, .NET collections, and how to properly close out .NET references from LabVIEW, which is very important. When writing this library, I found there were very little non-trivial examples of how to use .NET from LabVIEW, although your post is nice since it shows how users can write their own .NET assemblies to be called from LabVIEW.
The library can be found here: https://forums.ni.com/t5/Example-Code/LabVIEW-Perforce-API-based-upon-Perforce-NET-API/ta-p/3526675
For those asking for the source code for the dotnet assembly, I was able to decompile it with JetBrains dotPeek. xD
// Decompiled with JetBrains decompiler
// Type: Calculator.TI83
// Assembly: TI83, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: A46ECFF7-7D07-41CD-ACB2-66DC4E3FD6FB
// Assembly location: C:\Users\ebarber\Downloads\Calling dotNet assemblies in LabVIEW\Calling dotNet assemblies in LabVIEW\TI83.dll
namespace Calculator
{
public class TI83
{
public string manufacturer = "Texas Instruments";
public string model = nameof (TI83);
public int add(int x, int y) => x + y;
public int subtract(int x, int y) => x - y;
public int multiply(int x, int y) => x * y;
public double divide(double x, double y) => x / y;
public double square(double x) => x * x;
}
}
EDIT:
To those who are new, you will find that a decompiler might translate to newer .cs code, in order to use something that is compatible with labview, you would need framework dotnet. something like this should be compatible with
You will need to find the directory for it too to compile. Mine was in C:\Windows\Microsoft.NET\Framework64\v4.0.30319
namespace Calculator
{
public class TI83
{
public string manufacturer = "Texas Instruments";
public string model = typeof(TI83).ToString();
public int Add(int x, int y) {
return x + y;
}
public int Subtract(int x, int y) {
return x - y;
}
public int Multiply(int x, int y) {
return x * y;
}
public double Divide(double x, double y) {
return x / y;
}
public double Square(double x) {
return x * x;
}
}
}