1

I work with a relay module that I normaly connect via USB. That all works perfectly. Now I would like to connect it via the network. All manufacturer's VB.NET code works in vba except for accessing this module over the network.

Public Declare PtrSafe Function DapiOpenModuleEx Lib "DELIB64" (ByVal moduleID As Long, ByVal nr As Long, ByRef exbuffer As DAPI_OPENMODULEEX_STRUCT, ByVal open_options As Long) As Long


' Definitions for DapiOpenEx

Public Type DAPI_OPENMODULEEX_STRUCT
    address(255) As Byte
    timeout As Long
    portno As Long
    encryption_type As Long
    encryption_password(31) As Byte
End Type


'Open ETH-Module with parameter
    Dim handle as Ulong
    Dim MyModuleID As UInt32
    MyModuleID = 42

    Dim open_buffer As New DELib64.DAPI_OPENMODULEEX_STRUCT

    open_buffer.address = System.String.Copy(192.168.1.1 As String) As String 
    open_buffer.portno = 0

    handle = DELib.DapiOpenModuleEx(MyModuleID, 0, open_buffer)

I am getting an error "open_buffer.address = System.String.Copy(192.168.1.1 As String) As String "

Can someone help me with what i need to change here?

    Dim handle as LongLong 
    Dim MyModuleID As Long
    MyModuleID = 42


    Dim open_buffer As Delib64.DAPI_OPENMODULEEX_STRUCT
    open_buffer.address = system.String.Copy("192.168.1.1" AS String) As String
    open_buffer.portno = 0
    
    handle1 = DapiOpenModuleEx(MyModuleID, 0, open_buffer, 0)
7
  • 1
    VB.NET is a completely different language than VBA (albeit with similar syntax). However, open_buffer.address = System.String.Copy(192.168.1.1 As String) As String is not valid VB.NET either. What is the original, unmodified line in the manufacturer's sample code? Commented Nov 30, 2022 at 8:04
  • The answer here would seem to suggest that all you need to do is open_buffer.address = "192.1686.1.1". Have you tried that? Commented Nov 30, 2022 at 8:10
  • The Code from the manual: // Open ETH-Module with parameter DAPI_OPENMODULEEX_STRUCT open_buffer; strcpy((char*) open_buffer.address, "192.168.1.10"); open_buffer.portno = 0; open_buffer.timeout = 5000; handle = DapiOpenModuleEx(RO_ETH, 0, (unsigned char*) &open_buffer, 0); printf("Module handle = %x\n", handle); Commented Nov 30, 2022 at 9:54
  • I tried that. Then I get the error Can't assign to an array Commented Nov 30, 2022 at 10:02
  • deditec.de/media/manuals/en/manual_delib/… Commented Dec 1, 2022 at 14:14

1 Answer 1

2

According to your comment, the original line of code is

strcpy((char*) open_buffer.address, "192.168.1.10");

So you need to copy the ASCII (single-byte) string "192.168.1.10" into a VBA byte array. This is surprisingly hard, since the obvious approach of open_buffer.address = StrConv("192.168.1.10", vbFromUnicode) won't work (you can't assign to a fixed-size array that's part of a type).

One obvious solution would be to make a Windows API call to CopyMemory, but if we want a VBA-only solution, a simple loop should suffice:

Dim i As Long
Dim b() As Byte
b = StrConv("192.168.1.10", vbFromUnicode)

For i = 0 To UBound(b)
    open_buffer.address(i) = b(i)
Next
open_buffer.address(UBound(b) + 1) = 0     ' C-strings need to be 0-terminated

(I do have the feeling that this should be easier, so I'll gladly upvote competing, simpler answers.)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply. I no longer get an error. But cannot connect to the module
Your code works perfectly. I forgot to put the open_buffer.timeout = 5000. Thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.