Post by rogercabo on Mar 25, 2023 19:25:23 GMT 1
Hi everyone,
today I tried to build a program to send and receive files from a SFTP webserver. (same easy as FTP but port 22)
I build the whole code step by step. I didn't expect to get the following error.

today I tried to build a program to send and receive files from a SFTP webserver. (same easy as FTP but port 22)
I build the whole code step by step. I didn't expect to get the following error.

I downloaded the 32bit libssh2.dll from the secure website de.dll-files.com/libssh2.dll.html
And and copy it into the soucecode folder, CurDir() show the correct path where libssh2.dll is located.
Does the error mean:
Gb32 does not find the function inside of the dll? or
It doesn't find the DLL?
Any idea what this error cause?
Does the error mean:
Gb32 does not find the function inside of the dll? or
It doesn't find the DLL?
Any idea what this error cause?

$Library "gfawinx"
$Library "UpdateRT"
UpdateRuntime ' Patches GfaWin23.Ocx
Const LIBSSH2_FXF_WRITE = 2
Const LIBSSH2_FXF_CREAT = 8
Const LIBSSH2_FXF_TRUNC = 16
' Zugriffsrechte für die erstellte Datei
Const LIBSSH2_SFTP_S_IRUSR = 256
Const LIBSSH2_SFTP_S_IWUSR = 128
Const LIBSSH2_SFTP_S_IRGRP = 32
Const LIBSSH2_SFTP_S_IROTH = 4
' Erforderliche DLL-Aufrufe
Declare Function libssh2_init Lib "libssh2.dll" () As Integer
Declare Function libssh2_exit Lib "libssh2.dll" () As Integer
Declare Function libssh2_session_init_ex Lib "libssh2.dll" (ByVal alloc_func As Long, ByVal free_func As Long, ByVal realloc_func As Long, ByVal context As Long) As Long
Declare Function libssh2_session_free Lib "libssh2.dll" (ByVal session As Long) As Integer
Declare Function libssh2_session_disconnect Lib "libssh2.dll" (ByVal session As Long, ByVal description As String) As Integer
Declare Function libssh2_session_set_blocking Lib "libssh2.dll" (ByVal session As Long, ByVal blocking As Integer) As Integer
Declare Function libssh2_session_handshake Lib "libssh2.dll" (ByVal session As Long, ByVal socket As Long) As Integer
Declare Function libssh2_userauth_password Lib "libssh2.dll" (ByVal session As Long, ByVal username As String, ByVal password As String) As Integer
Declare Function libssh2_sftp_init Lib "libssh2.dll" (ByVal session As Long) As Long
Declare Function libssh2_sftp_shutdown Lib "libssh2.dll" (ByVal sftp As Long) As Integer
Declare Function libssh2_sftp_open Lib "libssh2.dll" (ByVal sftp As Long, ByVal filename As String, ByVal flags As Long, ByVal mode As Long) As Long
Declare Function libssh2_sftp_close Lib "libssh2.dll" (ByVal handle As Long) As Integer
Declare Function libssh2_sftp_write Lib "libssh2.dll" (ByVal handle As Long, ByVal buffer As String, ByVal count As Long) As Long
' Beispielcode für die Verwendung der libssh2.dll
Dim session As Long
Dim sftp As Long
Dim sock As Long
Dim username As String
Dim password As String
Dim remotePath As String
Dim localPath As String
Dim remoteHandle As Long
Dim localHandle As Integer
Dim buffer1 As String
Dim bytesRead As Long
' Initialisieren der libssh2.dll
libssh2_init() // <------------------- ERROR
' Initialisieren der Session
session = libssh2_session_init_ex(0, 0, 0, 0)
' Verbindung zum Server herstellen. Port 22
sock = ConnectToServer("myserver.com", 22)
' Handshake durchführen
libssh2_session_handshake(session, sock)
' Authentifizieren mit Benutzername und Passwort
username = "meinbenutzername"
password = "meinpasswort"
libssh2_userauth_password(session, username, password)
' SFTP-Session initialisieren
sftp = libssh2_sftp_init(session)
' Datei auf dem Server öffnen
remotePath = "/pfad/auf/datei.txt"
localPath = "C:\mein\lokales\verzeichnis\datei.txt"
remoteHandle = libssh2_sftp_open(sftp, remotePath, LIBSSH2_FXF_WRITE Or LIBSSH2_FXF_CREAT Or LIBSSH2_FXF_TRUNC, LIBSSH2_SFTP_S_IRUSR Or LIBSSH2_SFTP_S_IWUSR Or LIBSSH2_SFTP_S_IRGRP Or LIBSSH2_SFTP_S_IROTH)
' Datei auf dem Server schreiben
localHandle = FreeFile
Open localPath for Binary Access Read As # localHandle
buffer1 = String(1024, #0)
Do While Not EOF(# localHandle)
// !!!FIX THIS!!! create a custom function for Input that return the available size of bytes as well and fill the buffer
'bytesRead = Input #localHandle, buffer
libssh2_sftp_write(remoteHandle, buffer1, bytesRead)
Loop
Close # localHandle
' Datei auf dem Server schließen
libssh2_sftp_close(remoteHandle)
' SFTP-Session beenden
libssh2_sftp_shutdown(sftp)
' Session beenden
libssh2_session_disconnect(session, "")
libssh2_session_free(session)
libssh2_exit()
Function ConnectToServer(ByVal server As String, ByVal port As Integer) As Long
Declare Function socket Lib "wsock32.dll" (ByVal af As Long, ByVal socktype As Long, ByVal protocol As Long) As Long
Declare Function connect Lib "wsock32.dll" (ByVal sock As Long, ByRef name As SOCKADDR_IN, ByVal namelen As Long) As Long
Declare Function inet_addr Lib "wsock32.dll" (ByVal cp As String) As Long
Declare Function closesocket Lib "wsock32.dll" (ByVal socket As Long) As Long
Type IN_ADDR
s_addr As Long
End Type
Type SOCKADDR_IN
sin_family As Integer
sin_port As Integer
sin_addr As IN_ADDR
sin_zero As String * 8
End Type
Const AF_INET = 2
Const SOCK_STREAM = 1
Const IPPROTO_TCP = 6
Dim ipAddr As Long
Dim sock As Long
Dim sockaddr As SOCKADDR_IN
' IP-Adresse des Servers abrufen
ipAddr = inet_addr(server)
' Socket-Adresse vorbereiten
Const AF_INET As Integer = 2
sockaddr.sin_family = AF_INET
sockaddr.sin_port = htons(port)
sockaddr.sin_addr.s_addr = ipAddr
' Socket öffnen
sock = socket(AF_INET, SOCK_STREAM, 0)
If sock < 0 Then
MsgBox "Socket konnte nicht geöffnet werden"
Exit Function
End If
' Verbindung zum Server herstellen
// Declare Function connect Lib "wsock32.dll" (ByVal sock As Long, ByRef name As SOCKADDR, ByVal namelen As Long) As Long
If connect(sock, sockaddr, Len(sockaddr)) < 0 Then
MsgBox "Verbindung konnte nicht hergestellt werden"
closesocket(sock)
Exit Function
End If
ConnectToServer = sock
End Function
Function inet_addr(ByVal address As String) As Long
Dim arrOctets() As String
Dim lAddr As Long
Dim i As Integer
SplitString(arrOctets(), address, ".")
If UBound(arrOctets()) <> 3 Then
inet_addr = 0
Exit Function
End If
For i = 0 To 3
If Not IsNumeric(arrOctets(i)) Then
inet_addr = 0
Exit Function
End If
If CInt(arrOctets(i)) < 0 Or CInt(arrOctets(i)) > 255 Then
inet_addr = 0
Exit Function
End If
lAddr = lAddr Or Val(arrOctets(i)) * (256 ^ i)
Next i
Return lAddr
End Function
Procedure SplitString(ByRef arrOctets() As String, ByVal s As String, ByVal separator As String)
Dim parts() As String
Dim start As Integer, ende As Integer, i As Integer
start = 1
i = 0
Do While ende < Len(s)
ende = InStr(start, s, separator)
If ende = 0 Then ende = Len(s) + 1
ReDim parts(i)
parts(i) = Mid(s, start, ende - start)
i++
start = ende + 1
Loop
EndProc