PlanetSquires Forums

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Asking for some help with a C dll  (Read 294 times)

Robert Eaton

  • FireFly3 Registered User
  • Junior FireFly Member
  • *
  • Posts: 125
Asking for some help with a C dll
« on: May 22, 2017, 11:27:00 PM »

Hi,
This isn’t a Firefly question, but I’m hoping some of the experts here can offer some advice on working with a C dll. The dll works with a turntable used to do polar plot measurements on speakers and microphones. I have one on loan for a few days here at work to evaluate before purchasing.
The turntable works over an Ethernet port and the software that comes with it works fine (but doesn’t do what I need). (The turntable was made in Italy.)
I’ve tried a number of things but the best I can do is get an error #56 back which means there was an error binding a socket to a port.
Unfortunately, this first call to make the network connection has a lot of variables, so it’s hard to know if there is a problem with one or all of the variables. I’m plugging in the same values as their program uses. (port, IP, etc.)

This is from the documentation:
Description:
The function opens the socket on the computer to communicate with the turntable, verifies the
presence or absence of the turntable, reads the description of the turntable and reads the network
setting of the turntable.

Parameters:
SOCKET* Socket: pointer to the SOCKET struct (you could also define a pointer to an array of 32 byte); this struct is the same for all functions where indicated.

struct sockaddr_in* sa: pointer to the sockaddr_in struct; you could also define a pointer to an array of 32 byte.

char ip_pc[]: IP address of the computer, formatted as “x.x.x.x”
char ip_machine[]: IP address of the turntable, formatted as “x.x.x.x”
unsigned short porta: port number to be used
char description[]: description of the machine obtained by dll gnet.dll; you need to set this
parameter to “Basert” in order to control the ET250-3D Outline
int *stato: variable for debugging

unsigned char vector[]: array of 23 byte which included:
- subnet mask (4 byte)
- default gateway (4 byte)
- mac address (6 byte)
- ip address (4 byte)
- ip broadcast (4 byte)

int *dhcp: pointer to a variable whose value is 1 if the network settings have the option DHCP client set, otherwise 0.

Returns:
return 0 if successful command
return 51 if there is an error in the function wsastartup
return 52 if there is an error in the creation of the socket
return 53 if there is an error in setting receive time-out of the socket
return 54 if there is an error in setting send time-out of the socket
return 55 in case of error in the setting of the blocking or nonblocking mode on socket
return 56 if there is an error in the binding a socket to a port
return 72 error description of the device in relation to the description passed as parameter
return 71 error in the length of the data received or error in content data


Here is my latest attempt…

Code: [Select]
   Declare Function apri_macchina Lib "Basert.dll" Alias "apri_macchina" ( _
                socket As Dword, _
                socket_addr_in As Dword, _
                Char_IP_PC As Dword, _
                Char_IP_OL As Dword, _
                Porta As Word, _
                Char_Desc As Dword, _
                Stato As Dword, _
                Char_Vector As Dword, _
                DHCP As Dword ) As Long

Code (put in a button click event)

Code: [Select]
Dim b?, x&

Dim stato_ptr As Long Ptr
Dim stato As Long     

Dim ip_PC As StringZ   * 10
Dim ip_PC_ptr As StringZ Ptr

Dim ip_Mach_ptr As StringZ Ptr
Dim ip_Mach As StringZ * 10

Dim porta As Word   

Dim desc As StringZ * 10
Dim desc_ptr As StringZ Ptr

Dim vector_ptr As Byte Ptr
Dim vector(1 To 23) As Byte
vector_ptr = VarPtr(vector(0))

Dim dhcp_ptr As Long Ptr
Dim dhcp As Long

Dim Result As Long

Dim socketA_ptr As Byte Ptr   
Dim socketA(32) As Byte
socketA_ptr = VarPtr(socketA(0))

'Dim socketB_ptr As Byte Ptr   ' not used here
'Dim socketB(32) As Byte   
'socketB_ptr = VarPtr(socketB(0))
 
Dim ssA_ptr As Byte Ptr
Dim saA(32) As Byte
ssA_ptr = VarPtr(saA(0))

stato_ptr = VarPtr(stato)

'Dim saB(32) As Byte     ' not used here
'Dim saB(32) As Byte
'ssB_ptr = VarPtr(saA(0))

ip_PC = "192.168.1.1"
ip_Mach = "192.168.1.34"

ip_PC_ptr = VarPtr(ip_PC)
ip_Mach_ptr = VarPtr(ip_Mach)

desc = "Basert"
desc_ptr = VarPtr(desc)
 
porta = 6668

dhcp = 1
dhcp_ptr = VarPtr(dhcp)

Data 255, 255, 255, 0, 192, 168, 1, 250, &h12, &h34, &h45, &h78, &h9A, &hCD, 192, 168, 1, 1, 192, 168, 1, 34

