Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang: using Windows 10 API / UWP / System.WindowsRuntime?

Using syscall in Go how can I call the UWP APIs within Windows 10? I have seen and tried many win32 examples, but when I tried using System.WindowsRuntime.dll it was a no-go; specifically, I received:

panic: Failed to load System.WindowsRuntime.dll: The specified module could not be found.

(this was at runtime, the binary built fine)

I tried building both with a standard go build as well as

go build -ldflags="-H windows"

example code:

var(
    windowsRuntime      = syscall.NewLazyDLL("System.WindowsRuntime.dll")
    getDiskFreeSpace    = windowsRuntime.NewProc("GetDiskFreeSpace")
)

Note: Other variants tried:

windowsRuntime      = syscall.NewLazyDLL("System.Runtime.WindowsRuntime.dll")

&

windowsRuntime      = syscall.NewLazyDLL("WindowsRuntime.dll")

Anyone been able to get this running or have any advice on the matter? As always, greatly appreciated!!

like image 571
ehiller Avatar asked Mar 23 '17 14:03

ehiller


1 Answers

Create a file like this:

//go:generate mkwinsyscall -output zfree.go free.go
//sys getDiskFreeSpace(pathName string, sectorsPerCluster *int, bytesPerSector *int, freeClusters *int, numberOfClusters *int) (err error) = GetDiskFreeSpaceA
package main

func main() {
   var bytesPerSector, freeClusters, numberOfClusters, sectorsPerCluster int
   getDiskFreeSpace(
      `C:\`,
      &sectorsPerCluster,
      &bytesPerSector,
      &freeClusters,
      &numberOfClusters,
   )
   println("bytesPerSector", bytesPerSector)
   println("freeClusters", freeClusters)
   println("numberOfClusters", numberOfClusters)
   println("sectorsPerCluster", sectorsPerCluster)
}

Then build:

go generate
go mod init free
go mod tidy
go build

Result:

bytesPerSector 512
freeClusters 12511186
numberOfClusters 25434879
sectorsPerCluster 8

https://github.com/golang/sys/tree/master/windows/mkwinsyscall

like image 104
Zombo Avatar answered Oct 20 '22 01:10

Zombo