Author Topic: [Solved] Trimmed computer name  (Read 325 times)

artem101

  • Full Member
  • ***
  • Posts: 149
[Solved] Trimmed computer name
« on: November 04, 2026, 04:14:52 pm »
Code: Pascal  [Select][+][-]
  1. function GetLocalComputerName1: string;
  2. var
  3.   Size: dword;
  4.   Buf: array [0..MAX_COMPUTERNAME_LENGTH + 1] of char;
  5.   Res: Boolean;
  6. begin
  7.   Size := MAX_COMPUTERNAME_LENGTH + 1;
  8.   Res := GetComputerName(@Buf, Size);
  9.   if Res and (Size > 0) then
  10.     Result := Buf
  11.   else
  12.     Result := '';
  13. end;
  14.  
  15. function GetLocalComputerName2: string;
  16. const
  17.   COMP_ENV_VAR: UnicodeString = 'COMPUTERNAME';
  18. begin
  19.   Result := SysUtils.GetEnvironmentVariable(COMP_ENV_VAR);
  20.   Result := LazUTF8.{UTF8ToWinCP}SysToUtf8(Result);
  21. end;    

Both methods return computer name trimmed up to 15 characters. Is there a way in pascal to get full computer name?
« Last Edit: November 08, 2026, 04:38:59 pm by artem101 »

Bart

  • Hero Member
  • *****
  • Posts: 5800
    • Bart en Mariska's Webstek
Re: Trimmed computer name
« Reply #1 on: November 04, 2026, 04:52:11 pm »
Windows GetComputerName retruns the NetBIOS computername, with a maximum length of 15 characters.

You might want to take a look at GetComputerNameEx.

Bart

Fibonacci

  • Hero Member
  • *****
  • Posts: 1126
  • Behold, I bring salvation - Unleashed Pascal
    • fibo.gg
Re: Trimmed computer name
« Reply #2 on: November 04, 2026, 06:22:44 pm »
Code: Pascal  [Select][+][-]
  1. function GetLocalComputerName3: string;
  2. var
  3.   d: dword;
  4. begin
  5.   GetComputerName(nil, @d);
  6.   setlength(result, d);
  7.   GetComputerName(@result[1], @d);
  8. end;
Unleashed Pascal: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub ⭐

artem101

  • Full Member
  • ***
  • Posts: 149
Re: Trimmed computer name
« Reply #3 on: November 04, 2026, 06:43:12 pm »
Code: Pascal  [Select][+][-]
  1. function GetLocalComputerName3: string;
  2. var
  3.   d: dword;
  4. begin
  5.   GetComputerName(nil, @d);
  6.   setlength(result, d);
  7.   GetComputerName(@result[1], @d);
  8. end;
this returns trimmed

Fibonacci

  • Hero Member
  • *****
  • Posts: 1126
  • Behold, I bring salvation - Unleashed Pascal
    • fibo.gg
Re: Trimmed computer name
« Reply #4 on: November 04, 2026, 06:53:11 pm »
Code: Pascal  [Select][+][-]
  1. function GetLocalComputerName4: string;
  2. var
  3.   d: dword;
  4.   b: unicodestring;
  5. begin
  6.   d := 0;
  7.   GetComputerNameExW(ComputerNamePhysicalDnsHostname, nil, @d);
  8.   SetLength(b, d);
  9.   result := '';
  10.   if (d > 0) and GetComputerNameExW(ComputerNamePhysicalDnsHostname, @b[1], @d) then result := UTF8Encode(b);
  11. end;
Unleashed Pascal: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub ⭐

artem101

  • Full Member
  • ***
  • Posts: 149
Re: Trimmed computer name
« Reply #5 on: November 08, 2026, 04:38:28 pm »
GetLocalComputerName4 - This works! thanks

 

TinyPortal © 2005-2018