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 6 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
16 changes: 16 additions & 0 deletions src/crystal/system/win32/hostname.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require "c/sysinfoapi"

module Crystal::System
def self.hostname
retry_wstr_buffer do |buffer, small_buf|
name_size = buffer.size.to_u
neatorobito marked this conversation as resolved.
Show resolved Hide resolved
if LibC.GetComputerNameExW(LibC::COMPUTER_NAME_FORMAT::ComputerNameDnsHostname, buffer, pointerof(name_size)) != 0
break String.from_utf16(buffer[0, name_size])
elsif small_buf && name_size > 0
next name_size
else
raise WinError.new("GetComputerNameExW")
end
end
end
end
45 changes: 45 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,45 @@
require "c/winnt"
require "c/win_def"
require "c/int_safe"

lib LibC
fun GetNativeSystemInfo(system_info : SYSTEM_INFO*)

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 : Void*
lpMaximumApplicationAddress : Void*
dwActiveProcessorMask : DWORD*
dwNumberOfProcessors : DWORD
dwProcessorType : DWORD
dwAllocationGranularity : DWORD
wProcessorLevel : WORD
wProcessorRevision : WORD
end

fun GetComputerNameExW(computer_name_format : COMPUTER_NAME_FORMAT,
machine_name : LPWSTR,
machine_name_size : DWORD*) : BOOLEAN

enum COMPUTER_NAME_FORMAT
ComputerNameNetBIOS
ComputerNameDnsHostname
neatorobito marked this conversation as resolved.
Show resolved Hide resolved
ComputerNameDnsDomain
ComputerNameDnsFullyQualified
ComputerNamePhysicalNetBIOS
ComputerNamePhysicalDnsHostname
ComputerNamePhysicalDomain
ComputerNamePhysicalDnsFullyQualified
end
end