Mega Code Archive

 
Categories / Delphi / Printing
 

Finding Printers in the network

Title: Finding Printers in the network Question: Find the printers for a server specific on the network Answer: First: insert on uses the unit name "winspool" and "printers". After this you can write this code using the windows API EnumPrinters: The parameter psName is the server in the network and the result is TStrings. You can use this in a combo or a listbox. function TForm1.GetPrintServers(psName: string): TStrings; var lpBuffer,lpPrinterInfo: PChar; lcCount,lcNumCount : DWord ; liCount,liAux:integer; lsAuxNome:TStrings; function GetContexts ( pctipo : cardinal; ppName : PChar ):TStringlist; var liCont : integer; lsNome:string; lbPode:boolean; begin lbPode:=false; Result:=TStringlist.Create; case pcTipo of 8: lbPode := EnumPrinters(pctipo,ppName,1,PByte(lpBuffer),lcCount,lcCount, lcNumCount); 16: lbPode := EnumPrinters(pctipo,nil,1,PByte(lpBuffer),lcCount,lcCount, lcNumCount); end; if not lbPode then Exit; lpPrinterInfo := lpBuffer; for licont := 0 to lcNumCount - 1 do begin with PPrinterInfo1(lpPrinterInfo)^ do begin if pcTipo = 8 then begin liAux:=Pos(',',pDescription); lsNome := copy(pDescription,liAux+1,Length(pDescription)); Result.Add(lsNome); end else begin liAux := Pos(ppName,pName); lsNome := copy(pName,liAux,Length(pName)); if lsNome = ppName then begin Result.Add(string(pName)); Exit; end; end; end; Inc(lpPrinterInfo, sizeof(TPrinterInfo1)); end; end; begin lsAuxNome:=TStrings.Create; lcCount := 0; EnumPrinters(PRINTER_ENUM_REMOTE,nil,1,nil,0,lcCount,lcNumCount); GetMem(lpBuffer,lcCount); lsAuxNome := GetContexts(16,PChar(psName)); if lsAuxNome.Count 0 then Result:=GetContexts(8,PChar(lsAuxNome.Strings[0])) else begin lsAuxNome.Add('No Encontrada'); Result:=lsAuxNome; end; end;