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

vohkndzv  于 2023-03-18  发布在  其他
关注(0)|答案(3)|浏览(240)

在我的电脑上(1)我有一个兄弟DCP打印机。它是一个网络打印机。我运行的是XP(sp3)上的 Delphi 7企业版。
现在我想 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属性工作正常。
我也试过“wnetenumresource”,但似乎不能正常工作。
我怎样才能找到网络打印机?

cbwuti44

cbwuti441#

我不是很确定你要找的是什么,但是这给出了所有可能的类型。注意,我已经删除了“默认”限制-这将只返回一台打印机。

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站点。

e0bqpujr

e0bqpujr2#

你的问题...我的打印机是网络打印机吗?很容易查出来。检查一下你的打印机型号末尾是否有字母N。例如,N=网络,DN双工和网络。

rslzwgfq

rslzwgfq3#

也许这段来自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;

相关问题