Consultor Eletrônico



Kbase 19311: How to call WIN32 API Function: EnumPrintersA
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   4/6/2011
Status: Verified

GOAL:

How to call WIN32 API Function: EnumPrintersA

GOAL:

How to get information on local printers using the Windows Print Spooler API from ABL / 4GL

FACT(s) (Environment):

Progress 9.x
OpenEdge 10.x
Windows

FIX:

The following sample code shows how to call the Win32 API function called EnumPrintersA. This function returns a list of available local printers.

This code has been tested on Windows NT 4.0 Workstation and Windows 95.

/* DEFINE the widget handle for the window */
DEFINE VARIABLE C-Win AS WIDGET-HANDLE NO-UNDO.

/* Definitions of the field level widgets */
DEFINE BUTTON cmdFind
LABEL "&Find"
SIZE 15 BY 1.14.

DEFINE VARIABLE lstPrinter AS CHARACTER
VIEW-AS SELECTION-LIST SINGLE SCROLLBAR-VERTICAL
SIZE 54 BY 8.57
FGCOLOR 1 FONT 6 NO-UNDO.

/* ************************ Definitions ************************ */
PROCEDURE EnumPrintersA EXTERNAL "winspool.drv":
DEFINE RETURN PARAMETER res AS LONG.
DEFINE INPUT PARAMETER flags AS LONG.
DEFINE INPUT PARAMETER name AS CHARACTER.
DEFINE INPUT PARAMETER Level AS LONG.
DEFINE INPUT-OUTPUT PARAMETER pPrinterEnum AS MEMPTR.
DEFINE INPUT PARAMETER cdBuf AS LONG.
DEFINE OUTPUT PARAMETER pcbNeeded AS LONG.
DEFINE OUTPUT PARAMETER pcReturned AS LONG.
END PROCEDURE.

/* ********************** Frame Definitions ********************* */
DEFINE FRAME frmMain
lstPrinter AT ROW 1.95 COL 4 NO-LABEL
cmdFind AT ROW 11 COL 43
WITH 1 DOWN NO-BOX KEEP-TAB-ORDER OVERLAY
SIDE-LABELS NO-UNDERLINE THREE-D
AT COL 1 ROW 1
SIZE 62 BY 11.62.

/* ******************** Triggers ********************************/
ON CHOOSE OF cmdFind IN FRAME frmMain /* Find */
DO:
DEFINE VARIABLE pPrinterInfo AS MEMPTR NO-UNDO.
DEFINE VARIABLE lpPrinterName AS MEMPTR NO-UNDO.
DEFINE VARIABLE pPrinterEnum AS MEMPTR NO-UNDO.

DEFINE VARIABLE i AS INTEGER NO-UNDO.
DEFINE VARIABLE res AS INTEGER NO-UNDO.
DEFINE VARIABLE pcbNeeded AS INTEGER NO-UNDO.
DEFINE VARIABLE pcReturned AS INTEGER NO-UNDO.

/* initial value */
/* Just want to know the correct size of the pPrinterEnum MEMPTR */
SET-SIZE(pPrinterEnum)= 1208.

RUN EnumPrintersA(
OUTPUT res,
INPUT 2,
INPUT "",
INPUT 2,
INPUT-OUTPUT pPrinterEnum,
INPUT GET-SIZE(pPrinterEnum),
OUTPUT pcbNeeded,
OUTPUT pcReturned). /* Number of printers */

/* Get the size of the pPrinterEnum MEMPTR */
SET-SIZE(pPrinterEnum)= 0.
SET-SIZE(pPrinterEnum)= pcbNeeded.

RUN EnumPrintersA(
OUTPUT res,
INPUT 2,
INPUT "",
INPUT 2,
INPUT-OUTPUT pPrinterEnum,
INPUT GET-SIZE(pPrinterEnum),
OUTPUT pcbNeeded,
. OUTPUT pcReturned). /* Number of printers */

/* Printer list */
DO i = 0 TO pcReturned - 1 :
SET-POINTER-VALUE(pPrinterInfo) = GET-POINTER-VALUE(pPrinterEnum) + (i * 84 ).
SET-POINTER-VALUE(lpPrinterName) = GET-LONG(pPrinterInfo,5).
lstPrinter:insert(string(GET-STRING(lpPrinterName,1)),1).
END.

/* Clean Up */
SET-SIZE(pPrinterEnum) = 0.
SET-SIZE(pPrinterInfo) = 0.
SET-SIZE(lpPrinterName) = 0.
END.

DO ON ERROR Undo, LEAVE
ON END-KEY UNDO, LEAVE:
DISPLAY cmdFind lstPrinter WITH FRAME frmMain.
ENABLE cmdFind lstPrinter WITH FRAME frmMain.
END.

WAIT-FOR CLOSE OF THIS-PROCEDURE..