PDA

Click to See Complete Forum and Search --> : API Gurus only please!!!


CyberSurfer
Sep 29th, 2000, 02:03 PM
Part of my app needs to calculate stuff about the Users hard disk(s) which would be done using the GetDiskFreeSpace API. The problem is that this will not work on Drives over 2GB in size, or FAT32 Drives. The recommended Microsoft solution is to use the GetDiskFreeSpaceEx API. My questions are this....


1) Does the GetDiskFreeSpaceEx API use the same parameters as GetDiskFreeSpace?

2) Will GetDiskFreeSpaceEx work with FAT16, sub 2GB drives, removing the need to use GetDiskFreeSpace at all?

3) If not, Microsoft also recommend that you use the GetVersionEx API to determine the users Windows version. How can I distinguish between different versions of Win 95 when using this?

Thanks in Advance!!

Sep 29th, 2000, 02:23 PM
Try the following:

Private Declare Function GetDiskFreeSpaceEx Lib "kernel32.dll" Alias "GetDiskFreeSpaceExA" (ByVal lpDirectoryName As String, lpFreeBytesAvailableToCaller As ULARGE_INTEGER, lpTotalNumberOfBytes As ULARGE_INTEGER, lpTotalNumberOfFreeBytes As ULARGE_INTEGER) As Long
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Private Type ULARGE_INTEGER
LowPart As Long
HighPart As Long
End Type

Dim FreeUser As ULARGE_INTEGER
Dim Total As ULARGE_INTEGER
Dim FreeSys As ULARGE_INTEGER
Dim Temp As Currency
Dim fTemp As Currency

Private Sub Command1_Click()
'Get DiskSpace
GetDiskFreeSpaceEx "C:\", FreeUser, Total, FreeSys
CopyMemory Temp, Total, 8
CopyMemory fTemp, FreeUser, 8
Print "Total Space: " & (CCur(Temp) * 10000) / 1000000000 & " GB"
Print "Free Space: " & (CCur(fTemp) * 10000) / 1000000000 & " GB"
End Sub

CyberSurfer
Sep 29th, 2000, 02:41 PM
Thanks Megatron.

This is the code I have so far.



Option Explicit

Public Type DRIVE_INFO
DrvSectors As Long
DrvBytesPerSector As Long
DrvFreeClusters As Long
DrvTotalClusters As Long
DrvSpaceFree As Long
DrvSpaceUsed As Long
DrvSpaceTotal As Long
End Type

Public Declare Function GetDiskFreeSpace _
Lib "kernel32" Alias "GetDiskFreeSpaceA" _
(ByVal lpRootPathName As String, _
lpSectorsPerCluster As Long, _
lpBytesPerSector As Long, _
lpNumberOfFreeClusters As Long, _
lpTotalNumberOfClusters As Long) As Long

Public Declare Function GetLogicalDriveStrings _
Lib "kernel32" Alias "GetLogicalDriveStringsA" _
(ByVal nBufferLength As Long, _
ByVal lpBuffer As String) As Long


FORM CODE

Option Explicit

Private Sub Form_Load()

Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
LoadAvailableDrives Combo1
Combo1.ListIndex = 1

End Sub


Private Sub cmdEnd_Click()

Unload Me

End Sub


Private Sub cmdVolumeInfo_Click()

'Calls other functions to provide the info.
'Data is stored in my own user-defined type.
Dim RDI As DRIVE_INFO
Dim r As Long
Dim RootPathName As String

'get the drive to find
RootPathName = Combo1.List(Combo1.ListIndex)

'get the drive's disk parameters
Call rgbGetDiskFreeSpaceRDI(RootPathName, RDI)

