Results 1 to 5 of 5

Thread: Handles

  1. #1

    Thread Starter
    Fanatic Member Stevie's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    565
    I'm starting to use the API for the first time and need a little help.

    I want to use the GetFileSize function, but do not know how to obtain the handle to the file.

    How do you go about obtaining handles in general?

    Thanks in advance


  2. #2
    Guest
    Look at the "Partner Sites" section. and select VB API link.
    There you can find hundreds API's and how you can use them.

    But here i have the GetFileSize api for you.

    Code:
    Declare Function GetFileSize Lib "kernel32.dll" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long 
    
    ' Display the file size of "C:\MyProgram\datafile.txt".  Note how
    ' the alternate declare of the CreateFile function (needed to get the file's handle)
    ' must be used -- see that function's page for details.
    
    ' receives a handle to the file
    Dim hfile As Long  
    ' receive the low- and high-order halves of the file size
    Dim loworder As Long, highorder As Long  
    ' return value
    Dim retval As Long  
    
    ' Get a handle to the file using CreateFile's alternate declare (necessary for non-Win NT).
    hfile = CreateFileNS("C:\MyProgram\datafile.txt", GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, 0)
    If hfile = -1 Then  ' error opening the file
      Debug.Print "Could not open file C:\MyProgram\datafile.txt"
      End  ' abort the program
    End If
    
    ' Read and display that file's size in bytes.
    highorder = 0  ' initialize the value for high-order half
    loworder = GetFileSize(hfile, highorder)  ' read the file's size
    If highorder = 0 Then  ' if there is no high-order part
      Debug.Print "File size:"; loworder; "bytes"  ' display the file size
    Else  ' if there is a high-order part (file size >= 4.29 GB!)
      ' Visual Basic has no 64-bit variables, so we can't display the actual value:
      Debug.Print "File size:"; highorder; "* 2^32 +"; loworder; "bytes (in base-10)"
      ' But we can combine the two hex values to give the result in hexadecimal:
      Debug.Print "File size: "; Hex(highorder); Hex(loworder); " bytes (in hexadecimal)"
    End If
    
    ' Close the file
    retval = CloseHandle(hfile)  ' close the handle
    Goodluck.

    -Kayoca Mortation

  3. #3

    Thread Starter
    Fanatic Member Stevie's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    565
    I'm going to try that right now.

    Thanks for the help.

  4. #4

    Thread Starter
    Fanatic Member Stevie's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    565
    I've implemented the GetFileSize and CreateFile APIs and they work just great when being run from within VB.

    When I make an exe CreateFile never opens the file I specify as it always returns -1. I am passing it the correct path, but in a different way than when it's being run within VB. When it is an exe it finds the path of the file by using CurDir.

    Can anybody tell me why this is??

  5. #5

    Thread Starter
    Fanatic Member Stevie's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    565
    Sorry, seems that the error isn't with the CreateFile or GetFileSize functions but with the MoveFile function.

    It always returns 0. (As the file isn't moved to the correct location, that causes the error to pop up in the other functions)

    Any ideas why MoveFile doesn't seem to work when the paths passed to it use CurDir?

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