mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Fix GetOsDeviceName returns string with trailing \0.
PiperOrigin-RevId: 688673080
This commit is contained in:
committed by
Copybara-Service
parent
00d0a7d3f0
commit
43aa565f14
@@ -53,6 +53,7 @@ cc_library(
|
||||
"//internal/platform:logging",
|
||||
"//internal/platform:uuid",
|
||||
"//internal/platform/implementation:types",
|
||||
"//internal/platform/implementation/windows:string_utils",
|
||||
"//internal/platform/implementation/windows/generated:types",
|
||||
"@com_google_absl//absl/base:core_headers",
|
||||
"@com_google_absl//absl/container:flat_hash_map",
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#include "internal/base/files.h"
|
||||
#include "internal/platform/implementation/device_info.h"
|
||||
#include "internal/platform/implementation/windows/generated/winrt/base.h"
|
||||
#include "internal/platform/implementation/windows/string_utils.h"
|
||||
#include "internal/platform/logging.h"
|
||||
#include "winrt/Windows.Foundation.Collections.h"
|
||||
#include "winrt/Windows.Foundation.h"
|
||||
@@ -43,6 +44,8 @@ using UserType = winrt::Windows::System::UserType;
|
||||
using UserAuthenticationStatus =
|
||||
winrt::Windows::System::UserAuthenticationStatus;
|
||||
|
||||
using ::nearby::windows::string_utils::WideStringToString;
|
||||
|
||||
template <typename T>
|
||||
using IVectorView = winrt::Windows::Foundation::Collections::IVectorView<T>;
|
||||
|
||||
@@ -57,18 +60,20 @@ std::optional<std::string> DeviceInfo::GetOsDeviceName() const {
|
||||
DWORD size = 0;
|
||||
|
||||
// Get length of the computer name.
|
||||
if (!GetComputerNameExW(ComputerNameDnsHostname, nullptr, &size)) {
|
||||
if (GetComputerNameExW(ComputerNameDnsHostname, nullptr, &size) == 0) {
|
||||
if (GetLastError() != ERROR_MORE_DATA) {
|
||||
LOG(ERROR) << ": Failed to get device name size, error:"
|
||||
<< GetLastError();
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
std::wstring device_name(size, L' ');
|
||||
if (GetComputerNameExW(ComputerNameDnsHostname, device_name.data(), &size)) {
|
||||
winrt::hstring device_name_str(device_name);
|
||||
return winrt::to_string(device_name_str);
|
||||
if (GetComputerNameExW(ComputerNameDnsHostname, device_name.data(), &size) !=
|
||||
0) {
|
||||
// On input size includes null termination.
|
||||
// On output size excludes null termination.
|
||||
device_name.resize(size);
|
||||
return WideStringToString(device_name);
|
||||
}
|
||||
|
||||
LOG(ERROR) << ": Failed to get device name, error:" << GetLastError();
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
|
||||
#include "internal/platform/implementation/windows/device_info.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "internal/platform/implementation/device_info.h"
|
||||
@@ -22,8 +24,11 @@ namespace nearby {
|
||||
namespace windows {
|
||||
namespace {
|
||||
|
||||
TEST(DeviceInfo, DISABLED_GetComputerName) {
|
||||
EXPECT_TRUE(DeviceInfo().GetOsDeviceName().has_value());
|
||||
TEST(DeviceInfo, GetComputerName) {
|
||||
ASSERT_TRUE(DeviceInfo().GetOsDeviceName().has_value());
|
||||
std::string device_name = DeviceInfo().GetOsDeviceName().value();
|
||||
// Makes sure device_name does not include terminating null character.
|
||||
EXPECT_EQ(device_name.size(), std::strlen(device_name.data()));
|
||||
}
|
||||
|
||||
TEST(DeviceInfo, DISABLED_GetDeviceType) {
|
||||
|
||||
Reference in New Issue
Block a user