Folks,
I have tried everything, and searched everywhere. I cannot find a definitive
answer to how to add a printer port to XP and W2K. I have seen some posts on
the web say to use AddPortEx and others to use XcvData. Neither are defined
in Delphi, so both must be used via the LoadLibrary method to ensure they
exist before calling.
My problem is, firstly which to use. I have a sample program written using
AddPortEx, but it is not working (fails on the line that adds the port).
This is my code thus far:
unit UPorts;
interface
uses
SysUtils, Dialogs, Types, Windows, Classes, WinSpool;
procedure AddPrinterPort(Handle: HWnd; Instance: HInst; CommandLine:
PChar; ShowWindow: Integer); stdcall; export;
implementation
{$WARN SYMBOL_PLATFORM OFF}
procedure ListPorts(var PortList: TStringList);
var
i, bn, r: DWORD;
pi, ps: PPortInfo1;
begin
{ check list object }
if not Assigned(PortList) then
Exit;
{ get count of ports }
EnumPorts(nil, 1, nil, 0, bn, r);
{ reserve memory for port list }
GetMem(pi, bn);
try
{ process port list }
Win32Check(EnumPorts(nil, 1, pi, bn, bn, r));
ps := pi;
for i := 0 to (r - 1) do
begin
PortList.Add(ps^.pName);
Inc(ps);
end;
{ free reserved memory }
finally
FreeMem(pi);
end;
end;
procedure AddPrinterPort(Handle: HWnd; Instance: HInst; CommandLine: PChar;
ShowWindow: Integer);
var
pl: TStringList;
fn: function(pName: PChar; pLevel: Integer; lpBuffer: Pointer;
pMonitorName: PChar): Integer;
i: Integer;
lh: THandle;
pi: PPortInfo1;
pn: String;
begin
try
{ check for valid port name }
if (Trim(CommandLine) = '') then
Exit;
{ check for existing port name }
pl := TStringList.Create;
try
ListPorts(pl);
if (pl.IndexOf(CommandLine) >= 0) then
Exit;
finally
pl.Free;
end;
{ add new port }
lh := LoadLibrary('winspool.drv');
if (lh >= 32) then
begin
@fn := GetProcAddress(lh, 'AddPortExA');
if (@fn <> nil) then
begin
GetMem(pi, SizeOf(pi));
try
ZeroMemory(pi, SizeOf(pi));
pn := String(CommandLine);
pi^.pName := PChar(pn + #0);
i := fn(nil, 1, @pi, 'Local Port');
if (i = 0) then
SysErrorMessage(GetLastError);
finally
FreeMem(pi);
end;
end;
FreeLibrary(lh);
end;
{ handle all errors }
except
on e: Exception do
ShowMessage(e.Message);
end;
end;
end.
In the line 'i := fn(nil, 1, @pi, 'Local Port');' I have tried @pi and pi,
as I don't fully understand pointers.
Any assistance gratefully accepted, or if you know of someone that might be
able to help. I am really desperate at this stage and I have a client
waiting on the answer.
Thanks,
Darren
_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi