List all physical printers using WMI query in Inno Setup

為{幸葍}努か 提交于 2019-12-22 00:45:32

问题


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;

回答1:


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 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.



来源:https://stackoverflow.com/questions/44167016/list-all-physical-printers-using-wmi-query-in-inno-setup

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