Newsgroup: it.comp.lang.delphi
Titolo: Una procedura che funziona ma da problemi

Questa procedura funziona, nel senso che mi rileva il valore corretto, solo
che alla fine mi solleva una eccezzione e mi blocca il programma. Dove
sbaglio?
Poi perchè la variabile "TxtMsg" la devo dichiarare in modo globale? se la
dichiaro nella procedura stessa mi segnala un altro problema.
Grazie a tutti in anticipo.
USES WinSpool;
...
PROCEDURE TForm1.Button1Click(Sender: TObject);
      VAR Pri_2      : TPrinterInfo2;
          PrnDevMode : TDEVMODE;
          FHandle    : THandle;
          VDW        : DWORD;
          FDevice    : PChar;
          FDriver    : PChar;
          FPort      : PChar;
PROCEDURE Libera;
    BEGIN
          If FDevice <> Nil Then FreeMem(FDevice,255);
          If FDriver <> Nil Then FreeMem(FDriver,255);
          If FPort <> Nil Then FreeMem(FPort,255);
    END;
    BEGIN
          GetMem(FDevice,255);
          GetMem(FDriver,255);
          GetMem(FPort,255);
          Printer.GetPrinter(FDevice,FDriver,FPort,FHandle);
          If FDevice='' Then//stampante non installata
             Begin
             Libera;
             Exit;
             End;
          OpenPrinter(FDevice,FHandle,Nil);
          Pri_2.pDevMode := @PrnDevMode;
          If Not GetPrinter(FHandle,2,@Pri_2,SizeOf(Pri_2),@VDW) Then
             If Not GetPrinter(FHandle,2,@Pri_2,VDW,@VDW) Then
                Begin
                Libera;
                ShowMessage(SysErrorMessage(GetLastError));
                Exit;
                End;
          Case Pri_2.pDevMode.dmPaperSize Of
            DMPAPER_LETTER : TxtMsg := 'Letter, 8 1/2- by 11-inches';
            DMPAPER_LEGAL : TxtMsg := 'Legal, 8 1/2- by 14-inches';
            DMPAPER_A4         : TxtMsg := 'A4 Sheet, 210- by
297-millimeters';
            DMPAPER_CSHEET : TxtMsg := 'C Sheet, 17- by 22-inches';
            DMPAPER_DSHEET : TxtMsg := 'D Sheet, 22- by 34-inches';
            DMPAPER_ESHEET : TxtMsg := 'E Sheet, 34- by 44-inches';
            DMPAPER_LETTERSMALL : TxtMsg := 'Letter Small, 8 1/2- by
11-inches';
            DMPAPER_TABLOID : TxtMsg := 'Tabloid, 11- by 17-inches';
            DMPAPER_LEDGER : TxtMsg := 'Ledger, 17- by 11-inches';
            DMPAPER_STATEMENT : TxtMsg := 'Statement, 5 1/2- by 8
1/2-inches';
            DMPAPER_EXECUTIVE : TxtMsg := 'Executive, 7 1/4- by 10
1/2-inches';
            DMPAPER_A3      : TxtMsg := 'A3 sheet, 297- by 420-millimeters';
            DMPAPER_A4SMALL : TxtMsg := 'A4 small sheet, 210- by
297-millimeters';
            DMPAPER_A5 : TxtMsg := 'A5 sheet, 148- by 210-millimeters';
            DMPAPER_B4 : TxtMsg := 'B4 sheet, 250- by 354-millimeters';
            DMPAPER_B5 : TxtMsg := 'B5 sheet, 182- by 257-millimeter paper';
            DMPAPER_FOLIO : TxtMsg := 'Folio, 8 1/2- by 13-inch paper';
            DMPAPER_QUARTO : TxtMsg := 'Quarto, 215- by 275-millimeter
paper';
            DMPAPER_10X14 : TxtMsg := '10- by 14-inch sheet';
            DMPAPER_11X17 : TxtMsg := '11- by 17-inch sheet';
            DMPAPER_NOTE : TxtMsg := 'Note, 8 1/2- by 11-inches';
            DMPAPER_ENV_9 : TxtMsg := '#9 Envelope, 3 7/8- by 8 7/8-inches';
            DMPAPER_ENV_10 : TxtMsg := '#10 Envelope, 4 1/8- by 9
1/2-inches';
            DMPAPER_ENV_11 : TxtMsg := '#11 Envelope, 4 1/2- by 10
3/8-inches';
            DMPAPER_ENV_12 : TxtMsg := '#12 Envelope, 4 3/4- by 11-inches';
            DMPAPER_ENV_14 : TxtMsg := '#14 Envelope, 5- by 11 1/2-inches';
            DMPAPER_ENV_DL : TxtMsg := 'DL Envelope, 110- by
220-millimeters';
            DMPAPER_ENV_C5 : TxtMsg := 'C5 Envelope, 162- by
