How to find the printer on the network LAN
How to find the printer on the network LAN
First: enter the unit and Winspool Printers in the uses clause.
PsName parameter is the name of the computer in the network and store in TStrings.
We can use the Combo or ListBox. The following snippets of source code it
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('Printer tidak ditemukan');
Result:=lsAuxNome;
end;
end;
An example of it, tambakan TButton components and TListBox. In the event onclick TButton, paste the following code:
procedure TForm1.Button1Click(Sender: TObject);
begin
ListBox1.Items:= GetPrintServers(Edit1.Text);
end;
delphi components see below
First: enter the unit and Winspool Printers in the uses clause.
PsName parameter is the name of the computer in the network and store in TStrings.
We can use the Combo or ListBox. The following snippets of source code it
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('Printer tidak ditemukan');
Result:=lsAuxNome;
end;
end;
An example of it, tambakan TButton components and TListBox. In the event onclick TButton, paste the following code:
procedure TForm1.Button1Click(Sender: TObject);
begin
ListBox1.Items:= GetPrintServers(Edit1.Text);
end;
delphi components see below