【问题标题】:Golang: using Windows 10 API / UWP / System.WindowsRuntime?Golang:使用 Windows 10 API / UWP / System.WindowsRuntime?
【发布时间】:2017-08-16 04:20:52
【问题描述】:

在 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")

有人能够让这个运行或对此事有任何建议吗? 一如既往,非常感激!!

【问题讨论】:

  • GetDiskFreeSpace 位于 kernel32.dll 中(参见 msdn.microsoft.com/en-us/library/windows/desktop/… )。所以你应该有 windowsRuntime = syscall.NewLazyDLL("kernel32.dll")
  • 是的,你从哪里得到System.WindowsRuntime.dll?您是否尝试调用特定 UWP 类的 GetDiskFreeSpace() 方法?如果是这样,你将进入一个受伤的世界......
  • 是的 - kernel32.dll 它是,不知道我从哪里得到的,我试图追溯我的步骤,无论如何,System.WindowsRuntime.dll 没有找到 panic: Failed to load System.WindowsRuntime.dll: The specified module could not be found. 而当我有错误的函数/过程名称GetDiskFreeSpace 我收到panic: Failed to find GetDiskFreeSpace procedure in Kernel32.dll: The specified procedure could not be found. 所以System.WindowsRuntime.dll 肯定丢失了。现在只是想弄清楚这是否是正确的名称。我会尽快回复您。
  • 顺便说一句,您必须使用 Unicode 函数名称,这就是找不到该过程的原因——所以GetDiskFreeSpaceW 你会很好——以防其他人遇到这个问题.

标签: winapi go windows-runtime uwp winrt-xaml


【解决方案1】:

像这样创建一个文件:

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

【讨论】:

    【解决方案2】:

    我不知道您在哪里找到“System.WindowsRuntime.dll”,因为据我所知,它不存在(除非您手动创建了一个名为该名称的 DLL)。

    至于 System.Runtime.WindowsRuntime.dll:它是 .NET Framework 的一部分,而不是 Windows,它是一个托管 DLL。您不能使用 syscall.NewLazyDLL 加载此类 DLL。更重要的是,该 DLL 并不真正包含任何 Windows API——只是为了让 .NET 能够使用它们而粘合。

    您可能正在 combase.dll 中寻找像 RoGetActivationFactory 这样的函数。

    【讨论】:

      猜你喜欢
      • 2016-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-12
      • 2018-01-31
      • 2016-07-24
      • 2017-10-06
      相关资源
      最近更新 更多