Inno setup 如何在Inno安装程序中获取计算机全名

Inno setup 如何在Inno安装程序中获取计算机全名,inno-setup,Inno Setup,我想知道如何在Inno设置中获取完整的计算机名,例如下图中的Win8-CL01.cpx.local 我已经知道如何获得计算机名,但我也想有计算机的域名。如何获取此计算机全名或域名?Inno安装程序中没有内置此功能。您可以使用Windows API函数: [Setup] AppName=My Program AppVersion=1.5 DefaultDirName={pf}\My Program [Code] #ifdef UNICODE #define AW "W" #else #

我想知道如何在Inno设置中获取完整的计算机名,例如下图中的
Win8-CL01.cpx.local


我已经知道如何获得计算机名,但我也想有计算机的域名。如何获取此计算机全名或域名?

Inno安装程序中没有内置此功能。您可以使用Windows API函数:

[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;

使用内置函数,您可以通过以下方式获取全名:

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

尽管TLama的解决方案为您提供了更广泛的进一步开发的可能性。

您可以通过
GetEnv('UserDnsDomain')
使用DNS检查域,您可以通过
GetEnv('UserDomain')
检查主域,但是我不确定本例中使用的标志。如果它返回的名称与您想要的不同,请尝试使用与
ComputerNameDnsFullyQualified
(我没有要验证的计算机)不同的标志。我已在域中的两台计算机上检查过,结果是肯定的。@RobeN,谢谢!我根据来自的建议选择了这个,但我不确定(尽管,回答这个问题的人来自MS支持团队,所以他们应该是对的)。这个解决方案存在一个问题:返回的字符串以空字符结尾。因此,如果在
格式
函数中使用此字符串,结果将在空字符位置截断。为了删除空字符,可以添加行
SetLength(Output,BufLen)GetComputerNameEx(格式、输出、BufLen)
后的代码>。实际上,BufLen“在输出时,接收复制到目标缓冲区的TCHAR数,不包括终止的空字符”。