'show the results
Cls
Print
Print " Drive Statistics for ", ": "; UCase$(RootPathName$)
Print
Print " No of Sectors", ": "; RDI.DrvSectors
Print " Bytes per sector", ": "; RDI.DrvBytesPerSector
Print " Clusters Free", ": "; RDI.DrvFreeClusters
Print " Total Clusters", ": "; RDI.DrvTotalClusters
Print " Drive Space Used", ": "; Format$(RDI.DrvSpaceUsed, "###,###,###,##0") & " bytes"
Print " Drive Space Free", ": "; Format$(RDI.DrvSpaceFree, "###,###,###,##0") & " bytes"
Print " Total Drive Size", ": "; Format$(RDI.DrvSpaceTotal, "###,###,###,##0") & " bytes"

End Sub


Private Sub LoadAvailableDrives(cmbo As ComboBox)

Dim lpBuffer As String

'get list of available drives
lpBuffer = GetDriveString()

'Separate the drive strings
'and add to the combo. StripNulls
'will continually shorten the
'string. Loop until a single
'remaining terminating null is
'encountered.
Do Until lpBuffer = Chr(0)

'strip off one drive item
'and add to the combo
cmbo.AddItem StripNulls(lpBuffer)

Loop

End Sub


Private Function GetDriveString() As String

'returns string of available
'drives each separated by a null
Dim sBuffer As String

'possible 26 drives, three characters each
sBuffer = Space$(26 * 3)

If GetLogicalDriveStrings(Len(sBuffer), sBuffer) Then

'do not trim off trailing null!
GetDriveString = Trim$(sBuffer)

End If

End Function


Private Function StripNulls(startstr As String) As String

'Take a string separated by chr$(0)
'and split off 1 item, shortening the
'string so next item is ready for removal.
Dim pos As Long

pos = InStr(startstr$, Chr$(0))

If pos Then

StripNulls = Mid$(startstr, 1, pos - 1)
startstr = Mid$(startstr, pos + 1, Len(startstr))
Exit Function

End If

End Function


Private Function rgbGetDiskFreeSpaceRDI(RootPathName, _
RDI As DRIVE_INFO) As Long

'returns data about the selected drive.
'Passed is the RootPathName; the other
'variables are filled in here.
Dim r As Long

r = GetDiskFreeSpace(RootPathName, RDI.DrvSectors, _
RDI.DrvBytesPerSector, _
RDI.DrvFreeClusters, _
RDI.DrvTotalClusters)

RDI.DrvSpaceTotal = (RDI.DrvSectors * RDI.DrvBytesPerSector * RDI.DrvTotalClusters)
RDI.DrvSpaceFree = (RDI.DrvSectors * RDI.DrvBytesPerSector * RDI.DrvFreeClusters)
RDI.DrvSpaceUsed = RDI.DrvSpaceTotal - RDI.DrvSpaceFree

rgbGetDiskFreeSpaceRDI = r

End Function



I did not write this, which is why I am not sure. Will this work if I change GetDiskFreeSpace to GetDiskFreeSpaceEx??

THX

CyberSurfer
Oct 2nd, 2000, 06:58 AM
NE Body?

kb244
Oct 2nd, 2000, 09:01 AM
I dont have the answer, but just wanted to say, you wouldnt
get too far if you say you ONLY want Guru's to help,
considering there may be an intermediate people who knows
this particular API command very well. I used it alot, but
since I'm not a Guru I guess I better not answer.

[edited to make it fit the screen]

oetje
Oct 2nd, 2000, 10:57 AM
Look at www.vbapi.com

CyberSurfer
Oct 2nd, 2000, 01:41 PM
Sorry kb244!

I should probably have put Guru Question. Its just that questions with the word Guru in the subject tend to get lots of views. No offence meant. Any help welcome!

Thanks oetje, will do.

Oct 2nd, 2000, 02:20 PM
The example I provided demonstrates the use of GetFreeDiskSpaceEx.

CyberSurfer
Oct 2nd, 2000, 02:34 PM
Thanks very much Megatron.

I was wondering whether GetDiskSpaceEx did all that free cluster stuff, but upon looking it up on VBAPI (thanks oetje) I discovered that it didn't. I wasn't dismissing your example or anything.