inno-setupwmi-querypascalscript

List all physical printers using WMI query in Inno Setup


I'm trying to get the list of physical printer's name, that are connected to Windows, based on an answer from Query available RAM in Inno Setup.

But just get: "Send To OneNote 16".

Here is my query:

Query := 'SELECT Name FROM Win32_Printer';
Printer := WbemQuery(WbemServices, Query);
if not VarIsNull(Printer) then
begin
  Log(Format('Printers=%s', [Printer.Name]));
end;

Solution

  • You have to iterate the result set:

    var
      Query: string;
      WbemLocator, WbemServices, WbemObjectSet: Variant;
      Printer: Variant;
      I: Integer;
    begin
      WbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
      WbemServices := WbemLocator.ConnectServer('.', 'root\CIMV2');
      Query := 'SELECT Name FROM Win32_Printer';
      WbemObjectSet := WbemServices.ExecQuery(Query);
      if not VarIsNull(WbemObjectSet) and (WbemObjectSet.Count > 0) then
      begin
        for I := 0 to WbemObjectSet.Count - 1 do
        begin
          Printer := WbemObjectSet.ItemIndex(I);
          if not VarIsNull(Printer) then
          begin
            Log(Printer.Name);
          end;
        end;
      end;
    end;
    

    The code requires Unicode version of Inno Setup (the only version as of Inno Setup 6) for a better Variant support.


    Actually, you can see this code in the same question, where you took the WbemQuery from:
    Is there a way to read the system's information in Inno Setup

    Note how the Win32_NetworkAdapterConfiguration is iterated there.