1.

Solve : Inconsistent vb error?

Answer»

So I have an FTP client integrated into one of my VB6 programs. The function to send files through FTP is acting inconsistent and I think that it is a problem with VB.

This works:

Code: [Select]Private Declare Function FTPPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hFtpSession As Long, ByVal lpszLocalFile As String, ByVal lpszRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean

Public Function SendFTPFile(sFile As String, sRemote As String) As Boolean
If FTPPutFile(hConnection, sFile, sRemote, FTP_TRANSFER_TYPE_BINARY, 0) Then
SendFTPFile = True
Else
SendFTPFile = False
End If
End Function


This doesn't work with a while loop:

Code: [Select]Private Declare Function FTPPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hFtpSession As Long, ByVal lpszLocalFile As String, ByVal lpszRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean

Public Function SendFTPFile(sFile As String, sRemote As String) As Boolean
SendFTPFile = FTPPutFile(hConnection, sFile, sRemote, FTP_TRANSFER_TYPE_BINARY, 0)
End Function

When I use the code below, it only works when calling the first function even though the functions should do exactly the same thing. WHY? It doesn't make sense.

While Not SendFTPFile(...........) And i < tries
i = i + 1
WendYou imply that there are errors....

but you NEVER mention them outside the subject. What I mean is that the second function appears to return true (using the built in vb debugger), but the while loop I posted goes until i = tries even if the SendFTPFile function returns true every time. The while loop does work with the first function though, which SHOULD do exactly the same as the second. So what I am asking is why doesn't the second function work the same way as the first?Quote from: Linux711 on May 01, 2011, 09:29:14 PM

So what I am asking is why doesn't the second function work the same way as the first?

FTPPutFile doesn't return a boolean- it returns a BOOL, which in the WINDOWS headers is actually an int; additionally, it actually returns 1 for true, and 0 for false; whereas Visual Basic uses -1 for true, and 0 for false. This can cause some problems when you try to mix them around. You could either return a boolean expression LIKE this:

Quote
Private Declare Function FTPPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hFtpSession As Long, ByVal lpszLocalFile As String, ByVal lpszRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Long

Public Function SendFTPFile(sFile As String, sRemote As String) As Boolean
SendFTPFile = (FTPPutFile(hConnection, sFile, sRemote, FTP_TRANSFER_TYPE_BINARY, 0)<>0)
End Function

Or you could use CBool() on the return value of FTPPutFile when returning it from the function, or just use the method you did and use an If. In any case, the inconsistency is most likely because of the fact that the function declared doesn't return a OLE Boolean value but rather a Long Integer and VB was interpreting it incorrectly as a RESULT.


Discussion

No Comment Found