Changing the computer name with Inno Setup

拜拜、爱过 提交于 2019-12-04 19:17:09

You should call SetComputerName API function.

Also as the computer name change is valid only after the computer restarts, you should set AlwaysRestart directive to yes to make the installer restart the computer after the installation.

[Setup]
AlwaysRestart=yes

[Code]

function SetComputerName(lpComputerName: PAnsiChar): BOOL;
    external 'SetComputerNameA@kernel32.dll stdcall';

procedure CurStepChanged(CurStep: TSetupStep);
begin
  if CurStep = ssInstall then
  begin
    Log('Changing computer name');
    if SetComputerName('MyNewName') then
    begin
      Log('Computer name changed');
    end
      else
    begin
      Log('Failed to change computer name - ' + SysErrorMessage(DLLGetLastError));
    end;
  end;
end;

Tested on Unicode version of Inno Setup (the only version as of Inno Setup 6).


Changing registry key might work too. Just you should use ComputerName key (not ActiveComputerName) and restart.

I believe your syntax is correct otherwise. Though I didn't test it. However note that maximal computer name length is 15 characters (so MyNewComputerName is too long).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!