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.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Calling a LV8.2 created DLL in VB.net

I would like to obtain some assistance (perhaps an example) with the proper way to build a DLL in LV8.2 that has string outputs and then how to call that DLL in VB.net.
I have searched and read just about all of the many associated posts on this board and in the final analysis I can not get my VB.net application to display the string data.  There was an article available in the NI Developer Zone by the title "Using Microsoft Visual Basic to Call LabVIEW DLLs That Pass String Data Types".  When I found this article I felt that I had finally obtained the document that was going to clear up the issue for me and I would be able to move forward.  When I looked carefully at the article I found that the LV DLL had been created in 6.0 and the function prototype was:   void __stdcall Basicstring(CStr Stringin, CStr Stringout, int32 len2);
In my Project Explorer I don't see the mechanism for getting the type "CStr".  I can get "char", "LStrHandle" and "PStr" ???
I was able to rebuild the example VB code in my Visual Studio.net 2003 and get the demo application to run, but I have been unable to get my application to run...but as mentioned, I can't build the function prototype with "CStr".
In my VB.net I declared my function using the following:

Private

Declare Sub Getsw Lib "..\Labview\DemoSwCtrl.dll" Alias "Getsw" (ByVal swin As String, ByVal swipa As String, ByRef ecode As Int32, ByVal errmsg As Char, ByVal swout As Char, ByVal lenerr As Int32, ByVal lenver As Int32)

And I call the function with the following:

Dim ipa As String = TxtBxIpa.Text
Dim errcode As Int32
Dim lerr As Int32 = 100
Dim lout As Int32 = 100
Dim errtxt As String
errtxt = Space(100)
Dim outtxt As String
outtxt = Space(100)
Dim intxt As String = TxtBxStrIn.Text

Getsw(intxt, ipa, errcode, errtxt, outtxt, lerr, lout)
TxtBxErrCode.Text = errcode.ToString
TxtBxStrOut.Text = outtxt
TxtBxErrMsg.Text = errtxt

In the above code the errtxt = Space(100) and outtxt = Space(100) result in an error being generated when I run my vb executable.  The error is:

"An unhandled exception of type 'System.NullReferenceException' occurred in DLL Demo.exe  Additional information: Object reference not set to an instance of an object."

If I comment out these two lines the vb code runs without error and returns the proper numeric errcode, but the string indicators for the returned values in errtxt and outtxt are empty.

I have developed my DLL using "char", "PStr" and "LStrHandle" and can't get any of these types to work.

The DLL works fine when called in labview (using "char").  What am I doing wrong in VB.net?

There are so many variables and so many permutations that can be tried using all the variables.....

Ed

Ed
0 Kudos
Message 1 of 9
(4,389 Views)
I am currently building an example and will post it when I am finished.
Daniel Eaton
National Instruments
Systems Engineering
Embedded and Industrial Control
0 Kudos
Message 2 of 9
(4,367 Views)
Hey, here is one way to do this.  You basically initialize the variable first.  The LabVIEW DLL is simply a pass through.  The program just writes to the DLL and should read the same thing back.
Daniel Eaton
National Instruments
Systems Engineering
Embedded and Industrial Control
0 Kudos
Message 3 of 9
(4,338 Views)

Here is another way I made it work with StringBuilder.  This is the same project as the other one, just modified to use StringBuilder.  I would not recommend having both on your computer at the same time.

Let me know if this helps!

Daniel Eaton
National Instruments
Systems Engineering
Embedded and Industrial Control
0 Kudos
Message 4 of 9
(4,336 Views)
Hi Dan,
 
Many thanks for your time in providing the example support.  One thing that I would be interested in would be the LabView created Function Prototype that you used as there was some confusion regarding exactly what this should look like based on a considerable amount of research done going through the postings here in this forum.
 
Another point (although a relatively small one) is that I am running VS.net 2003 and apparently you compiled in VS.net 2005 so I couldn't run your example.  However a review of your "Form1.vb" was adequate to see the approach you were taking to access the DLL.
 
My LabView created Function Prototype that I finally ended up working with is the following:
void Getsw(char StrInput[], char IPAddress[], long *ErrCode, char error[], char status[], long len, long len2)
 
 
Ed
0 Kudos
Message 5 of 9
(4,320 Views)
Here is the source code.  It is in LabVIEW 8.2. 
 
Let me know if helps.
 
Dan
Daniel Eaton
National Instruments
Systems Engineering
Embedded and Industrial Control
0 Kudos
Message 6 of 9
(4,307 Views)

how come all the vb.net to labview.dll samples/examples result in the same error and wont continue??

 

"Attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang."

 

trried a dozen of them now and all result in the same error? what am i missing?

Im on dotnet 2.0 with vs2005 by the way

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

Hi Matma,

 

Here's KnowledgeBase Article that deals with that exact issue:

 

KnowledgeBase 3ZJ8MC45: Error with LoaderLock Managed Debugging Assitant (MDA) when Calling a DLL.

 

This is an issue specific to .NET 2.0.

 

Hope this helps!

Tanya Visser
National Instruments
LabVIEW Group Manager
0 Kudos
Message 8 of 9
(3,597 Views)

Hi Daniel,

 

Thank you so much for providing this example.  It is complete, thorough and clear.  

This gives me better understanding how the string work in Labview.

 

Have a great day,

Mie Mie

0 Kudos
Message 9 of 9
(2,914 Views)