getUserName method

  1. @visibleForTesting
String getUserName()

Implementation

@visibleForTesting
String getUserName() {
  const maxLength = 256; // defined as UNLEN in Lmcons.h
  final lpBuffer = wsalloc(maxLength + 1); // allow for terminating null
  final pcbBuffer = calloc<DWORD>()..value = maxLength + 1;
  try {
    final result = GetUserName(lpBuffer, pcbBuffer);
    if (result != 0) {
      return lpBuffer.toDartString();
    } else {
      throw WindowsException(HRESULT_FROM_WIN32(GetLastError()));
    }
  } finally {
    free(pcbBuffer);
    free(lpBuffer);
  }
}