본문으로 건너뛰기

현재 윈도가 32비트인지 64비트 인지 확인

Go언어는 개발하는 컴퓨터의 OS나 아키택처 상관 없이 다양한 플랫폼을 대상으로 실행파일 빌드가 가능합니다만. runtime 패키지의 GOARCH나 GOOS 등은 컴파일시에 해당 바이너리가 타겟으로 잡은 정보가 나올뿐 실제 현 시스템에 대한 정보를 대변하진 않습니다.

예를들어 64비트 윈도에서 32비트 실행파일을 실행이 가능한데 runtime.GOARCH 정보를 보면 386 으로 뜨게 되지요..

이런 경우 실제 OS의 CPU 아키텍처 정보를 얻으려면 시스템 정보를 얻어와야 합니다.

이와 관련한 윈도 API 바인딩 함수들 입니다.

Go
package main

import (
	"errors"
	"syscall"
	"unsafe"
)

var _ unsafe.Pointer

var (
	modkernel32 = syscall.NewLazyDLL("kernel32.dll")

	procGetNativeSystemInfo = modkernel32.NewProc("GetNativeSystemInfo")
)

func _GetNativeSystemInfo(systemInfo *SystemInfo) {
	syscall.Syscall(procGetNativeSystemInfo.Addr(), 1, uintptr(unsafe.Pointer(systemInfo)), 0, 0)
	return
}

type ProcessorArchitecture uint16
type ProcessorType uint32

type SystemInfo struct {
	ProcessorArchitecture     ProcessorArchitecture
	Reserved                  uint16
	PageSize                  uint32
	MinimumApplicationAddress uintptr
	MaximumApplicationAddress uintptr
	ActiveProcessorMask       uint64
	NumberOfProcessors        uint32
	ProcessorType             ProcessorType
	AllocationGranularity     uint32
	ProcessorLevel            uint16
	ProcessorRevision         uint16
}

// List of processor architectures associated with SystemInfo.
const (
	ProcessorArchitectureAMD64   ProcessorArchitecture = 9
	ProcessorArchitectureARM     ProcessorArchitecture = 5
	ProcessorArchitectureARM64   ProcessorArchitecture = 12
	ProcessorArchitectureIA64    ProcessorArchitecture = 6
	ProcessorArchitectureIntel   ProcessorArchitecture = 0
	ProcessorArchitectureUnknown ProcessorArchitecture = 0xFFFF
)

// List of processor types associated with SystemInfo.
const (
	ProcessorTypeIntel386     ProcessorType = 386
	ProcessorTypeIntel486     ProcessorType = 486
	ProcessorTypeIntelPentium ProcessorType = 586
	ProcessorTypeIntelIA64    ProcessorType = 2200
	ProcessorTypeAMDX8664     ProcessorType = 8664
)

func GetNativeSystemInfo() (SystemInfo, error) {
	var systemInfo SystemInfo

	if procGetNativeSystemInfo == nil {
		return systemInfo, errors.New("GetNativeSystemInfo not found!!")
	}

	_GetNativeSystemInfo(&systemInfo)
	return systemInfo, nil
}

func is64BitWindow() (bool, error) {
	sysinfo, err := GetNativeSystemInfo()

	if err == nil {
		if sysinfo.ProcessorArchitecture == ProcessorArchitectureAMD64 {
			return true, nil
		}
		return false, nil
	}

	return false, err
}

func is32BitWindow() (bool, error) {
	sysinfo, err := GetNativeSystemInfo()

	if err == nil {
		if sysinfo.ProcessorArchitecture == ProcessorArchitectureIntel {
			return true, nil
		}
		return false, nil
	}

	return false, err
}