Alain
Alain

Reputation: 410

How to get the full computer name in Inno Setup

I would like to know how to get the Full computer name in Inno Setup, for example Win8-CL01.cpx.local in the following image.

windows System computer Name

I already know how to get the computer name with GetComputerNameString but I also would like to have the Domain name of the computer. How can I get this full computer name or this domain name ?

Upvotes: 4

Views: 4154

Answers (3)

TheHunsra
TheHunsra

Reputation: 1

To resolve the null-termination problem left over with TLama's solution, you can use the Inno setup Copy function to copy the correct number of characters without the null terminator.

So, in the original answer, replace:

Result := GetComputerNameEx(Format, Output, BufLen);

with:

if (Boolean(GetComputerNameEx(Format, Output, BufLen)) then
begin
    Result := Copy(Output, 1, BufLen);
end;

Upvotes: 0

RobeN
RobeN

Reputation: 5456

With built-in functions you can get the full name this way:

[Code]
procedure InitializeWizard;
begin
    MsgBox(GetComputerNameString + '.' + GetEnv('UserDnsDomain'), mbInformation, MB_OK);
end;

Although TLama's solution gives you wider possibilities of further development.

Upvotes: 4

TLama
TLama

Reputation: 76683

There's no built-in function for this in Inno Setup. You can use the GetComputerNameEx Windows API function:

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program

[Code]
#ifdef UNICODE
  #define AW "W"
#else
  #define AW "A"
#endif

const
  ERROR_MORE_DATA = 234;

type
  TComputerNameFormat = (
    ComputerNameNetBIOS,
    ComputerNameDnsHostname,
    ComputerNameDnsDomain,
    ComputerNameDnsFullyQualified,
    ComputerNamePhysicalNetBIOS,
    ComputerNamePhysicalDnsHostname,
    ComputerNamePhysicalDnsDomain,
    ComputerNamePhysicalDnsFullyQualified,
    ComputerNameMax
  );

function GetComputerNameEx(NameType: TComputerNameFormat; lpBuffer: string; var nSize: DWORD): BOOL;
  external 'GetComputerNameEx{#AW}@kernel32.dll stdcall';

function TryGetComputerName(Format: TComputerNameFormat; out Output: string): Boolean;
var
  BufLen: DWORD;
begin
  Result := False;
  BufLen := 0;
  if not Boolean(GetComputerNameEx(Format, '', BufLen)) and (DLLGetLastError = ERROR_MORE_DATA) then
  begin
    SetLength(Output, BufLen);
    Result := GetComputerNameEx(Format, Output, BufLen);
  end;    
end;

procedure InitializeWizard;
var
  Name: string;
begin
  if TryGetComputerName(ComputerNameDnsFullyQualified, Name) then
    MsgBox(Name, mbInformation, MB_OK);
end;

Upvotes: 6

Related Questions