getComputerName method
Implementation
@visibleForTesting
String getComputerName() {
// We call this a first time to get the length of the string in characters,
// so we can allocate sufficient memory.
final nSize = calloc<DWORD>();
GetComputerNameEx(ComputerNameDnsFullyQualified, nullptr, nSize);
// Now allocate memory for a native string and call this a second time.
final lpBuffer = wsalloc(nSize.value);
try {
final result =
GetComputerNameEx(ComputerNameDnsFullyQualified, lpBuffer, nSize);
if (result != 0) {
return lpBuffer.toDartString();
} else {
developer.log('Failed to get computer name', error: GetLastError());
return "";
}
} finally {
free(lpBuffer);
free(nSize);
}
}