Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

DiskSize and Diskfree for all Windows platforms.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • DiskSize and Diskfree for all Windows platforms.

    As I see several requests about disksize and diskfree that won't work with any Windows version and return negative value for hard disk larger than 2 Mb.
    Here is the solution that should work in any situation and with any Windows version.
    Code:
    '---------------------------------------------------------------------------
    ' dvDiskFree - Returns the space left on very large hard disk.
    '
    ' This function works either with 95 OSR1    using GetDiskFreeSpace
    ' or 95 OSR2, 98 or NT 4.0 and NT 5.0        using GetDiskFreeSpaceEx
    '
    FUNCTION dvDiskFree&&(BYVAL DSK$) EXPORT
    
        DIM pGetDiskFreeSpaceEx AS DWORD PTR
    
      ' Returns the number of free bytes on selected drive
        LOCAL d  AS ASCIIZ * 4
        LOCAL sc AS DWORD
        LOCAL bs AS DWORD
        LOCAL fc AS DWORD
        LOCAL tc AS DWORD
        Drive% = ASC(UCASE$(Dsk$))
        IF Drive% < 65 OR Drive% > 90 THEN Drive% = 0
        IF Drive% THEN
           d = CHR$(Drive%) + ":\"
        ELSE
           d = CURDIR$
        END IF
    
        Done& = 0
        hLib& = LoadLibrary("KERNEL32.DLL")
        IF hLib& THEN
           pGetDiskFreeSpaceEx = GetProcAddress(hLib&, "GetDiskFreeSpaceExA")
           IF pGetDiskFreeSpaceEx THEN
              CALL DWORD pGetDiskFreeSpaceEx CDECL (d, lpFreeBytesAvailableToCaller&&, lpTotalNumberOfBytes&&, lpTotalNumberOfFreeBytes&&)
              FUNCTION = lpFreeBytesAvailableToCaller&&
              Done& = -1
           END IF
           CALL FreeLibrary(hLib&)
        END IF
        IF NOT Done& THEN
           IF ISTRUE(GetDiskFreeSpace(d, sc, bs, fc, tc)) THEN
              FUNCTION = CQUD(sc * bs * fc)
           END IF
        END IF
    END FUNCTION
    
    '---------------------------------------------------------------------------
    ' dvDiskSize - Returns the size of very large hard disk.
    '
    ' This function works either with 95 OSR1    using GetDiskFreeSpace
    ' or 95 OSR2, 98 or NT 4.0 and NT 5.0        using GetDiskFreeSpaceEx
    '
    FUNCTION dvDiskSize&&(BYVAL DSK$) EXPORT
    ' This function works either with 95 OSR1    using GetDiskFreeSpace
    ' or 95 OSR2, 98 or NT 4.0 and NT 5.0        using GetDiskFreeSpaceEx
    
        DIM pGetDiskFreeSpaceEx AS DWORD PTR
    
      ' Returns the number of total bytes on selected drive
        LOCAL d  AS ASCIIZ * 4
        LOCAL sc AS DWORD
        LOCAL bs AS DWORD
        LOCAL fc AS DWORD
        LOCAL tc AS DWORD
        Drive% = ASC(UCASE$(Dsk$))
        IF Drive% < 65 OR Drive% > 90 THEN Drive% = 0
        IF Drive% THEN
           d = CHR$(Drive%) + ":\"
        ELSE
           d = CURDIR$
        END IF
        Done& = 0
        hLib& = LoadLibrary("KERNEL32.DLL")
        IF hLib& THEN
           pGetDiskFreeSpaceEx = GetProcAddress(hLib&, "GetDiskFreeSpaceExA")
           IF pGetDiskFreeSpaceEx THEN
              CALL DWORD pGetDiskFreeSpaceEx CDECL (d, lpFreeBytesAvailableToCaller&&, lpTotalNumberOfBytes&&, lpTotalNumberOfFreeBytes&&)
              FUNCTION = lpTotalNumberOfBytes&&
              Done& = -1
           END IF
           CALL FreeLibrary(hLib&)
        END IF
        IF NOT Done& THEN
           IF ISTRUE(GetDiskFreeSpace(d, sc, bs, fc, tc)) THEN
              FUNCTION = CQUD(sc * bs * tc)
           END IF
        END IF
    END FUNCTION
    Patrice Terrier
    www.zapsolution.com
    www.objreader.com
    Addons: GDImage.DLL 32/64-bit (Graphic library), WinLIFT.DLL 32/64-bit (Skin Engine).

  • #2
    WARNING: This code incorrectly use CDECL calling convention which could cause stack corruption.

    These two API functions use the SDECL/STDCALL convention, and should therefore be called with SDECL instead of CDECL.


    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment

    Working...
    X