Skip to content

Commit 187895c

Browse files
authored
os: use GetComputerNameW to retrieve hostname on Windows (#9861)
1 parent dee4904 commit 187895c

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

vlib/builtin/cfns.c.v

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ fn C.SetHandleInformation(hObject voidptr, dwMask u32, dw_flags u32) bool
215215

216216
fn C.ExpandEnvironmentStringsW(lpSrc &u16, lpDst &u16, nSize u32) u32
217217

218+
fn C.GetComputerNameW(&u16, &u32) bool
219+
218220
[trusted]
219221
fn C.SendMessageTimeout() u32
220222

vlib/os/os_windows.c.v

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,13 @@ pub fn uname() Uname {
390390
}
391391

392392
pub fn hostname() string {
393-
// TODO: use C.GetComputerName(&u16, u32) int instead
394-
return execute('cmd /c hostname').output
393+
hostname := [255]u16{}
394+
size := u32(255)
395+
res := C.GetComputerNameW(&hostname[0], &size)
396+
if !res {
397+
return error(get_error_msg(int(C.GetLastError())))
398+
}
399+
return unsafe { string_from_wide(&hostname[0]) }
395400
}
396401

397402
pub fn loginname() string {

0 commit comments

Comments
 (0)