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 3 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/sysinfoapi"

module Crystal::System
def self.cpu_count
LibC.GetNativeSystemInfo(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/sysinfoapi"

module Crystal::System
def self.hostname
name_size = LibC::MAX_COMPUTER_NAME_SIZE.to_u32
unless LibC.GetComputerNameExW(LibC::COMPUTER_NAME_FORMAT::ComputerNameDnsHostname, out machine_name, pointerof(name_size))
neatorobito marked this conversation as resolved.
Show resolved Hide resolved
raise WinError.new("Failed to get machine hostname")
neatorobito marked this conversation as resolved.
Show resolved Hide resolved
end
actual_name = Slice.new(machine_name.to_unsafe, name_size)
String.from_utf16(actual_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/sysinfoapi.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 GetNativeSystemInfo = GetNativeSystemInfo(system_info : SYSTEM_INFO*)

PROCESSOR_ARCHITECTURE_AMD64 = 9
neatorobito marked this conversation as resolved.
Show resolved Hide resolved
PROCESSOR_ARCHITECTURE_ARM = 5
PROCESSOR_ARCHITECTURE_ARM64 = 12
PROCESSOR_ARCHITECTURE_IA64 = 6
PROCESSOR_ARCHITECTURE_INTEL = 0
PROCESSOR_ARCHITECTURE_UNKNOWN = 0xffff

struct PROCESSOR_INFO
wProcessorArchitecture : WORD
wReserved : WORD
end

union OEM_PROCESSOR_INFO
dwOemId : DWORD
processorInfo : PROCESSOR_INFO
end

struct SYSTEM_INFO
oemProcessorInfo : OEM_PROCESSOR_INFO
dwPageSize : DWORD
lpMinimumApplicationAddress : LPVOID
lpMaximumApplicationAddress : LPVOID
dwActiveProcessorMask : DWORD_PTR
dwNumberOfProcessors : DWORD
dwProcessorType : DWORD
dwAllocationGranularity : DWORD
wProcessorLevel : WORD
wProcessorRevision : WORD
end

fun GetComputerNameExW = GetComputerNameExW(computer_name_format : COMPUTER_NAME_FORMAT,
neatorobito marked this conversation as resolved.
Show resolved Hide resolved
machine_name : WCHAR[MAX_COMPUTER_NAME_SIZE]*,
machine_name_size_ptr : DWORD_PTR) : BOOLEAN

MAX_COMPUTER_NAME_SIZE = 255

enum COMPUTER_NAME_FORMAT
ComputerNameNetBIOS
ComputerNameDnsHostname
neatorobito marked this conversation as resolved.
Show resolved Hide resolved
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