Query list of Windows accounts in Inno Setup

久未见 提交于 2021-01-01 06:43:52

问题


In my Inno Setup project I need to allow the user to choose an account from the list of all local accounts on a custom page. The selected account will be used to install a service with custom credential. How can I make this?

Thank you in advance!


回答1:


You can use WMI Win32_UserAccount class to query the list of accounts.

[Run]
Filename: sc.exe; Parameters: ... {code:GetAccount}
[Code]

var
  AccountPage: TInputOptionWizardPage;

procedure InitializeWizard();
var
  WMIService: Variant;
  WbemLocator: Variant;
  WbemObjectSet: Variant;
  I: Integer;
begin
  Log('InitializeWizard');
  AccountPage := CreateInputOptionPage(
    wpSelectTasks, 'Service account', '', 'Please select account for the service:',
    True, True);

  WbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  WMIService := WbemLocator.ConnectServer('localhost', 'root\CIMV2');
  WbemObjectSet :=
    WMIService.ExecQuery('SELECT * FROM Win32_UserAccount');
  if not VarIsNull(WbemObjectSet) then
  begin
    for I := 0 to WbemObjectSet.Count - 1 do
    begin
      AccountPage.Add(WbemObjectSet.ItemIndex(I).Caption);
    end;
    AccountPage.SelectedValueIndex := 0;
  end;
end;

function GetAccount(Param: string): string;
var
  I: Integer;
begin
  for I := 0 to AccountPage.CheckListBox.Items.Count - 1 do
  begin
    if AccountPage.Values[I] then Result := AccountPage.CheckListBox.Items[I];
  end;
end;


Related questions:

  • Inno Setup Create individual shortcuts on all desktops of all users
  • Inno Setup to create a user in Windows


来源:https://stackoverflow.com/questions/65249995/query-list-of-windows-accounts-in-inno-setup

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