Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement System for Win32 #6972

Merged
merged 14 commits into from
Dec 17, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/crystal/system/win32/cpucount.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "c/ntsysteminfo"

module Crystal::System
def self.cpu_count
LibC.NTGetSystemInfo(out system_info)
system_info.dwNumberOfProcessors
end
end
12 changes: 12 additions & 0 deletions src/crystal/system/win32/hostname.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require "c/ntsysteminfo"

module Crystal::System
def self.hostname
name_size = LibC::NT_COMPUTER_NAME_SIZE.to_u32
unless LibC.NTGetComputerNameExA(LibC::ComputerNameFormat::ComputerNameDnsHostname, out machine_name, pointerof(name_size))
raise Errno.new("Failed to get machine hostname.")
end
name = String.new machine_name.to_unsafe
name
end
end
1 change: 1 addition & 0 deletions src/lib_c/x86_64-windows-msvc/c/int_safe.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
lib LibC
alias DWORD = UInt32
alias DWORD_PTR = UInt32*
neatorobito marked this conversation as resolved.
Show resolved Hide resolved
end
54 changes: 54 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/ntsysteminfo.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require "c/winnt"
require "c/win_def"
require "c/int_safe"

lib LibC
fun NTGetSystemInfo = GetSystemInfo(system_info : SystemInfo*)
neatorobito marked this conversation as resolved.
Show resolved Hide resolved

PROCESSOR_ARCHITECTURE_AMD64 = 9
PROCESSOR_ARCHITECTURE_ARM = 5
PROCESSOR_ARCHITECTURE_ARM64 = 12
PROCESSOR_ARCHITECTURE_IA64 = 6
PROCESSOR_ARCHITECTURE_INTEL = 0
PROCESSOR_ARCHITECTURE_UNKNOWN = 0xffff

struct ProcessorInfo
wProcessorArchitecture : WORD
wReserved : WORD
end

union OEMProcessorInfo
dwOemId : DWORD
processorInfo : ProcessorInfo
end

struct SystemInfo
neatorobito marked this conversation as resolved.
Show resolved Hide resolved
oemProcessorInfo : OEMProcessorInfo
dwPageSize : DWORD
lpMinimumApplicationAddress : LPVOID
lpMaximumApplicationAddress : LPVOID
dwActiveProcessorMask : DWORD_PTR
dwNumberOfProcessors : DWORD
dwProcessorType : DWORD
dwAllocationGranularity : DWORD
wProcessorLevel : WORD
wProcessorRevision : WORD
end

fun NTGetComputerNameExA = GetComputerNameExA(computer_name_format : ComputerNameFormat,
neatorobito marked this conversation as resolved.
Show resolved Hide resolved
machine_name : CHAR[NT_COMPUTER_NAME_SIZE]*,
machine_name_size_ptr : DWORD_PTR) : BOOLEAN

NT_COMPUTER_NAME_SIZE = 256

enum ComputerNameFormat
neatorobito marked this conversation as resolved.
Show resolved Hide resolved
ComputerNameNetBIOS
ComputerNameDnsHostname
ComputerNameDnsDomain
ComputerNameDnsFullyQualified
ComputerNamePhysicalNetBIOS
ComputerNamePhysicalDnsHostname
ComputerNamePhysicalDomain
ComputerNamePhysicalDnsFullyQualified
end
end
1 change: 1 addition & 0 deletions src/lib_c/x86_64-windows-msvc/c/winnt.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ lib LibC
alias LPWSTR = WCHAR*
alias LPWCH = WCHAR*

alias LPVOID = Void*
neatorobito marked this conversation as resolved.
Show resolved Hide resolved
alias HANDLE = Void*

INVALID_FILE_ATTRIBUTES = DWORD.new(-1)
Expand Down