LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Passing parameters to Labview DLL from VB.NET

I have tasks to call various LabView DLLs from my VB.NET application.

I found  the LVCalculator.dll example on NI website to call Labview DLL from VB.net. It shows how to declare the DLL and call it from VB.net

Declare Auto Function Calculator Lib "..\LabVIEW\LVCalculator.dll" (ByVal val1 As Double, ByVal val2 As Double, ByVal Op As Integer) As Double

Result.Text = Calculator(System.Double.Parse(Operand1.Text), System.Double.Parse(Operand2.Text), add)

 

This example works fine. Unfortunately, all examples I found have parameters with standard data type such as Integer, Double or String.However in my DLL I have a parameters  LVRefNum *VISAIN, *VISAOUT.

 

void __cdecl LCDColor(LVRefNum *VISAIN, uint16_t LCDColor2,

            TD1 *errorInNoError, LVRefNum *VISAOUT, char OutputString[], TD1 *errorOut,

            int32_t len);

 These parameters in my DLL are a Com Port numbers, which Labview VI uses to communicate with the controlled hardware. When this VI runs as standalone program with Labview GUI the COM port is selected from the menu. But when I call this VI as a DLL, I need to pass my COM port numbers via the parameter LVRefNum *VISAIN and *VISAOUT.  And I could not figure out what data type and format to use to make  DLL understand it.   

0 Kudos
Message 1 of 21
(4,875 Views)

Usually LVRefNum are U32 values. But please note that those numbers are generated by LabVIEW within the same instance - they are not from VB.NET.

 

(By the way, you can also use strings to specify comports in VISA in LabVIEW. But if your vi uses RefNum then you'd have to pass U32 value from VB.NET)

 

-DP

--------------------------------------------------------

New Controls & Indicators made using vector graphics & animations? Click below for Pebbles UI


0 Kudos
Message 2 of 21
(4,869 Views)

Thanks for a quick response. It's still not clear to me. If I need to tell the DLL using Com Port 1, what shoul be in my calling variable that DLL understands: 1 or COM1 or "COM1" or "1" or anything else ?

 

Thanks

0 Kudos
Message 3 of 21
(4,862 Views)

Well, you could modify your LabVIEW code so that instead of calling your subvi using reference#, you could call it using VISA Reference Name (in which case you can pass a string value instead of a reference name).

 

You'd have to post your LabVIEW code (which you're compiling into a DLL) for me to be able to tell how you would be able to call them from VB.NET... I'd have to see how you generate reference# in LabVIEW (if you generate them.)

 

-DP

--------------------------------------------------------

New Controls & Indicators made using vector graphics & animations? Click below for Pebbles UI


0 Kudos
Message 4 of 21
(4,849 Views)

I did not create this DLL.  I only get a header file from Labiew project with calling parameters knowing that this is a variable for a Com Port.  I have attached DLL file.

 

Thanks.

0 Kudos
Message 5 of 21
(4,830 Views)

I still have not solved this problem. This is how the header file looks like:

 

#include "extcode.h"
#pragma pack(push)
#pragma pack(1)

#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
 LVBoolean status;
 int32_t code;
 LStrHandle source;
 } TD1;


void __cdecl LCDColor(LVRefNum *VISAIN, uint16_t LCDColor2,  TD1 *errorInNoError, LVRefNum *VISAOUT, char OutputString[], TD1 *errorOut, int32_t len);

 

long __cdecl LVDLLStatus(char *errStr, int errStrLen, void *module);

#ifdef __cplusplus
} // extern "C"
#endif

#pragma pack(pop)

 

Also ffrom this DLL developer I learned that inside Labview code it uses name "COM1" as a string for the Com Port under VISAIN and VISAOUT parametr, also that parameters errorIn,OutputStr, errorOut, len are optional.

 I have tried several ways to declare  and call it in VB.NET:

1.

DeclareAutoFunction LCDColor Lib"..\LabVIEWdlls\LCD Color.dll" (ByVal ComPortIn AsString, ByVal color As Int16, ByRef errIn As Int32, ByVal ComPortOut AsString, ByRef outPutStr AsString, ByRef errOut As Int32, ByRef len As Int32) AsInt32

 

Call  as

LCDColor("COM1", color, errorIn,"COM1", OutputStr, errorOut, len)    

 

2.

DeclareAutoFunction LCDColor Lib"..\LabVIEWdlls\LCD Color.dll" (ByVal ComPortIn As Int32, ByVal color As Int16, ByRef errIn As Int32, ByVal ComPortOut As Int32, ByRef outPutStr AsString, ByRef errOut As Int32, ByRef len As Int32) As Int32

call as

LCDColor(1, color, errorIn, 1, OutputStr, errorOut, len)

 

3.DeclareAutoFunction LCDColor Lib"..\LabVIEWdlls\LCD Color.dll" (ByVal ComPortIn As IntPtr, ByVal color As Int16, ByRef errIn As Int32, ByVal ComPortOut As IntPtr, ByRef outPutStr AsString, ByRef errOut As Int32, ByRef len As Int32) As Int32

call as

Dim ComPort AsString = "COM3"

Dim ptrCom As IntPtr = Marshal.StringToHGlobalAnsi(ComPort)

LCDColor(ptrCom, color, errorIn, ptrCom, OutputStr, errorOut, len)

 

In all cases I get the same error: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

I need to have it done.

 

Thanks for any suggestions.

0 Kudos
Message 6 of 21
(4,788 Views)

Ah I see what you're trying to do.

 

Rather than going deeper into where your problem lies, I quickly made a little project for you - see if this gives you an idea.  This is how we had developed a DLL file for a client who needed to use it in his VisualStudio (I think C#) project and as far as I know, it worked well for them.

 

Concept is that:

1. You're Opening VISA using LabVIEW VISA Open - and you're only passing a text string to tell it which comport to open (you can get this string name from MAX, but I believe you already have something in your list that looks correct).


2. Once VISA is opened using "VISA_Open()" call, you can do VISA_Read() and VISA_Write()  (In our organization we don't pass the whole error cluster back and forth between the DLL and calling non-LabVIEW application.  We simplify it by only using a code# which can be easily defined as a constant in a C-based or C# or VB.NET based application.  Sorry I forgot and started with C/C++ style nomenclature, but I'm sure its obvious how you'd call it from VB.NET)

 

3. At the end, close VISA reference by calling "VISA_Close()" subroutine in the DLL.

 

With above method, you have no need to pass VISA reference back and forth. Not only that, it also allows you for much simplified function calls as you don't need to call the whole error cluster/etc.  By the way, if any error is generated inside the calls, it does get returned since all calls follow "int ...." structure.

 

Hope that makes sense and helps...

 

-DP

 

(Hmm.. it is not letting me attach a zip file with the project - I'll try again)

--------------------------------------------------------

New Controls & Indicators made using vector graphics & animations? Click below for Pebbles UI


0 Kudos
Message 7 of 21
(4,783 Views)

Here are the files, it won't let me attach a zip...

--------------------------------------------------------

New Controls & Indicators made using vector graphics & animations? Click below for Pebbles UI


Download All
0 Kudos
Message 8 of 21
(4,776 Views)

And more files...

--------------------------------------------------------

New Controls & Indicators made using vector graphics & animations? Click below for Pebbles UI


Download All
0 Kudos
Message 9 of 21
(4,775 Views)

Thanks a lot. Have you attached the right project file? VISA_C_DLL.lvproj looks like HTML file, not a C project file. 

0 Kudos
Message 10 of 21
(4,754 Views)