?
Solved

EnumPrintProcessors in Delphi

Posted on 2004-04-25
7
Medium Priority
?
454 Views
Last Modified: 2010-04-05
I have studied Microsoft WinDDK and found that if I want to track detail of print job from each client (win98, XP, 2000) then I have two choices to choose
       1. Writing print monitor (Language or port monitor)
       2. Writing print processor
However writing print monitor in Delphi is a mad process. It has difficulty in translate some header file which is easier in MS VC++. So I choose the PrintProcessor. In all approach I have to study Printer Language like PCL5, PCL5e, PCL6 etc and use information found in RAW data file to count printed page. I begin studying from PrintProcessor Installer program and some related functions. And I have problem in getting result from EnumPrintProcessors API function
in case of there are more than one processor. This is my function:

procedure TForm1.MyEnumPrintProcessors(AServer: string);
var pInfo : Pointer;
    cbBuf, pcbNeeded, pcbReturned : Cardinal;
    i : integer;
begin
    cbBuf := 128;
    try
        GetMem(pInfo,cbBuf);
        if EnumPrintProcessors(PChar(AServer),nil,1,pInfo, cbBuf,pcbNeeded,pcbReturned) then
        begin
            Memo1.Lines.Add('EnumprintProcessors Succeeded - buffer size needed is '+IntToStr(pcbNeeded));
            Memo1.Lines.Add('EnumprintProcessors Succeeded - number of PrintProcessors '+inttostr(pcbReturned));
            for i := 1 to pcbReturned do
            begin
                Memo1.Lines.Add(' Printprocessor #'+IntToStr(i)+' named "'+string(pInfo^));
                //Inc(,pcbNeeded);
            end;

        end
        else
            Memo1.Lines.Add('Failed');

    finally
        FreeMem(pInfo);
    end;
end;

In my case pcbReturned is 2 which is the number of print processor installed. The problem is how can I get the second string from pInfo pointer.

soapsiam
0
Comment
Question by:soapsiam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
7 Comments
 
LVL 1

Author Comment

by:soapsiam
ID: 10916323
I have tried this form of program it works. Howerver, it has to fix number of array member, Is there anyway better than this? I think 50 point is worth for this solution.

procedure TForm1.MyEnumPrintProcessors(AServer: string);
var
    pServerName : PChar;
    pInfo : array[1..255] of TPrintProcessorInfo1;
    pcbNeeded, pcbReturned : Cardinal;
    i : integer;
begin
    try
        if AServer = '' then
            pServerName := nil
        else
            pServerName := PChar(AServer);
        if EnumPrintProcessors(pServerName,nil,1,@pInfo, SizeOf(pInfo),pcbNeeded,pcbReturned) then
        begin
            Memo1.Lines.Add('EnumprintProcessors Succeeded - buffer size needed is '+IntToStr(pcbNeeded));
            Memo1.Lines.Add('EnumprintProcessors Succeeded - number of PrintProcessors '+inttostr(pcbReturned));
            for i := 1 to pcbReturned do
            begin
                Memo1.Lines.Add(' Printprocessor #'+IntToStr(i)+' named "'+string(pInfo[i].pName)+'"');
            end;
        end
        else
            Memo1.Lines.Add('Failed');
    finally
    end;
end;
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10916340

  Parametes pInfo :
      Points to an ARRAY of PRINTPROCESSOR_INFO_1 structures. One structure is created for each available print processor.

  string(pInfo^) - this if what you need, but the address is the address of the first element. The index starts from 0

  string(pInfo[1]^) - should be the second one.
0
 
LVL 1

Author Comment

by:soapsiam
ID: 10925312
That is not correct as in Delphi pInfo is Pointer. However in MS SDK it specifies that pInfo points to array of printprocessorinfo1. It may be point to string separated by NULL (#0).
I think I have to check this.
0
 
LVL 1

Author Comment

by:soapsiam
ID: 10926056
I found the solution it is:

unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, WinSpool, Printers;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure MyEnumPrintProcessors(pServer: PChar);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function IncPtr(P:Pointer;Delta:Integer=1):Pointer;register;
asm
   add   eax, edx
end;

procedure TForm1.MyEnumPrintProcessors(pServer: PChar);
var pInfo : Pointer;
    pcbNeeded, pcbReturned : Cardinal;
    i : integer;
    pProcessInfo : PPrintProcessorInfo1;
begin
    EnumPrintProcessors(nil,nil,1,pInfo, 0, pcbNeeded, pcbReturned);
    if (GetLastError <> ERROR_INSUFFICIENT_BUFFER) then exit;
    Memo1.Lines.Add('EnumprintProcessors Succeeded - buffer size needed is '+IntToStr(pcbNeeded));
    GetMem(pInfo, pcbNeeded);
    try
        if EnumPrintProcessors(nil,nil,1,pInfo, pcbNeeded,pcbNeeded,pcbReturned) then
        begin
            Memo1.Lines.Add('EnumprintProcessors Succeeded - number of PrintProcessors installed is '+inttostr(pcbReturned));
            for i := 0 to pcbReturned-1 do
            begin
                pProcessInfo := IncPtr(pInfo,i*SizeOf(TPrintProcessorInfo1));
                Memo1.Lines.Add(' Printprocessor #'+IntToStr(i)+' named "'+string(pProcessInfo.pName)+'"');
            end;
        end
        else
        begin
            Memo1.Lines.Add('Failed');
            RaiseLastOSError;
        end

    finally
        FreeMem(pInfo);
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
    MyEnumPrintProcessors(nil);
end;

end.
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 10966956
Closed, 50 points refunded.

modulo
Community Support Moderator
Experts Exchange
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month10 days, 17 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question