問題描述
編譯為 x86 時檢測 OS x86 或 x64 (Detect OS x86 or x64, when compiled as x86)
我開發了引導軟件來開始我的遊戲。我用 Go 做到了這一點。跨平台對我來說尤其重要。另外,我不想將下載鏈接分成兩個 x86 / x64。我想在一個輸出中處理所有內容。這就是為什麼我必須編譯到 x86。當我這樣做時,我無法正確檢測到操作系統是 x86 還是 x64。
在編譯為 x86 的軟件中,如何正確檢測操作系統 x86 或 x64(在 Go 中)。
此代碼編譯為 x86 時不正確。
const is64Bit = uint64(^uintptr(0)) == ^uint64(0)
參考解法
方法 1:
On Windows you can call IsWow64Process
to determine whether you are a 32‑bit process running on a 64‑bit OS. Note that it returns false if you are a 64‑bit process running on a 64‑bit OS, but if you have a 64‑bit process running, then you know the OS is 64‑bit or it wouldn't run.
Also note that 32‑bit Windows is considered obsolete. Microsoft is already phasing out support for 32‑bit Windows ‑ they no longer want it to be installed on new computers.
On Linux you can call uname
and look at the machine
field. Here's a list of possible values. Note that most of them won't be compatible with your program, only i386
, i686
and x86_64
.
(by fladon、user253751)