From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Example Code

Dynamically Call .NET Code using LabWindows/CVI

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

  • Other

Code and Documents

Attachment

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.


Requirements

  1. LabWindows/CVI 2013SP2,
  2. .NET Framework 4.5
  3. NI-SCOPE device


Steps to Implement or Execute Code

  1. Open the CVI.NET Example.prj file
  2. 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.
  3. 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.

Comments
ASIC_LabRat
Member
Member
on

I keep getting this error -6579. I verified all of the paths and have a clean build. any ideas why my .NET is not working?

Eric.M
Active Participant
Active Participant
on

The assembly I used in this example was generated for .NET 4.5 so you have to make sure you have this version installed on your PC. I'm adding this to the SW Req !

Eric M. - Senior Software Engineer
Certified LabVIEW Architect - Certified LabVIEW Embedded Systems Developer - Certified LabWindows™/CVI Developer
Neosoft Technologies inc.

ASIC_LabRat
Member
Member
on

Ok, So I need to know from the dll's author what .NET is being used?

ASIC_LabRat
Member
Member
on

I checked my Windows 7 PC. The .NET framework is 4.5.2. Any other dependencies?

Eric.M
Active Participant
Active Participant
on

Sorry about the troubles you are having. I can't replicate this error unless the name of the assembly is wrong. Same goes for 3 different computers here 😕

Are you trying to run the code with the attached assembly? On which function is the error returned (Register or Load) ?

Eric M. - Senior Software Engineer
Certified LabVIEW Architect - Certified LabVIEW Embedded Systems Developer - Certified LabWindows™/CVI Developer
Neosoft Technologies inc.

ASIC_LabRat
Member
Member
on

I have run this on 2 machines, one is a 32bit windows 7 Enterprise sp1, the other one is a 64bit windows 7 Enterprise sp1    Errors on this line      status = CDotNetLoadAssembly ("Dummy, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", &assmHandle);      PrintErr(status); //[=-6579]  Variables in main  (int) argc = 1 (char**) argv = "Test Calendar.exe."  Local Variables in main (char*) TimeString = "360\255\272\... repeated 28 times... p"  (char*) ClassName = "360\255\272\... repeated 28 times ... 316\210\1" (int) status = -6579  possible that "Admin privileges" could cause this issue? this is a enterprise managed computer...

ASIC_LabRat
Member
Member
on

Eric my LabWindows CVI is this version: Full Development system (DeBug only) Version 13.0.2 (278)

GarryG
Member
Member
on

Eric, Is it possible to turn this into a DLL that can be called from LabVIEW with the call library node so .NET assemblies could then be loaded dynamically in LabVIEW?  I've run into the need to do this in LabVIEW several times, and so have others according to the forums.

CLA, CTA
Not my tempo... AGAIN!
GarryG
Member
Member
on

Also, does the assembly have to be strongly named for this to work?  Is there a way to do this without it being strongly named?

CLA, CTA
Not my tempo... AGAIN!