Skip to content

Instantly share code, notes, and snippets.

@khchen
Last active November 9, 2021 18:27
Show Gist options
  • Select an option

  • Save khchen/51f69cebe940e8c65b2a36e2d9f0e5b3 to your computer and use it in GitHub Desktop.

Select an option

Save khchen/51f69cebe940e8c65b2a36e2d9f0e5b3 to your computer and use it in GitHub Desktop.
#[
Author: Ward
Example of GetLogicalDrives and GetLogicalDriveStrings.
References:
https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getlogicaldrives
https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getlogicaldrivestringsw
]#
import winim/lean
import bitops, strutils
# This converter is really convenient.
converter nimIntTocint(x: int): cint = cint x
echo "Use GetLogicalDrives: "
var
drives = GetLogicalDrives()
output = ""
for i in 0..25:
if drives.testBit(i):
output.add chr('A'.ord + i)
output.add ":\\ "
echo output, "\n"
echo "Use GetLogicalDriveStrings:"
var buffer = T(1024) # generate wstring or mstring buffer depend on conditional symbol
buffer.setLen(GetLogicalDriveStrings(buffer.len, &buffer))
# Here we get a list separated by \0, we can use struitls to deal with the \0 char.
# However, buffer is wstring or mstring, use `$` to convert it to nim string.
echo ($buffer).strip(chars={'\0'}).replace("\0", " ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment