Darren McBride wrote:
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');

fn is already a pointer to a function, and GetProcAddress returns a pointer to the function. Remove the '@' to make it

    fn := GetProcAddress(lh, 'AddPortExA');

      if (@fn <> nil) then

Delphi can sometimes get confused when using pointers to functions. Try this.

    if Assigned(fn) then

      begin
        GetMem(pi, SizeOf(pi));

You are getting to the size of a pointer here (four bytes). What you need is to dereference the pointer.

      GetMem(pi, SizeOf(pi^));

        try
          ZeroMemory(pi, SizeOf(pi));

Same here. Dereference the pointer to get the size of the type it points to.

      ZeroMemory(pi, SizeOf(pi^));

          pn := String(CommandLine);
          pi^.pName := PChar(pn + #0);
          i := fn(nil, 1, @pi, 'Local Port');

Since pi is already a pointer, you do not need to get the address of it. Remove the '@'.

      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.

--
Sly


This message and its attachments may contain legally privileged or confidential 
information. This message is intended for the use of the individual or entity 
to which it is addressed. If you are not the addressee indicated in this 
message, or the employee or agent responsible for delivering the message to the 
intended recipient, you may not copy or deliver this message or its attachments 
to anyone. Rather, you should permanently delete this message and its 
attachments and kindly notify the sender by reply e-mail. Any content of this 
message and its attachments, which does not relate to the official business of 
the sending company must be taken not to have been sent or endorsed by the 
sending company or any of its related entities. No warranty is made that the 
e-mail or attachment(s) are free from computer virus or other defect.
_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi

Reply via email to