LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I convert a C# executable to a LabVIEW executable

Solved!
Go to solution

I have a program that was created in C# by an engineer who has left the company.  C# is not widely used among my company, but LabVIEW is, so I would like to convert the C# program to a LabVIEW program somehow.  I've worked with C language before, but not .NET, Java or C#.  I've downloaded Visual Studios C# and I can compile the program, edit the button sizes of the executable window, etc.  I am starting to learn a little bit about C#, but I'm starting from scratch.

 

I am however quite familiar with LabVIEW programming and have been using it since version 6.1.  I heard that there is an easy way to access a C# program via LabVIEW, but I haven't had any luck finding out how it is done.  In the past I have created DLLs with a C program and accessed the DLL in LabVIEW, so I might be able to do this with a DLL that I created in C#, but I do not know how to create a DLL in C#.

 

Could anyone please help me with figuring out how to compile a DLL with C# code in Visual Studios 2008 so I can use it in LabVIEW?  I've searched online and I always find something that asks me to enter a line in DOS command prompt starting with "CSC," which is not a recognized command.  I was hoping for something simpler and more user friendly.  Otherwise, I would like to know of any easier shortcuts for accessing my C# program via LabVIEW.

 

Thank you in advance for anyone who is able to help me or at least shed some light on my situation.

0 Kudos
Message 1 of 9
(7,565 Views)

C# creates .NET assemblies, not old-fashioned DLLs. There's also a difference between a .NET application and a .NET library (aka old-style DLL). Using a .NET library in LabVIEW is similar to using a DLL in that you use the .NET functions that are available in LabVIEW. There are examples that ship with LabVIEW that show how this is done. A C# application is really no different than any other application, and so the question boils down to: how do I control an application from LabVIEW? The answer is that it depends on whether the application exposes an interface to allow it to be programmed. If it does, then you can actually use the same .NET functions to control the application. If not, then you have to resort to automation via a third-party tool that basically allows you to simulate pressing buttons and entering stuff on an application's front panel. In the past I have recommended using AutoIt to perform this kind of automation.

 

Your secondary question is how to compile a DLL with C#. There is no need to resort to the command line to do this. All you need to do is to create a .NET library project and build the solution. Yes, I know that's a bit of an oversimplification, but this isn't really the place to be teaching C#, after all. I found the MSDN documentation to be quite thorough and so I would recommend looking at that as far as this part is concerned. If you need more help, just ask. Smiley Wink

Message 2 of 9
(7,532 Views)

smercurio_fc, thank you for your reply. 🙂

 

I have a few follow up questions regarding your response.  When I create a .NET assembly and compile the program, where is the assembly located afterward?  Is it the same as the executable found in the bin, obj or properties folders?  e.g. my namespace is called MC3 and I generate a file called MC3.exe, is this what I use to access the program?

 

I did some messing around in LV8.5 with the .NET stuff.  I read some of the help topics and looked at some of the examples.  It looks pretty strait forward when dealing with libraries that are build into the LabVIEW .NET libraries, but I'm not getting very far when I try to access my custom libraries.  Whenever I open a .NET constructor node in LV8.5 I try to click browse and navigate to my program.  I click on MC3.exe (my custom executable) and it gives me some objects, but no constructors.  When I pick one of the objects and click 'OK' nothing happens and my constructor node still has no reference.  I tried changing everything from 'private' to 'public,' thinking that it would allow me access to more objects and constructors, but still no changes.

 

