ElHaix
ElHaix

Reputation: 12986

Multiplication in VB6 causing an Overflow Exception - how to resolve?

In antiquated VB6, I am trying to calculate the free and use disk space:

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

Dim info As DiskInformation
Dim lAnswer As Long
Dim lpRootPathName As String
Dim lpSectorsPerCluster As Long
Dim lpBytesPerSector As Long
Dim lpNumberOfFreeClusters As Long
Dim lpTotalNumberOfClusters As Long
Dim lBytesPerCluster As Long
Dim lNumFreeBytes As Double
Dim dPercentFreeClusters As Double
Dim sString As String

lpRootPathName = "c:\"
lAnswer = GetDiskFreeSpace(lpRootPathName, lpSectorsPerCluster, lpBytesPerSector, lpNumberOfFreeClusters, lpTotalNumberOfClusters)
lBytesPerCluster = lpSectorsPerCluster * lpBytesPerSector

' Throws overflow exception - I guess there were no Terabyte drives when VB6 came around
lNumFreeBytes = lBytesPerCluster * lpNumberOfFreeClusters

lBytesPerCluster = 4096 | lpNumberOfFreeClusters = 474304894

I have tried wrapping the multiplication with CLng still get the overflow exception.

How can I resolve ? lBytesPerCluster * lpNumberOfFreeClusters ??

Upvotes: 0

Views: 182

Answers (1)

ElHaix
ElHaix

Reputation: 12986

Conversion to Variant data type:

Dim v1, v2, v3 As Variant
v1 = lBytesPerCluster
v2 = lpNumberOfFreeClusters

' this now works:
? v1*v2 

Upvotes: 1

Related Questions