Results 1 to 18 of 18

Thread: [RESOLVED] true or false

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Resolved [RESOLVED] true or false

    vb Code:
    1. Dim ret As Boolean
    2. ' some other code
    3. ret = FtpPutFile(server, fname, fname, FTP_TRANSFER_TYPE_BINARY, 0)
    4. If Not ret Then myip = ""
    5. ' 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

  2. #2
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: true or false

    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.

  3. #3
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: true or false

    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.

  4. #4

  5. #5
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: true or false

    vb Code:
    1. 'http://allapi.mentalis.org/apilist/FtpPutFile.shtml
    2. Declare Function FtpPutFileA Lib "wininet.dll" (ByVal hConnect As Long, _
    3.                                                 ByVal lpszLocalFile As String, _
    4.                                                 ByVal lpszNewRemoteFile As String, _
    5.                                                 ByVal dwFlags As Long, _
    6.                                                 ByVal dwContext As Long) _
    7.                                                 As Boolean
    Rather than Ret have you declared FtpPutFile(A) correctly?

  6. #6
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: true or false

    I have read in Microsoft support page that FtpPutFile API returns TRUE even when the operation fails.

    http://support.microsoft.com/kb/321156

    This symptom occurs more frequently with a file that is less than 4 kilobytes (KB).
    I guess it's not that reliable.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: true or false

    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

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: true or false

    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?
    Attached Images Attached Images  
    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

  9. #9
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: true or false

    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

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: true or false

    @ 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

  11. #11
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: true or false

    My 2 cents; API's are (primarily) written for C, and the returns (and parameters in some cases) don't necessarily align to VB. - Thoughts.

    VB's True and False >>> True = -1, and False = 0 (as Frans C points out)

  12. #12
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: true or false

    Quote Originally Posted by Bruce Fox
    My 2 cents; API's are (primarily) written for C, and the returns (and parameters in some cases) don't necessarily align to VB. - Thoughts.

    VB's True and False >>> True = -1, and False = 0 (as Frans C points out)
    In which case how about simply altering the API declare to return an Integer rather than a Boolean. Keep Ret as a boolean.

  13. #13
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: true or false

    You probably need to multiply 'ret' by "-1"

    So, 1 will yeild -1, and 0 will yeild 0.

    BTW, from the Immediate Window:
    ? cbool(-1)
    True
    ? cbool(1)
    True
    ? cbool(0)
    False

  14. #14
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: true or false

    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

  15. #15
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: true or false

    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.
    Frans

  16. #16
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: true or false

    The function declaration is incorrect. FtpPutFile does not return a Boolean, it returns a BOOL, which is a Long.

  17. #17

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: true or false

    thanks to all

    i declared ret as a long and used
    vb Code:
    1. If Not ret = 1 Then

    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

  18. #18

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width