For x = 1 To 23
    b? = Val(Read$(x))
    vector(x)  = b?
Next

result = apri_macchina (socketA_ptr, _
                        ssA_ptr, _
                        ip_PC_ptr, _
                        ip_Mach_ptr, _
                        porta, _
                        desc_ptr, _
                        stato_ptr, _
                        vector_ptr, _
                        dhcp_ptr)

? Str$(result)

Thanks for any help or suggestions!!
Bob
Logged

José Roca

  • FireFly3 Registered User
  • Master FireFly Member
  • *
  • Posts: 2785
    • José Roca Software
Re: Asking for some help with a C dll
« Reply #1 on: May 23, 2017, 12:22:46 AM »

Everything looks wrong and confusing (with so many xxx_ptr). Sorry.

This will be closer:

Code: [Select]
   Declare Function apri_macchina CDECL Lib "Basert.dll" Alias "apri_macchina" ( _
                BYREF socket As Byte, _
                BYREF socket_addr_in As BYTE, _
                BYREF Char_IP_PC As ASCIIZ, _
                BYREF Char_IP_OL As ASCIIZ, _
                BYVAL Porta As Word, _
                BYREF Char_Desc As ASCIIZ, _
                BYREF Stato As Long, _
                BYREF Char_Vector As Byte, _
                BYREF DHCP As Long ) As Long

Dim b?, x&

Dim stato As Long     
Dim Result As Long
Dim socketA(1 TO 32) As Byte
Dim saA(1 TO 32) As Byte

Dim ip_PC As StringZ * 12
ip_PC = "192.168.1.1"
Dim ip_Mach As StringZ * 12
ip_Mach = "192.168.1.34"

Dim desc As StringZ * 10
desc = "Basert"
 
Dim porta As Word   
porta = 6668

Dim dhcp As Long
dhcp = 1

Data 255, 255, 255, 0, 192, 168, 1, 250, &h12, &h34, &h45, &h78, &h9A, &hCD, 192, 168, 1, 1, 192, 168, 1, 34

Dim vector(1 To 23) As Byte
' // 23 (?) - you're filling 22
For x = 1 To 23
    b? = Val(Read$(x))
    vector(x)  = b?
Next

result = apri_macchina (socketA(1), _
                        saA(1), _
                        ip_PC, _
                        ip_Mach, _
                        porta, _
                        desc, _
                        stato, _
                        vector(1), _
                        dhcp)

? Str$(result)

If it is a C dll, it probably will use the CDECL convention, but you should check it in the documentation.
« Last Edit: May 23, 2017, 12:25:07 AM by José Roca »
Logged

Robert Eaton

  • FireFly3 Registered User
  • Junior FireFly Member
  • *
  • Posts: 125
Re: Asking for some help with a C dll
« Reply #2 on: May 23, 2017, 01:28:30 PM »

Thanks so much Jose for your input and clarity!
Knowing that using byref should be all I need to forward a pointer is a big help. (And much simpler.)
They did mention in an email that the dll was written in C.

Unfortunately it still doesn't work but it returns a different error (#71).

The documentation is confusing  (at least to me).
It originally was used with a second dll (gnet.dll). but they told me it was not longer needed because you easily find the IP on the turntable display.
The example C code (in another section of the doc) doesn't put any data in the  "vector" array but it seems like it would be needed so I added it.
(But why send the same IP addresses in two different variables?)

You can also control the turntable with raw UDP packets and they sent some info on that. I've never done that before either but I am going to give it a try.

Thanks,
Bob
Logged

José Roca

  • FireFly3 Registered User
  • Master FireFly Member
  • *
  • Posts: 2785
    • José Roca Software
Re: Asking for some help with a C dll
« Reply #3 on: May 23, 2017, 01:47:09 PM »

This one should be at least 13

Dim ip_Mach As StringZ * 13

In general, I would use 260, e.g. Dim ip_Mach As StringZ * 260, instead of having to count characters.

José Roca

  • FireFly3 Registered User
  • Master FireFly Member
  • *
  • Posts: 2785
    • José Roca Software
Re: Asking for some help with a C dll
« Reply #4 on: May 23, 2017, 01:55:18 PM »

> Knowing that using byref should be all I need to forward a pointer is a big help. (And much simpler.)

Passing a variable by reference is the same that passing a pointer to it by value.

Also be careful when translating a declare, because you haven't used neither BYVAL nor BYREF, and, in BASIC te default is BYREF, so you were passing all the values by reference. Therefore, when you passed ip_PC_ptr using your declare, you weren't passing a pointer to the ip_PC variable, but a pointer to the ip_PC_ptr variable. In your declare, you should have declared all the parameters BYVAL (which is the default for C).