我的打印机是网络打印机吗?

时间:2016-12-08 09:11:11

标签: delphi printing

在我的电脑上(1)我有一台Brother DCP打印机。它是网络打印机。 我在XP(sp3)上运行Delphi 7 Enterprise。

现在我想让Delphi告诉我这是一台网络打印机。

我有这段代码:

Procedure TForm1.Button1Click(Sender: TObject); 
begin 
   Coinitialize(NIL); 
   DOLPT2(); 
   // ShowMessage('test6 '); 
   COUninitialize; 
end;

Procedure Tform1.DOLPT2();
begin;
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService   := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2','','');
  FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_Printer Where Default = True','WQL',wbemFlagForwardOnly);
  oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  if oEnum.Next(1, FWbemObject, iValue) = 0 then
     begin
        Listbox1.Items.Add(Format('Name    %s',[String(FWbemObject.Name)]));// String
        Listbox1.Items.Add(' ');
        Listbox1.Items.Add(Format('PRINTER_ATTRIBUTE_NETWORK  %s',[Integer(FWbemObject.PRINTER_ATTRIBUTE_NETWORK)]));// Integer
        Listbox1.Items.Add(Format('PRINTER_ATTRIBUTE_LOCAL    %s',[Integer(FWbemObject.PRINTER_ATTRIBUTE_LOCAL)]));// Integer
        Listbox1.Items.Add(' ');
        FWbemObject := Unassigned;
     end
  ELSE
     BEGIN
        Listbox1.Items.Add(Format('Name    %s',['Ingen printer']));   // String
        Listbox1.Items.Add(' ');
     END;
end; 

(*从RUI复制的代码 - 感谢他*)

我收到错误消息说"方法选择属性不受自动化对象支持"两个属性。

NAME-attribute工作正常。

我试过' wnetenumresource'也是如此,但这似乎无法正常工作。

如何找到NETWORK-printer?

2 个答案:

答案 0 :(得分:-1)

我并不确切地知道你在寻找什么,但这给了所有可能的类型。请注意,我已删除了'默认'限制 - 这只会返回一台打印机。

Procedure Tform2.DOLPT2;
var
  iValue : Cardinal;
const
  wbemFlagForwardOnly = $00000020;
begin;
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService   := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2','','');
  FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_Printer','WQL',wbemFlagForwardOnly);
  oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  if oEnum.Next(1, FWbemObject, iValue) = 0 then
     begin
        Listbox1.Items.Add(Format('Name    %s',[String(FWbemObject.Name)]));// String
        Listbox1.Items.Add(' ');
        if FWbemObject.Shared then Listbox1.Items.Add( 'Shared' );
        if FWbemObject.Network then Listbox1.Items.Add( 'Networked' );
        if FWbemObject.Local then Listbox1.Items.Add( 'Local' );
        Listbox1.Items.Add(' ');
        FWbemObject := Unassigned;
     end
  ELSE
     BEGIN
        Listbox1.Items.Add(Format('Name    %s',['Ingen printer']));   // String
        Listbox1.Items.Add(' ');
     END;
end;

如果您想知道其他哪些字段可用,请使用Google Win32_Printer类并转到Microsoft网站。

答案 1 :(得分:-2)

也许来自StackOverflow的这段代码可以帮到你。 idx_Net_printer是一个常量,用于定义打印机是否为网络打印机。你需要调整代码。我还没有测试过。

procedure TMyForm.RefreshPrinterList;
var
  I: Integer;
  NewItem: TComboExItem;
  PPrinterEnumArray, PLocator: PPrinterInfo2;
  ArraySize, BufferSize: cardinal;
  strBuffer: string;
const
  idx_Default_Net_printer = 0;
  idx_Net_printer = 1;
  idx_Default_Local_printer = 2;
  idx_Local_printer = 3;

begin
  DefaultPrinterName := getDefaultPrinterName;
  cbPrinterList.ItemsEx.Clear;
  // S.G. 4/4/2008: list all other printers
  // S.G. 4/4/2008: Get the necessary buffer size
  ArraySize := 0;
  BufferSize := 0;
  PPrinterEnumArray := nil;
  EnumPrinters(PRINTER_ENUM_LOCAL or PRINTER_ENUM_CONNECTIONS, nil, 2, nil, 0, BufferSize, ArraySize);
  PPrinterEnumArray := AllocMem(BufferSize);
  try
    if EnumPrinters(PRINTER_ENUM_LOCAL or PRINTER_ENUM_CONNECTIONS, nil, 2, PPrinterEnumArray, BufferSize, BufferSize, ArraySize) then
    begin
      PLocator := PPrinterEnumArray;
      if ArraySize > 0 then
      begin
        for I := 0 to ArraySize - 1 do    // Iterate
        begin
          NewItem := cbPrinterList.ItemsEx.Add;
          strBuffer := StrPas(PLocator^.pPrinterName);
          UniqueString(strBuffer); // make sure we have a unique string instance and not a pointer
          NewItem.Caption := strBuffer;

          if AnsiSameText(DefaultPrinterName, strBuffer) then
          begin
            // default printer
            if  (PRINTER_ATTRIBUTE_LOCAL AND PLocator^.Attributes) <> 0 then
            begin
              // Local, default printer
              NewItem.ImageIndex := idx_Default_Local_printer;
            end
            else
            begin
              // Network default printer
              NewItem.ImageIndex := idx_Default_Net_printer;
            end;
            cbPrinterList.ItemIndex := NewItem.Index;
          end
          else
          begin
            // default printer
            if  (PRINTER_ATTRIBUTE_LOCAL AND PLocator^.Attributes) <> 0 then
            begin
              // Local, default printer
              NewItem.ImageIndex := idx_Local_printer;
            end
            else
            begin
              // Network default printer
              NewItem.ImageIndex := idx_Net_printer;
            end;
          end;
          Inc(PLocator);
        end;    // for
      end;
    end;
  finally // wrap up
    FreeMem(PPrinterEnumArray);
  end;    // try/finally
end;