229-millimeters';
            DMPAPER_ENV_C3 : TxtMsg := 'C3 Envelope,  324- by
458-millimeters';
            DMPAPER_ENV_C4 : TxtMsg := 'C4 Envelope,  229- by
324-millimeters';
            DMPAPER_ENV_C6 : TxtMsg := 'C6 Envelope,  114- by
162-millimeters';
            DMPAPER_ENV_C65 : TxtMsg := 'C65 Envelope, 114- by
229-millimeters';
            DMPAPER_ENV_B4 : TxtMsg := 'B4 Envelope,  250- by
353-millimeters';
            DMPAPER_ENV_B5 : TxtMsg := 'B5 Envelope,  176- by
250-millimeters';
            DMPAPER_ENV_B6 : TxtMsg := 'B6 Envelope,  176- by
125-millimeters';
            DMPAPER_ENV_ITALY : TxtMsg := 'Italy Envelope, 110- by
230-millimeters';
            DMPAPER_ENV_MONARCH : TxtMsg := 'Monarch Envelope, 3 7/8- by 7
1/2-inches';
            DMPAPER_ENV_PERSONAL: TxtMsg := '6 3/4 Envelope, 3 5/8- by 6
1/2-inches';
            DMPAPER_FANFOLD_US : TxtMsg := 'US Std Fanfold, 14 7/8- by
11-inches';
            DMPAPER_FANFOLD_STD_GERMAN : TxtMsg := 'German Std Fanfold, 8
1/2- by 12-inches';
            DMPAPER_FANFOLD_LGL_GERMAN  : TxtMsg := 'German Legal Fanfold, 8
1/2- by 13-inches';
          End;
          ShowMessage(TxtMsg);
          Libera;
    END;

Imho...
io la sposterei in una unit, prima di tutto.
La trasformerei in una funzione che riceve come parametro VAR la variable
stringa txtmsg.
Poi, metterei un bel try--finally per essere sicuro di eseguire i freemem.
Infine, non conosco openprinter ne ho molta dimestichezza coi puntatori,
ma non è che ti serve una ClosePrinter?
Usi come handle la variabile FHanlde dichiarata in locale, che viene
distrutta
alla uscita della procedura.....
Bye
A.

Grazie a tutti in anticipo.
Cosě funziona ;)
var
  PrinterInfo2: PPrinterInfo2;  //Uso un puntatore invece di un tipo
  S: Integer;
................
  Prima di GetPrinter:
  S := 0;
  GetMem(PrinterInfo2, S);
  GetPrinter(FHandle,2, Printerinfo, s, @s);
  ReallocMem(PrinterInfo2, S);
  GetPrinter(FHandle,2, Printerinfo, s, @s);
  Ricordati poi di fare FreeMem(PrinterInfo2, S);
Quindi in pratica, cambia la procedure in questo modo;
         GetMem(FDevice,255);
         GetMem(FDriver,255);
         GetMem(FPort,255);
          Printer.GetPrinter(FDevice,FDriver,FPort,FHandle);
          If FDevice='' Then//stampante non installata
             Begin
             Libera;
             Exit;
             End;
          OpenPrinter(FDevice,FHandle,Nil);
         // Pri_2.pDevMode := @PrnDevMode; // questo non serve
         S := 0;
         GetMem(PrinterInfo2, S);
         if not GetPrinter(FHandle,2, Printerinfo, s, @s) then begin
           ReallocMem(PrinterInfo2, S);	
           if not GetPrinter(FHandle,2, Printerinfo, s, @s) then 
                Begin
                  Libera;
                  ShowMessage(SysErrorMessage(GetLastError));
                  FreeMem(PrinterInfo2, S);
                  Exit;
                End;
          Case Pri_2.pDevMode.dmPaperSize Of
          ...............
          FreeMem(PrinterInfo2, S);
Ciao,
Massimo.
-- 
Ore 18.30: premo invio.

          Case Pri_2.pDevMode.dmPaperSize Of
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Logicamente qui mi e' scappato. Il codice è:
case PrinterInfo2.pDevMode.dmPaperSize of


Grazie, finalmente ho capito dove sbagliavo.

)
Make Money Fast (MMF) chain letter or lookalike
Reposting or quoting significant portions of one of the above
Binary posting in non-binary newsgroup
Open Proxy abuse
Retromoderation by request of legitimate group moderator or due
   to charter violation
Forgery canceled by request of forged individual or organization
X-Cancel-ID: VV>BH_D,W`'LAA)JMLFC<0N7TM,I/`PQ'+9"FN;YTZ$[QP(#66P\84SF
The From: address above is not monitored or answered, as it has been
scraped too many times by brain-dead spammers looking for addresses
for UCE.  This address is sent directly to /dev/null.  Mail should be
sent to postmaster at the domain 
stopspam.org
 regarding this cancel
message.