LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Marshal.StringToHGlobalUni implementation in LabVIEW to call a stdcall(winapi) dll

Hello

I have a .dll file which i would like to call and use its functions . One of the parameter which I have to pass to this is an IP address

I am always getting an error on the return value ,which means I am not sending the parameters in the correct way.

 

The code in VB is something like this 

 

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices

 

Public Class Form1

Sub InitializeSendParams()

m_nSendType = 0

Dim strParams As String = "192.168.51.1"
m_pSendParams = Marshal.StringToHGlobalUni(strParams)

End Sub

 

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
InitializeSendParams()
End Sub

 

Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles switch_screen.Click
Dim pNULL As IntPtr = 0
Dim nErrorCode As Integer = 0
'// 5. Send to device
nErrorCode = CSDKExport.Cmd_SwitchProgram(m_nSendType, m_pSendParams, 2, 0)
If (nErrorCode <> 0) Then
nErrorCode = CSDKExport.Hd_GetSDKLastError()
End If

End Sub


End Class

 

The above VB functions access the dll and when clicked on switch_screen, the program on my LED display changes. 

I am trying to implement the same in LabVIEW by calling the dll by "call library function" 

But I am getting 13 in function return and when I see the help file its says "connecting device error"

I think this function "m_pSendParams = Marshal.StringToHGlobalUni(strParams)", I am unable to understand

I tried all available methods in Parameters section in call library function. but it still gives me the error 13

 

Can some one help me understand how to implement m_pSendParams = Marshal.StringToHGlobalUni(strParams) inLabVIEW?

 

Thanks

 

 

DEMO.png

0 Kudos
Message 1 of 6
(403 Views)

According to the documentation, the IP number string should be an UTF-16 string. I don't know if it should be big-endian or little-endian.

 

And ... correct the parameters.

Calling Convention C

Pass the pStrParam as byte array and don't forget the termination character (two-byte sequence 0x00 0x00)

pStrParam: Array, unsigned 8-bit integer, array data pointer

pDeviceGUID: Numeric, pointer-sized Integer (because you pass a Null Pointer)

Pass the pStrParam as byte array and don't forget the termination character (two-byte sequence 0x00 0x00)

pStrParam: Array, unsigned 8-bit integer, array data pointer

pDeviceGUID: Numeric, pointer-sized Integer (because you pass a Null Pointer)

 

 

0 Kudos
Message 2 of 6
(364 Views)

Hello Martin

I tried implementing the code as per your suggestion, but it still doesn't work

Also 

Change Calling convention "C" hangs LabVIEW execution

Also as per the header file it says   this "#define HDAPI _stdcall" in HDExport.h

Please check snippet and attached code

 

snippet_1.png

0 Kudos
Message 3 of 6
(335 Views)

The pStrParams parameter must be converted from ASCII to UTF-16LE. There are some Unicode packages on vipm.io that contain such a VI. This is Convert ASCII to UTF-16LE (Scalar).vi from NI Unicode tools:

Convert ASCII to UTF-16LE (Scalar).viConvert ASCII to UTF-16LE (Scalar).vi

Use the wire before conversion to string for the call library node.

The disabled case contains the "interleave with 0" method. Not sure if that works for all codepages.

 

Change the other parameters back to how they were before. The last parameter should be numeric, unsigned pointer sized integer, pass by value.

0 Kudos
Message 4 of 6
(318 Views)

Hello,

I am somehow not able to open your snippet. When I save it and drag to my block diagram, it opens as an image. 

Can you kindly post this vi

 

Thanks

 

0 Kudos
Message 5 of 6
(304 Views)

Hello

Thanks for the suggestions.

 

I tried the vipm option and by doing all kinds of combinations was still not able to get it to work.

Finally got a workaround, converted the vb.net code to vb.net dll and imported this dll through .net constructor in LabVIEW and finally got it to work..

 

It was really a difficult task as I had very less knowledge on VB. But thanks to chatgpt could get the .net dll  generated and got it working on LabVIEW..

 

Thanks all for the support 🙏 

0 Kudos
Message 6 of 6
(239 Views)