Skip to content

Commit b2f1ac3

Browse files
committed
fix(windows): use builtin bindings
1 parent 5ed4d86 commit b2f1ac3

1 file changed

Lines changed: 9 additions & 45 deletions

File tree

src/dns/servers/windows.cr

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class Socket
1515
end
1616
end
1717

18+
require "crystal/system/win32/windows_registry"
19+
1820
class DNS::Servers
1921
@[Link("iphlpapi")]
2022
lib IpHlpApi
@@ -59,58 +61,20 @@ class DNS::Servers
5961
fun GetAdaptersAddresses(family : LibC::ULong, flags : LibC::DWORD, reserved : Void*, addresses : IP_ADAPTER_ADDRESSES*, size : UInt32*) : LibC::DWORD
6062
end
6163

62-
@[Link("advapi32")]
63-
lib Advapi32
64-
# Registry constants
65-
HKEY_LOCAL_MACHINE = Pointer(Void).new(0x80000002_u64)
66-
KEY_READ = 0x20019_u32
67-
ERROR_SUCCESS = 0_u32
68-
69-
alias HKEY = Void*
70-
71-
fun RegOpenKeyExA(hKey : HKEY, lpSubKey : UInt8*, ulOptions : LibC::DWORD, samDesired : LibC::DWORD, phkResult : HKEY*) : LibC::Long
72-
fun RegQueryValueExA(hKey : HKEY, lpValueName : UInt8*, lpReserved : LibC::DWORD*, lpType : LibC::DWORD*, lpData : UInt8*, lpcbData : LibC::DWORD*) : LibC::Long
73-
fun RegCloseKey(hKey : HKEY) : LibC::Long
74-
end
75-
7664
# Read DNS search list from Windows registry
7765
private def self.read_search_list_from_registry : Array(String)
7866
search = [] of String
79-
key : Advapi32::HKEY = Pointer(Void).null
8067

8168
# Open the TCPIP Parameters key
82-
reg_path = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters"
83-
result = Advapi32.RegOpenKeyExA(
84-
Advapi32::HKEY_LOCAL_MACHINE,
85-
reg_path.to_unsafe,
86-
0_u32,
87-
Advapi32::KEY_READ,
88-
pointerof(key)
89-
)
90-
91-
return search unless result == Advapi32::ERROR_SUCCESS
92-
93-
# Try to read SearchList value (comma-separated list of domains)
94-
buffer = Bytes.new(2048)
95-
buffer_size = buffer.size.to_u32
96-
97-
result = Advapi32.RegQueryValueExA(
98-
key,
99-
"SearchList".to_unsafe,
100-
nil,
101-
nil,
102-
buffer.to_unsafe,
103-
pointerof(buffer_size)
104-
)
105-
106-
if result == Advapi32::ERROR_SUCCESS && buffer_size > 0
107-
# SearchList is a comma-separated string
108-
end_of_string = buffer.index(0_u8) || buffer_size.to_i
109-
search_string = String.new(buffer[0...end_of_string])
110-
search = search_string.split(',').map(&.strip).reject(&.empty?)
69+
reg_path = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters".to_utf16
70+
Crystal::System::WindowsRegistry.open?(LibC::HKEY_LOCAL_MACHINE, reg_path) do |key|
71+
# Try to read SearchList value (comma-separated list of domains)
72+
value_name = "SearchList".to_utf16
73+
if search_string = Crystal::System::WindowsRegistry.get_string(key, value_name)
74+
search = search_string.split(',').map(&.strip).reject(&.empty?)
75+
end
11176
end
11277

113-
Advapi32.RegCloseKey(key)
11478
search
11579
rescue
11680
[] of String

0 commit comments

Comments
 (0)