Get all network shared printers on a domain

I am trying to get EnumPrinters to return all printers on a domain (or a local network).

var
   pcbNeeded, pcReturned: DWORD;
   Buffer, PrinterInfo: PChar;
   I: integer;
begin
   EnumPrinters(PRINTER_ENUM_REMOTE or PRINTER_ENUM_SHARED, nil, 1, nil, 0, pcbNeeded, pcReturned);
   GetMem(Buffer, pcbNeeded);
   try
      EnumPrinters(PRINTER_ENUM_REMOTE or PRINTER_ENUM_SHARED, nil, 1, PByte(Buffer), pcbNeeded, pcbNeeded, pcReturned);
      PrinterInfo := Buffer;
      for I := 0 to pcReturned -1 do
         with PPrinterInfo1(PrinterInfo)^ do
         begin
            ListBox1.Items.Add(StrPas(pName));
            Inc(PrinterInfo, SizeOf(TPrinterInfo1));
         end;
   finally
      FreeMem(Buffer);
   end;
end;

As far as the documentation goes, EnumPrinters function (Winspool.h) - Win32 apps | Microsoft Learn, this should work. But it returns nothing. If I replace PRINTER_ENUM_REMOTE with PRINTER_ENUM_LOCAL I get SOME of the local printers.

Anyone tried this successfully before?

Try PRINTER_ENUM_REMOTE alone.

Alex

It “works” this way, but the result is weird, not an actual printer…

Alex

Thanks, I have tried all combinations, alone or in combination. I know there are shared printers on my network because Windows can see and connect to them.

Did you right click your app and select ‘Run As Administrator’. Its also possible this feature is depreciated and now requires you to use a network discovery api. I’m only guessing here so could be wrong.

Yes, run as administrator makes no difference. I suspect it’s more to do with the newer Windows security, probably works fine on XP. Maybe I can get all my clients to downgrade? :slight_smile:

Are you actually on a domain?

No, but I have built a domain server in a VM to try. Will see how that goes over the next few days.

~WRD0622.jpg