Inside the program (written in C#) I have a user interface (copy of screen is attached) with several pieces from the System.Windows.Forms reference such as Labels, Buttons, TextBoxes and CheckBoxes. Basically, the concept of the application is to set some values by filling strings and setting the boolean arguements and executing.  I would like to create a LabVIEW application that has a front panel that correlates to this executable application.  Here is an example of a small piece:

 
    I have a button on my application called "Read MC," which is found in my MC3main.cs file as a method, like so:

 

           public void btnReadMC_Click(object sender, EventArgs e)
          {
              rbReadMC.Checked = true;
              btnSendCommand_Click(sender, null);
           }    

 

   and it is also found in my MC3main.Designer.cs file like so:

 

           public System.Windows.Forms.Button btnReadMC;

 

   I would like to be able to click on a button on a LV front panel, which I also call "Read MC" that does the same as it does inside the C# application.  The reason I want to do this is because I am more comfortable with LabVIEW, and it allows me to create an automated program that will utilize the options inside my C# application.  Right now, in order to use the application to complete an action, one has to click several checkboxes and then an execute button.  I would like to have it all more automated, so that just 1 button can cycle through everything needed to complete an action.

 

I know it's a lot involving .NET and C# sharp stuff, but I still feel that there is a way to access this application easily through LabVIEW.  Any help or suggestions is appreciated.

0 Kudos
Message 3 of 9
(7,496 Views)

I can't really say why you're not able to see the contructors for your form without looking at your C# code. As for where the assembly is located, it would be in either the bin\Debug or bin\Release folder depending on which method you used to compile it. If you pressed F5 then that creates a Debug version.

 

To answer the broader question of how to programmatically click the button you essentially need to call the btnClick method. This method is a member of the form, and you can use the Reflection namespace to access the method. I actually tried to call the button's PerformClick() method (after getting the control and casting it as a System.Windows.Forms.Button), but that didn't seem to work. I didn't get an error, it just didn't seem to do anything. Even when I tried it from another C# app. Attached is a simple C# application and the corresponding LabVIEW VI that will programmatically click the button (using the System.Reflection namespace) and then read the value of the textbox that is on the form.

 

To be honest, I'm not sure if this is really better than just using something like AutoIt, but that's your decision to make.

Message 4 of 9
(7,463 Views)

Smercurio_fc, once again thank you for your reply.  This sample VI that you sent me has helped me to understand a lot.  It looks like I have a great deal to learn about all of the methods available in .NET.

 

I have made some changes and I am now successful when I use the 'constuctor node' and 'invoke node' to access the methods inside my program.  I was able to create a DLL by using the Class Library template instead of the Windows Forms template.  I am having some questions about what to do after that.  Your example was great in showing how to deal with the text boxes, but how about changing values and bool logic?  Here is an example of what I'm talking about:

 

    I have a method that I have opened in LabVIEW called btnBroadcast_Click , here is the method:

 

    public void btnBroadcast_Click(object sender, EventArgs e)       

        {
            if (rbMlog.Checked) tbxTargetID.Text = "0xFFFFFF";
            else tbxTargetID.Text = "0xFFFFFFFF";
        } 

 

     Inside we see the method checking whether 'rbMlog.Checked' is true, and if so, it changes the value of tbxTargetID.Text.  How would I be able to make sure 'rbMlog.Checked' is true, or otherwise, how would I be able to access 'tbxTargetID.Text' to change it to a value of "0xFFFFFF" so I wouldn't even have to call the method?

 

Another question I have is dealing with objects that I pass into the method.  I've got another method called btnSetLS1_Click, which checks if the 'sender' is equal to btnSetLS1 or btnSetLS2.  I opened up the method in LabVIEW with a constructor and invoke node andI had 2 parameters, 'sender' and 'e.'  I tried to wire a string "btnSetLS1" to the 'sender' parameter, but the string won't wire to the .NET refnum...this is understandable.  I tried to convert the string using a 'To .NET Object.VI' and feed it into the parameter, but I didn't get the results I was expecting.  Here is some of that code:

 

       public void btnSetLS1_Click(object sender, EventArgs e)
        {
            // Determine whether LS1 or LS2 button was pressed
            System.Windows.Forms.TextBox tbx = new System.Windows.Forms.TextBox();
            if (sender == btnSetLS1) tbx = txtPing1;
            else if (sender == btnSetLS2) tbx = txtPing2;

        } 

 

 Should I be feeding some other type into the 'To .NET Object.VI' in order to set 'sender' to be "btnSetLS1?"  Thanks

0 Kudos
Message 5 of 9
(7,437 Views)
Oh, one more thing...I installed AutoIt on to my system, but it looks like just another scripting platform.  How would I go about using AutoIt to access my C# program without starting from scratch?  The SciTE script editor doesn't seem to recognize the .NET syntax.
0 Kudos
Message 6 of 9
(7,436 Views)
Solution
Accepted by topic author Dbutz

I don't quite understand why you're making a .NET library instead of a .NET app. I thought the original objective was to deal with a C# executable. A .NET library is, well, a library, not an executable. As such, it doesn't (normally) have forms, or user controls, so there would be controls and event-handling functions for controls like button clicks.

 

As for your other questions: Setting of controls would be done in the same way as the code that reads the textbox. Just change the Property node to a write instead of a read. As for setting the "sender" parameter, in my example I simply created a generic, empty sender. If you want to set this to specific control you would need to get a reference to the control (as I did with the TextBox, and use this instead as the first parameter of the Build Array).

 

As for AutoIt: This is not intended to be used to access .NET. The point of this is to completely ignore .NET and simply focus on the application once it's running. It uses low-level Windows API functions to simulate clicking buttons, setting textboxes, reading textboxes, etc. You can either write a script and call it from LabVIEW or you can use AutoIt's ActiveX interface to program the code in LabVIEW. For example, using the example I had posted, a script that  launches the application, clicks the button, and gets the value of the textbox would look like this:

; Launch application and wait for window to become active
Run("LabVIEW Test\bin\Debug\LabVIEW Test.exe")
WinWaitActive("Form1", "", 5)
; Click the "Generate" button
ControlClick("Form1", "", "[CLASS:WindowsForms10.BUTTON.app.0.378734a; INSTANCE:2]")
; Get the text from the textbox
$var = ControlGetText("Form1", "", "[CLASS:WindowsForms10.EDIT.app.0.378734a; INSTANCE:2]")
MsgBox(0, "TextBox Contents", $var)

 The "[CLASS..." stuff refers to the ID of the controls to access which are acquired using the tool AU3Info that comes with AutoIt. (In the documentation it indicates there are several ways to specify the ID. The "CLASS" method is just one of them.) The same thing in LabVIEW would look like this: 

 

 

Note that this uses ActiveX since that's the interface that AutoIt exposes.  This to me seems a lot easier than the .NET stuff. 

 

Message Edited by smercurio_fc on 01-10-2009 12:02 PM
Download All
Message 7 of 9
(7,409 Views)
Thank you so much Smercurio_fc, you have helped me find what I am looking for!  I've finally got the hang of using the AutoIt program, what an easy tool!  Once I learned about the AU3info.exe and applied it the way you did in your example, I was able to get an automated executable working in no time.  Accessing it through LabVIEW and adding controls is pretty easy as well.  I didn't even have to get my hands dirty messing with the .NET code.  Thanks again for a stellar service in helping me with my question
0 Kudos
Message 8 of 9
(7,344 Views)
Glad that solved your problem. One note: When using the ActiveX methods for AutoIt, be sure to pass in empty strings as I show in the example if you don't need to specify the parameter. While the VI may not show as broken if you don't connect anything to that parameter, the method call will not work if you don't set the value to something. This does not apply to numeric parameters that are not used.
Message 9 of 9
(7,324 Views)