How do I get computer printer type with C++ or CSharp ?

Ji Shirley 181 Reputation points
2022-02-17T01:13:56.883+00:00

When my computer is connected to different type of printers, I can see the property pages of different type of printers are different.
For example, the property page of dot-matrix printer is much simpler than laser printer.
My question is how to use C++ or CSharp to get the type of each printer.
I use C# but it can't the type of the printer.
The property "MarkingTechnology" is null when I use the following interface.

        string printerName = "EPSON LQ-635KII ESC/P2";
        string query = string.Format("SELECT * from Win32_Printer WHERE Name LIKE '%{0}'", printerName);

        using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
        using (ManagementObjectCollection coll = searcher.Get())
        {
            try
            {
                foreach (ManagementObject printer in coll)
                {
                    foreach (PropertyData property in printer.Properties)
                    {
                        Console.WriteLine(string.Format("{0}: {1}", property.Name, property.Value));
                    }
                }
            }
            catch (ManagementException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

Is there any other functions I can use? Many thanks.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,836 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,686 questions
0 comments No comments
{count} vote

Accepted answer
  1. YujianYao-MSFT 4,281 Reputation points Microsoft Vendor
    2022-02-17T02:43:44.877+00:00

    Hi @Ji Shirley ,

    It's a pity to say that there is no way to get the result directly. This link uses C# methods to achieve your requirements, this issue uses enum. Regarding the C++ method, I checked the API related to the printer. The related function or structure does not define the variable of the printer type, so I couldn't use C++ to get the printer type. Hope it helps.

    Best regards,

    Elya


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Artemiy Moroz 271 Reputation points
    2022-02-17T16:31:22.493+00:00

    hi there! Although Windows does not provide the API of determining the printer technology, the hack may be to use the DPI ratio of the printer. Matrix printer should have 60 to 90 DPI, whereas newer printers may have 300-600 dpi and even higher. One of the ways to determine the printer DPI is use the GetDeviceCaps function.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.