Page 1 of 1

Get a list of scanners/printers

PostPosted: Sat May 19, 2018 3:54 am
by Lena
Tell me, please, how to get a list of scanners/printers that sees the computer.
Thank you.

Re: Get a list of scanners/printers

PostPosted: Sat May 19, 2018 5:21 pm
by HsiaLin
Code: Select all
void __fastcall TForm1::FormCreate(TObject *Sender)
{

    DWORD flags = PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS;
    int level = 2;
    PRINTER_INFO_2* printerInfo;
    DWORD memsize, printer_count;

    EnumPrinters(flags, NULL, level, NULL, 0, &memsize, &printer_count);
    if (memsize < 1) return;

    BYTE* bytes = new BYTE[memsize];
    if (EnumPrinters(flags, NULL, level, bytes, memsize, &memsize, &printer_count))
    {
        printerInfo = (PRINTER_INFO_2*)bytes;
        for (UINT i = 0; i < printer_count; i++)
        {
            ShowMessage(printerInfo->pPrinterName);
            ShowMessage(printerInfo->pDriverName);
            printerInfo++;
        }
    }
    delete[] bytes;
}

Re: Get a list of scanners/printers

PostPosted: Sun May 20, 2018 5:14 am
by Lena
Thank You very much HsiaLin!

P.S.
I collect all the devices in ComboBox.
How to send a document for scanning after selecting in ComboBox?
Thanks.

Re: Get a list of scanners/printers

PostPosted: Sun May 20, 2018 8:14 am
by HsiaLin
Lena wrote:I collect all the devices in ComboBox.
How to send a document for scanning after selecting in ComboBox?
Thanks.


If you are wanting to scan pictures or documents you will most likely have to use
TWAIN services. I dont know of any other way. TWAIN is highly complicated unless
you use a 3rd party component.

Re: Get a list of scanners/printers

PostPosted: Sun May 20, 2018 9:18 am
by HsiaLin
After looking into it, an easier way to get printer names is just do it this way.
Saves a lot of code.

Code: Select all
     int x = Printer()->Printers->Count;
     TStringList *S = (TStringList*)Printer()->Printers;

     for(int t=0; t < x; ++t)
     {
        ComboBox1->Items->Add(S->Strings[t]);
     }

     S->Clear();

Re: Get a list of scanners/printers

PostPosted: Mon May 21, 2018 12:31 am
by Lena
Thank You very much!

Re: Get a list of scanners/printers

PostPosted: Tue May 22, 2018 1:29 pm
by rlebeau
HsiaLin wrote:After looking into it, an easier way to get printer names is just do it this way.
Saves a lot of code.


You don't need the type-cast to TStringList:

Code: Select all
TStrings *S = Printer()->Printers;
int x = S->Count;
for(int t = 0; t < x; ++t)
{
    ComboBox1->Items->Add(S->Strings[t]);
}


This is even simpler:

Code: Select all
ComboBox1->Items->AddStrings(Printer()->Printers);