Home > Backend Development > Golang > How to Determine a Drive\'s Total Capacity Using Go and Windows APIs?

How to Determine a Drive\'s Total Capacity Using Go and Windows APIs?

Mary-Kate Olsen
Release: 2024-11-28 02:28:11
Original
401 people have browsed it

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
);
Copy after login

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:

  1. Load the kernel32.dll dynamic link library:
kernelDLL := syscall.MustLoadDLL("kernel32.dll")
Copy after login
  1. Get a function pointer to the GetDiskFreeSpaceExW() function:
GetDiskFreeSpaceExW := kernelDLL.MustFindProc("GetDiskFreeSpaceExW")
Copy after login
  1. Create variables to receive the results:
var free, total, avail int64
Copy after login
  1. Call the function with the appropriate arguments:
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)),
)
Copy after login
  1. Check the return value and print the results:
fmt.Println(r1, r2, lastErr)
fmt.Println("Free:", free, "Total:", total, "Available:", avail)
Copy after login

Example Output

Running the provided code will produce an output similar to this:

1 0 Success.
Free: 16795295744 Total: 145545281536 Available: 16795295744
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template