Link to home
Create AccountLog in
Delphi

Delphi

--

Questions

--

Followers

Top Experts

Avatar of Buropro-Citation
Buropro-Citation🇨🇦

getting printer trays
Hi everyone, I was using the code sample below in earlier version of Delphi to get the trays associated with a specific printer.    This code doesn't work under Delphi in some case I'm getting an access violation error and my application will not work until I'm shutting it down.

What I would need is a correction to this code ot any code that would let me get a lit of all the tray name from a printer.

I kinow the error is exactly on the getmem, I think delphi XE have different ways to manage caracters strings.

Thank you for any help on this matter.
GetMem(p,24*DeviceCapabilities(pchar(PName),nil,DC_BINNAMES,nil,nil));
  for i:=1 to DeviceCapabilities(pchar(PName),nil,DC_BINNAMES,p,nil) do
  begin
    ComboName.items.add(p+24*(i-1));
  end;
  FreeMem(p);

Open in new window

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of ThommyThommy🇩🇪

Please check, if this helps you...

http://www.swissdelphicenter.ch/de/showcode.php?id=796


ASKER CERTIFIED SOLUTION
Avatar of ThommyThommy🇩🇪

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of ThommyThommy🇩🇪

To finally change printer bin try following function...

http://www.efg2.com/Lab/Library/UseNet/1999/1102.txt

Avatar of Buropro-CitationBuropro-Citation🇨🇦

ASKER

It works perfectly, there was a slight error in the code but it was easy to correct, thank you Thommy :)

Here is the corrected code:
procedure TfMAin.GetPaperBins(sl: TStrings; PrinterName: String);
type
  TBinName = array [0..23] of Char;
  TBinNameArray = array [1..High(Integer) div SizeOf(TBinName)] of TBinName;
  PBinnameArray = ^TBinNameArray;
  TBinArray = array [1..High(Integer) div SizeOf(Word)] of Word;
  PBinArray = ^TBinArray;
var
  Device, Driver, Port: array [0..255] of Char;
  hDevMode: THandle;
  i, numBinNames, numBins, temp: Integer;
  pBinNames: PBinnameArray;
  pBins: PBinArray;
begin
  //set specified printer
  for numBins := 0 to Printer.Printers.Count -1 do begin
      if Printer.Printers [numBins] = PrinterName then
        Printer.PrinterIndex := numBins ;
  end;
  //original Code
    Printer.GetPrinter(Device, Driver, Port, hDevmode);
  numBinNames := WinSpool.DeviceCapabilities(Device, Port, DC_BINNAMES, nil, nil);
  numBins     := WinSpool.DeviceCapabilities(Device, Port, DC_BINS, nil, nil);
  if numBins <> numBinNames then
  begin
    raise Exception.Create('DeviceCapabilities reports different number of bins and bin names!');
  end;
  if numBinNames > 0 then
  begin
    pBins := nil;
    GetMem(pBinNames, numBinNames * SizeOf(TBinname));
    GetMem(pBins, numBins * SizeOf(Word));
    try
      WinSpool.DeviceCapabilities(Device, Port, DC_BINNAMES, PChar(pBinNames), nil);
      WinSpool.DeviceCapabilities(Device, Port, DC_BINS, PChar(pBins), nil);
      sl.Clear;
      for i := 1 to numBinNames do
      begin
        temp := pBins^[i];
        sl.addObject(pBinNames^[i], TObject(temp));
      end;
    finally
      FreeMem(pBinNames);
      if pBins <> nil then
        FreeMem(pBins);
    end;
  end;
end;

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.

Delphi

Delphi

--

Questions

--

Followers

Top Experts

Delphi is the most powerful Object Pascal IDE and component library for cross-platform Native App Development with flexible Cloud services and broad IoT connectivity. It provides powerful VCL controls for Windows 10 and enables FMX development for Windows, Mac and Mobile. Delphi is your choice for ultrafast Enterprise Strong Developmentâ„¢. Look for increased memory for large projects, extended multi-monitor support, improved Object Inspector and much more. Delphi is 5x faster for development and deployment across multiple desktop, mobile, cloud and database platforms including 32-bit and 64-bit Windows 10.