Home >Backend Development >Golang >How to Determine a Drive\'s Total Capacity Using Go and Windows APIs?
Determining Drive Capacity in Windows Using Go APIs
In a previous question, you sought guidance on obtaining the available disk space in Go using Windows API calls. This response demonstrated the use of the GetDiskFreeSpaceExW() function from kernel32.dll to retrieve this information.
To further your exploration, you now wish to determine the total size of a specific drive, such as C:. The GetDiskFreeSpaceExW() function can cater to this need as well.
Signature of GetDiskFreeSpaceExW()
The signature of this function is as follows:
BOOL GetDiskFreeSpaceExW( LPCWSTR lpDirectoryName, PULARGE_INTEGER lpFreeBytesAvailableToCaller, PULARGE_INTEGER lpTotalNumberOfBytes, PULARGE_INTEGER lpTotalNumberOfFreeBytes );
It takes an in-parameter (the drive path) and returns three out-parameters: the free bytes available to the caller, the total size of the disk, and the total free bytes.
Usage in Go
To utilize this function in Go, you can follow these steps:
kernelDLL := syscall.MustLoadDLL("kernel32.dll")
GetDiskFreeSpaceExW := kernelDLL.MustFindProc("GetDiskFreeSpaceExW")
var free, total, avail int64
path := "c:\" r1, r2, lastErr := GetDiskFreeSpaceExW.Call( uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(path))), uintptr(unsafe.Pointer(&free)), uintptr(unsafe.Pointer(&total)), uintptr(unsafe.Pointer(&avail)), )
fmt.Println(r1, r2, lastErr) fmt.Println("Free:", free, "Total:", total, "Available:", avail)
Example Output
Running the provided code will produce an output similar to this:
1 0 Success. Free: 16795295744 Total: 145545281536 Available: 16795295744
This output indicates that the C: drive has a total size of 145545281536 bytes.
The above is the detailed content of How to Determine a Drive\'s Total Capacity Using Go and Windows APIs?. For more information, please follow other related articles on the PHP Chinese website!