ret = FtpPutFile(server, fname, fname, FTP_TRANSFER_TYPE_BINARY, 0)
If Not ret Then myip = ""
' more code
when i declare ret like this the myip = "" runs everytime regardless of whether ret is true or false
if i don't declare ret and leave it to vb to cast it as a variant then ret will still return true or false and the if statement runs correctly
what logic am i missing about declaring ret?
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
You're doing it correctly. I don't think it's doing what you think it is, but rather it just seems that way.
Put a breakpoint on the If...Then line, and step through when ret is True. Dollars to donuts says that the myip = "" doesn't execute, but that myip is already empty.
However, if FtpPutFile() isn't returning a Boolean, you shouldn't cast it to a Boolean. Better would be to declare ret as the same type as FtpPutFile() returns. I'm guessing it returns a Long.
I'm not familiar with FtpPutFile but unless it returns a Boolean your code is prone to error. Perhaps you should check for specific values like 0 or 1.
i had already checked the code before posting, according to allapi putftpfile returns a boolean, my declare is copied from their examples,
i tested f the value of ret is true or false depending if it succeed or fail, which was correct, by forcing it to fail, i stepped through the code and the if code ran regardless of the value of ret when ret is declared as boolean, i knew that while testing the value of myip was already "", but the code statement ran anyway.
if i did not declare ret, then it would still return either true or false, but the code then ran correctly
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
on further testing it still makes no sense to me, as you can see from my screenshot it just isn't right
if i put a line ret = true, then the code will work, so can a return value from a function return true that is a different value from true?
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
This is a really strange situation. ret contains True, and NOT True should be False.
Can you check what CLng(ret) gives for value (just type it in the immediate window)?
Possibly the function returns 0 or 1 instead of a true VB boolean, and somehow this value is stored in a VB Boolean (which should not be possible, because VB Booleans use 0 and -1, and not 0 and 1)
This would result in
Not 1 = -2
CBool(-2) = True
The solution would be:
Code:
ret = CBool(FtpPutFile(server, fname, fname, FTP_TRANSFER_TYPE_BINARY, 0))
@ frans
you are quite correct about the reason for the problem, it returns 1 not -1
your solution does not work however, still returns 1, i had already tried it, though i hadn't figured why it did not work, at least now i can safely work around the problem
thanks
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
You should not have to, VB will convert a number to a Boolean correctly i.e. zero = false = 0, nonzero = True = -1. I think that the function is returning a boolean with its bits incorrectly set for VB, but which VB thinks is valid... this results in a boolean which appears correct until you try using Not (which inverts the bits) where it will equate to True regardless
I agree with Milk. If the function returns 0 or 1, you are probably best of by changing the definition of the api that it returns an Integer, and not a boolean.
VB should then understand that it must convert the integer to a boolean.
I was hoping that CBool would fix it, but probably it just copies the value because it thinks it is already a boolean.
Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hConnect As Long, ByVal lpszLocalFile As String, ByVal lpszNewRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
as copied from AllApi (the bible), but not quite right
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete