Overview This example demonstrates how to demystify the native .NET library, as it is not shown in any examples.
Description Here is a brief description of the example:
Assemblies are made of namespaces that contain classes which are to be instantiated and from which members/fields/methods are called.
The first thing to access a .NET object is to load its parent assembly and create an instance of this object.
The assembly must be fully qualified using the "AssemblyName, Version=x.x.x.x, Culture=language, PublicKeyToken=abcdefghij123456" format.
The objects are not known by CVI, but they are comparable to structs, so you need to create as many typedef struct as object types to instantiate. And because you don't know what the struct contain, the definition must be as follows:
typedef struct MyObject* ObjectA;
Because we are dealing with a pointer, CVI does not care at build time what type of data is contained.
The next steps are all about calling a property (Get/Set) and/or a Method. This is done with the CDotNetInvokeGenericMember function. Make sure:
- you are using the right class in the right namespace
- you know all the arguments and their data type
- you know the return type
This sample code show how a homemade assembly named Dummy.dll is called. The assembly source code is attached so you can see how it is written (and how simple it is, hence its name).
The CVI code will load Dummy.dll, instantiate a StrTime object and call the ToString() and GetNow() methods, and finally return both return parameters in a console.
.NET Framework 4.5
Steps to Implement or Execute Code
Open the CVI.NET Example.prj file
Edit the path of the assembly in the CDotNetRegisterAssemblyPath function. Note: this function is needed if the assembly is not loaded in the Global Assembly Cache.
Run the code.
**This document has been updated to meet the current required format for the NI Code Exchange.**
Eric M. - Senior Software Engineer Certified LabVIEW Architect - Certified LabVIEW Embedded Systems Developer - Certified LabWindows™/CVI Developer Neosoft Technologies inc.
Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.