title: print spooler apis in delphi
body: <p>I'm trying to list the jobs on a printer.
  I try to call enumjobs a first time to get the size of the buffer i need to pass as a parameter in a second call (as it is advised in microsoft documentation)
  But, when calling enumjobs, i keep getting a descriptor not valid error with the enumjobs api.</p>
  
  <p>What the f... am i doing wrong ? !</p>
  
  <pre><code>procedure showLastError();
  var
  pErrorText:pchar;
  lastError:integer;
  begin
     lastError := GetLastError();
     pErrorText := nil;
  
     if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM or FORMAT_MESSAGE_ALLOCATE_BUFFER
                   ,nil,lastError,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),@pErrorText,0,nil)&lt;&gt; 0) then
     begin
      showmessage(pErrorText);
     end;
  end;
  
  procedure TForm3.Button1Click(Sender: TObject);
  var
  handlePrinter:NativeUInt;
  pJob:pointer;
  sizePJob:integer;
  pcbNeeded:cardinal;
  pcReturned:cardinal;
  cByteNeeded,cByteUsed:cardinal;
  
  i:integer;
  pJobInfo: PJobInfo1;
  temp:integer;
  ret:boolean;
  pPrinterInfo:PPrinterInfo2W;
  PPRINTER_INFO_1 : PRINTER_INFO_1;
  
  begin
   handlePrinter := 0;
   if not OpenPrinter(nil,handlePrinter,nil) then
   begin
    showLastError();
    exit;
   end;
   if not GetPrinter(handlePrinter,3,nil,0,@cByteNeeded)then
   begin
    if (GetLastError() &lt;&gt; ERROR_INSUFFICIENT_BUFFER) then
    begin
     showLastError();
     exit;
    end;
   end;
   pPrinterInfo := allocMem(  cByteNeeded);
   if not GetPrinter(handlePrinter,3,pPrinterInfo,cByteNeeded,@cByteUsed)then
   begin
    if (GetLastError() &lt;&gt; ERROR_INSUFFICIENT_BUFFER) then
    begin
     showLastError();
     FreeMem(pPrinterInfo);
     exit;
    end;
   end;
  
   ret := EnumJobs(handlePrinter,0,pPrinterInfo.cJobs,2,nil,0,&amp;pcbNeeded,&amp;pcReturned);
   if (not ret)then
   begin
    if (GetLastError() &lt;&gt; ERROR_INSUFFICIENT_BUFFER) then
    begin
     showLastError();
     FreeMem(pPrinterInfo);
     exit;
    end;
   end;
   FreeMem(pPrinterInfo);
  end;
  </code></pre>