LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Pass a reference from C# to LabView VI

I have a VI that I am calling from a C# app using the ActiveX VirtualInstrument method. I need to pass a reference to a C# object to my VI. What I've tried to do is create a Control of type Variant on my VI Front Panel, I also added a .NET RefNum object and I set the .NET Class to be my desired C# type. From my C# code I call VirtualInstrument.SetControlValue and I pass my reference.

On the VI side I take the output of my Variant control and input that into a 'Variant To Data', I connect my .NET Refnum to the type input of my 'variant to data' so I'm basically trying to cast my variant to my desired .NET type.

If I take the output of my 'variant to data' object and input that to an invoke node I can see that I get all of the methods of my .NET object so that seems to be the right thing to do. The problem is in passing my reference. I end up getting a type mismatch error at my 'variant to data' when I try to run.

I guess this isn't the proper way to pass a reference. Could somebody please help me and tell me the right way to do it?

 

           VirtualInstrument vi = labVIEWAPP.GetVIReference(myPath, "", false, 0);
            vi.OpenFrontPanel(true, FPStateEnum.eVisible);
           

            vi.SetControlValue("Variant", m_myRef)

 

I've attached a screenshot of the relevant part of my VI

 

Thanks and regards

Mike

0 Kudos
Message 1 of 11
(5,682 Views)
I've also tried casing m_myRef to type Object but I still get the same type mismatch.
0 Kudos
Message 2 of 11
(5,660 Views)

A refnum created in one application environment is not the same as a refnum created in a different application environment. For example, a file refnum created in C is in no way the same as a file refnum created in LabVIEW, even though they're both file refnums.

 

What exactly are you trying to do in the LabVIEW side? Why not just call the .NET assembly from LabVIEW?

0 Kudos
Message 3 of 11
(5,634 Views)
I don't have experience doing exactly what you're doing, but if I had to guess, I would guess the problem is that the LV code is running inside its own memory space, where the reference you opened earlier is meaningless. As suggested, opening the reference inside the LV code should probably help.

___________________
Try to take over the world!
0 Kudos
Message 4 of 11
(5,623 Views)

If you tell LabVIEW that you pass it a variant you better make sure you create a correct variant from your reference. A variant is a very specific datatype in Windows (that can wrap many different datatypes) it is not equivalent to any datatype including references but simply a wrapper around most possible datatypes. So you must make sure you wrap your reference accordingly. There still is a possible difficulty since LabVIEW variants are not exactly the same as Windows variants. Most internal variant functions can deal with them both but I'm not sure that assigning a Windows variant to a LabVIEW variant exactly would work.

 

Also you seem to want to use the external reference as an object reference but that is likely  not gonna fly as easily. LabVIEW refnums are not the external reference directly but a specific LabVIEW datatype that again wraps the native reference such as ActiveX or .Net references, but also network connections, instrument communication resources, etc.

Message Edited by rolfk on 04-25-2010 02:31 PM
Rolf Kalbermatter
My Blog
0 Kudos
Message 5 of 11
(5,618 Views)
I need to call a specific instance of the C# object and I need to pass a reference to that instance in to Labview. So I need to pass the reference to that instance in to LabView
0 Kudos
Message 6 of 11
(5,582 Views)
Compile the C# program as a .dll and call it directly?
G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 7 of 11
(5,572 Views)

I can compile my C# to a dll and call it directly with no problem. The problem is that I need to work with a specific object instance in LabView and I can't seem to pass a reference to this object from C# to LabView.

I've included some code that hopefully illustrates what I'm trying to do.

 

 

In the included C# Program I have a C# console application (ConsoleApp) and a very simple C# class (SimpleClass).

In the attached LabView VI OpenCSharpFromLabView I use the .NET Constructor object to create an instance of SimpleClass and call a function on it, that works great.

 

Now what I can't figure out how to do....

In my C# Console application  I create an instance of SimpleClass (myClassA) and I call some some properties/methods on this instance...now I need to pass this instance (myClassA) to a LabView VI. In PassRefFromCSharpToLabView I've tried do this by adding a Variant control and trying to cast that to my .NET Type (SimpleClass). But I can't figure out how to do it. The code in this VI does not work, I get an error that  the To .NET Object VI can't convert a variant, I also tried to use the Variant To Data object but I get a type mismatch error.

So I'm trying to figure out how to pass a reference to a specific instance of an object into LabView and work with that specific instance.

Thanks

Mike

0 Kudos
Message 8 of 11
(5,555 Views)

Yamaeda wrote:
Compile the C# program as a .dll and call it directly?

I would rather create a .Net assembly exporting an entire interface including the right object types.

Rolf Kalbermatter
My Blog
0 Kudos
Message 9 of 11
(5,544 Views)

I came across the same situation. I need to pass the reference of .Net Object (Instance) to LabVIEW VI as parameter.

But seems that we cann't pass the .Net Object reference to labVIEW. Smiley Surprised

Anybody found the  work arround for passing .Net object reference to and fro from LabVIEW.

Hemant

0 Kudos
Message 10 of 11
(5,051 Views)