Forum Discussion

nastya_e's avatar
nastya_e
Occasional Contributor
11 years ago

Get the default printer

Добрый день.

Как  определить принтер, который в настоящий момент используется по умолчанию. Написание тестов на delphiscript



Locator: = Sys.OleObject ('WbemScripting.SWbemLocator');

WmiService: = Locator.ConnectServer ('.', 'root\cimv2', '', '');



если затем использовать

objPrinter := WMIService.Get('Win32_Printer.Default=True'), то выдает ошибку 

SWbemServicesEx



если выводить с помощью запроса 

WbemObject := WmiService.ExecQuery('Select * from Win32_Printer Where Default = True');

то затем не знаю как получить результат запроса

  • In the meantime you can try using the WbemObjectSet methods to enumerate the list:



      WbemObjectSet := WmiService.ExecQuery('Select * from Win32_Printer Where Default = True');

      count := WbemObjectSet.Count;

      for i := 0 to count - 1 do

      begin

        WbemObject := WbemObjectSet.ItemIndex(i);

        Log.Message(WbemObject.Name);

      end;

  • In the meantime you can try using the WbemObjectSet methods to enumerate the list:



      WbemObjectSet := WmiService.ExecQuery('Select * from Win32_Printer Where Default = True');

      count := WbemObjectSet.Count;

      for i := 0 to count - 1 do

      begin

        WbemObject := WbemObjectSet.ItemIndex(i);

        Log.Message(WbemObject.Name);

      end;

  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)

    Добрый день, Настя.


     


     


    Вот этот код должен делать то, что вам надо. Для того, чтобы другие члены нашего сообщества тоже могли использовать этот скрипт, я сделаю описание на английском. 


     


     


    The code below demonstrates how to get the default printer on the computer. It uses the Windows Management Instrumentation technology. Please read the "Working With WMI Objects in Scripts" article to learn how to use it in the script.


     




    procedure GetDefaultPrinter();


    const wbemImpersonationLevelImpersonate = 3;


    var WmiService, Locator, ComputerName;


    var WbemObject, enumPrinters;


    begin


     


      ComputerName := '.';


      Locator := Sys.OleObject ('WbemScripting.SWbemLocator');


      Locator.Security_.ImpersonationLevel := wbemImpersonationLevelImpersonate;


      WmiService := Locator.ConnectServer (ComputerName, 'root\cimv2');


      


      WbemObject := WmiService.ExecQuery('Select * from Win32_Printer Where Default = True');


      enumPrinters := Enumerator(WbemObject);


      while not enumPrinters.AtEnd do


      begin


        objPrinter := enumPrinters.Item;


        Log.Message(objPrinter.Name);


        enumPrinters.MoveNext;


      end;


    end;



  • nastya_e's avatar
    nastya_e
    Occasional Contributor
    Спасибо за ответ.

    Но у меня после выполнения теста в лог ничего не падает, тест просто проходит как успешный.



    результат у запроса Select * from Win32_Printer Where Default = True есть, т.к. если в коде прописать

    if WbemObject.Count>0 then Log.Message('Установлен принтер по умолчанию'), то в лог попадает сообщение.

  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)
    Hi Nastya,

     


    It looks like it's only me here who can read Russian :) That's why, we recommend using English for discussions.


     


    As for your question, it looks like there is some issue with the Enumerator function in DelphiScript in TC 10.30. In TestComplete 10.40 (I used this test version), everything works fine.


    Do you use the WMI object in your test? This object is implemented in TestComplete's script extension. I suggest that you modify this extension and add the new GetDefaultPrinter function there. Once you do it, you can call the WMI.GetDefaultPrinter method to get the info about the default printer. Here is the VBScript code for this (the extension is written in this script language):


     




    Function GetDefaultPrinter


     


      Set oWMI = GetObject("winmgmts:root/cimv2")


      Set res = oWMI.ExecQuery("Select * from Win32_Printer Where Default = True")


      For Each objPrinter in res


        Log.Message(objPrinter.Name)


      Next


    End Function




     


    Here are some helper resources about script extensions:


     -- the "Script Extensions" article


     -- the TestComplete Extensions Workshop recorded webinar


     


    Please let us know if you have anymore questions.


     

  • nastya_e's avatar
    nastya_e
    Occasional Contributor
    Joseph Michaud, thank you! Your answer helped me ))



    Tanya Gorbunova, I badly speak English ))

    Your version answer is not for Delphi script.