use windows API GetHostNameW instead of gethostname

This commit is contained in:
eagleliang 2021-04-21 11:47:38 +08:00
parent 3d7631b4b4
commit e9c524aa4a

View File

@ -1664,11 +1664,9 @@ int uv_os_unsetenv(const char* name) {
int uv_os_gethostname(char* buffer, size_t* size) { int uv_os_gethostname(char* buffer, size_t* size) {
char buf[UV_MAXHOSTNAMESIZE]; WCHAR* buf;
size_t len; size_t len;
char* utf8_str; char* utf8_str;
WCHAR* utf16_str;
int utf16_str_buf_size;
int convert_result; int convert_result;
if (buffer == NULL || size == NULL || *size == 0) if (buffer == NULL || size == NULL || *size == 0)
@ -1676,26 +1674,18 @@ int uv_os_gethostname(char* buffer, size_t* size) {
uv__once_init(); /* Initialize winsock */ uv__once_init(); /* Initialize winsock */
if (gethostname(buf, sizeof(buf)) != 0) buf = uv__malloc(sizeof(WCHAR) * UV_MAXHOSTNAMESIZE);
if (buf == NULL)
return UV_ENOMEM;
if (GetHostNameW(buf, UV_MAXHOSTNAMESIZE) != 0)
return uv_translate_sys_error(WSAGetLastError()); return uv_translate_sys_error(WSAGetLastError());
buf[sizeof(buf) - 1] = '\0'; /* Null terminate, just to be safe. */ convert_result = uv__convert_utf16_to_utf8(buf, -1, &utf8_str);
utf16_str_buf_size = MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0); uv__free(buf);
utf16_str = uv__malloc(sizeof(WCHAR) * utf16_str_buf_size);
if (utf16_str == NULL) { if (utf8_str == NULL)
*size = 0;
return UV_ENOMEM; return UV_ENOMEM;
}
MultiByteToWideChar(CP_ACP, 0, buf, -1, utf16_str, utf16_str_buf_size);
convert_result = uv__convert_utf16_to_utf8(utf16_str, utf16_str_buf_size, &utf8_str);
uv__free(utf16_str);
if (utf8_str == NULL) {
*size = 0;
return UV_ENOMEM;
}
if (convert_result != 0) { if (convert_result != 0) {
uv__free(utf8_str); uv__free(utf8_str);