英文:
Query total physical memory in Windows with Golang
问题
尝试在Windows上使用Go语言获取总物理内存,但不确定要使用哪个包和调用。我相信可以使用syscall
来实现。同时,我也希望不必与C进行接口交互来完成这个任务。
英文:
Trying to get the total physical memory using Go on Windows but not sure which package(s) to use and calls to make. I believe this can be done with syscall
. Would also prefer not to interface with C to do this.
答案1
得分: 5
syscall
包的官方在线godoc文档位于https://golang.org/pkg/syscall/,似乎记录了Linux的API,因此在网上找到资源有些困难。
首先要做的是在Windows平台上运行godoc,或者通过更改GOOS
和GOARCH
的值在任何平台上运行。
例如,以下命令在Linux shell中运行,使godoc相信它在Windows上运行,从而记录相应的文件:
export GOOS=windows
export GOARCH=amd64
godoc -http=:8080
在浏览器中访问http://localhost:8080/pkg/syscall/将显示Windows的syscall API文档。
快速搜索在MSDN上发现了一个有趣的函数,即GetPhysicallyInstalledSystemMemory
(参见https://msdn.microsoft.com/en-us/library/windows/desktop/cc300158(v=vs.85).aspx)。
显然,这个函数在Windows的Go syscall包中不存在,因此无法直接调用它。
由于MSDN页面显示该函数存在于kernel32.dll
中,因此存在一种解决方案(https://github.com/golang/go/wiki/WindowsDLLs),不涉及与C的接口。
将该技术调整为此函数,我们得到以下代码:
//+build windows
package main
import (
"fmt"
"syscall"
"unsafe"
)
func main() {
var mod = syscall.NewLazyDLL("kernel32.dll")
var proc = mod.NewProc("GetPhysicallyInstalledSystemMemory")
var mem uint64
ret, _, err := proc.Call(uintptr(unsafe.Pointer(&mem)))
fmt.Printf("Ret: %d, err: %v, Physical memory: %d\n", ret, err, mem)
}
运行时,输出如下:
Ret: 1, err: L'opération a réussi., Physical memory: 16777216
该值以千字节为单位,因此除以1048576(1024*1024)以获得以千兆字节为单位的值。
英文:
The official online godoc for the syscall package at https://golang.org/pkg/syscall/ seems to document Linux APIs so it is somewhat hard to find the resources online.
First thing to do is to run godoc on the Windows platform, or on any platform by changing the values of GOOS
and GOARCH
.
For example, the following commands run in a Linux shell allow godoc to believe it runs on Windows, and therefore document the corresponding files:
export GOOS=windows
export GOARCH=amd64
godoc -http=:8080
Accessing http://localhost:8080/pkg/syscall/ in a browser shows the Windows syscall API docs.
A quick search reveals an interesting function on MSDN, namely GetPhysicallyInstalledSystemMemory
(see https://msdn.microsoft.com/en-us/library/windows/desktop/cc300158(v=vs.85).aspx).
Apparently, this function does not exist in the Windows Go syscall package, so calling it directly is not possible.
Since the MSDN page shows that this function is present in kernel32.dll
a solution given by this page (https://github.com/golang/go/wiki/WindowsDLLs) exists, that does not involve interfacing with C.
Adapting the technique to this function gives us the following code:
//+build windows
package main
import (
"fmt"
"syscall"
"unsafe"
)
func main() {
var mod = syscall.NewLazyDLL("kernel32.dll")
var proc = mod.NewProc("GetPhysicallyInstalledSystemMemory")
var mem uint64
ret, _, err := proc.Call(uintptr(unsafe.Pointer(&mem)))
fmt.Printf("Ret: %d, err: %v, Physical memory: %d\n", ret, err, mem)
}
When run, this outputs:
> Ret: 1, err: L’opération a réussi., Physical memory: 16777216
The value is given in kilobytes, so divide by 1048576 (1024*1024) to obtain a value in gigabytes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论