Golang:使用Windows 10 API / UWP / System.WindowsRuntime?

huangapple go评论94阅读模式
英文:

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

问题

使用Go中的syscall,我如何调用Windows 10中的UWP API?我已经看过并尝试了许多_win32_的示例,但当我尝试使用System.WindowsRuntime.dll时,出现了问题;具体来说,我收到了以下错误信息:

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

(这是在运行时发生的,二进制文件构建正常)

我尝试使用标准的go build命令构建,以及

go build -ldflags="-H windows"

示例代码:

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

注意:我还尝试了其他变体:

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

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

有人能够成功运行这个程序或者对此有任何建议吗?
一如既往,非常感谢!

英文:

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!!

答案1

得分: 2

创建一个如下所示的文件:

//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)
}

然后进行构建:

go generate
go mod init free
go mod tidy
go build

结果:

bytesPerSector 512
freeClusters 12511186
numberOfClusters 25434879
sectorsPerCluster 8

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

英文:

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

答案2

得分: 0

我不知道你在哪里找到了"System.WindowsRuntime.dll",据我所知,它并不存在(除非你手动创建了一个名为这样的DLL)。

至于System.Runtime.WindowsRuntime.dll,它是.NET Framework的一部分,而不是Windows,它是一个托管的DLL。你不能使用syscall.NewLazyDLL加载这样的DLL。而且,该DLL实际上并不包含任何Windows API,只是为了让.NET能够与它们一起工作的粘合剂。

你可能正在寻找combase.dll中的RoGetActivationFactory等函数。

英文:

I have no idea where you found "System.WindowsRuntime.dll" because as far as I can tell, it doesn't exist (unless you manually made a DLL named that).

As for System.Runtime.WindowsRuntime.dll: it's part of .NET Framework, not Windows, and it is a managed DLL. You cannot load such DLLs using syscall.NewLazyDLL. What's more, that DLL doesn't really contain any Windows APIs - just glue for .NET to be able to work with them.

You're probably looking for functions like RoGetActivationFactory in combase.dll.

huangapple
  • 本文由 发表于 2017年3月23日 22:29:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/42978989.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定