How to get the printer port number using windows API call

Hi all,

Using the Windows API call, could you please tell us how to get the port number of the printer. Because while setting the value of activeprinter method/property of MS word 2007 it requires "printer name" on "port number".
The port number which we are trying to get by this method is equivalent to 'Session:printer-port"

Thanks,
Ashwath


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Printer port - MS Word

define variable holdPort as character no-undo.

holdPort = " on " + getPrinterPort(pv-printer).

...

ch-WordApplication:ActivePrinter =
pv-printer + holdPort no-error.

...

function getPrinterPort returns character
(pv-printerName as character ):
/*----------------------------------------------------------------------------
Purpose: To find out from the registry what port a given printer is using.
Notes: Microsoft Word requires this as part of the printer name.
----------------------------------------------------------------------------*/
define variable registryData as character no-undo.

load "Software" base-key "HKEY_CURRENT_USER".
use "Software".

get-key-value
section "Microsoft\Windows NT\CurrentVersion\PrinterPorts"
key pv-printerName
value registryData.

unload "Software".
return entry(2,registryData).
end function.


How to get the printer port number using windows API call

Hi,

In the following procedure get-string(lpDevName, get-short(lpDevName,3) + 1)
will give the printer name. Is there a way to get the port number of the printer using this procedure.

/* memory pointers */
DEF VAR p-printdlg AS MEMPTR NO-UNDO.
DEF VAR p-docinfo AS MEMPTR NO-UNDO.
DEF VAR p-docname AS MEMPTR NO-UNDO.

DEF VAR lp-memory AS INT NO-UNDO.
DEFINE VARIABLE r-val AS INTEGER NO-UNDO.
/* devmode structure address constants */
DEFINE VARIABLE c-dmDeviceName AS INTEGER INIT 1 NO-UNDO.
DEFINE VARIABLE c-dmSpecVersion AS INTEGER INIT 33 NO-UNDO.
DEFINE VARIABLE c-dmDriverVersion AS INTEGER INIT 35 NO-UNDO.
DEFINE VARIABLE c-dmSize AS INTEGER INIT 37 NO-UNDO.
DEFINE VARIABLE c-dmDriverExtra AS INTEGER INIT 39 NO-UNDO.
DEFINE VARIABLE c-dmFields AS INTEGER INIT 41 NO-UNDO.
DEFINE VARIABLE c-dmOrientation AS INTEGER INIT 45 NO-UNDO.
DEFINE VARIABLE c-dmPaperSize AS INTEGER INIT 47 NO-UNDO.
DEFINE VARIABLE c-dmPaperLength AS INTEGER INIT 49 NO-UNDO.
DEFINE VARIABLE c-dmPaperWidth AS INTEGER INIT 51 NO-UNDO.
DEFINE VARIABLE c-dmScale AS INTEGER INIT 53 NO-UNDO.
DEFINE VARIABLE c-dmCopies AS INTEGER INIT 55 NO-UNDO.
DEFINE VARIABLE c-dmDefaultSource AS INTEGER INIT 57 NO-UNDO.
DEFINE VARIABLE c-dmPrintQuality AS INTEGER INIT 59 NO-UNDO.
DEFINE VARIABLE c-dmColor AS INTEGER INIT 61 NO-UNDO.
DEFINE VARIABLE c-dmDuplex AS INTEGER INIT 63 NO-UNDO.
DEFINE VARIABLE c-dmYResolution AS INTEGER INIT 65 NO-UNDO.
DEFINE VARIABLE c-dmTTOption AS INTEGER INIT 67 NO-UNDO.
DEFINE VARIABLE c-dmCollate AS INTEGER INIT 69 NO-UNDO.
DEFINE VARIABLE c-dmFormName AS INTEGER INIT 71 NO-UNDO.
DEFINE VARIABLE c-dmlogpixels AS INTEGER INIT 103 NO-UNDO.
DEFINE VARIABLE c-dmBitsPerPel AS INTEGER INIT 104 NO-UNDO.
DEFINE VARIABLE c-dmPelsWidth AS INTEGER INIT 105 NO-UNDO.
DEFINE VARIABLE c-dmPelsHeight AS INTEGER INIT 109 NO-UNDO.
DEFINE VARIABLE c-dmDisplayFlags AS INTEGER INIT 113 NO-UNDO.
DEFINE VARIABLE c-dmDisplayFreq AS INTEGER INIT 117 NO-UNDO.
DEFINE VARIABLE c-dmICMMethod AS INTEGER INIT 121 NO-UNDO.
DEFINE VARIABLE c-dmICMIntent AS INTEGER INIT 125 NO-UNDO.
DEFINE VARIABLE c-dmMediaType AS INTEGER INIT 129 NO-UNDO.
DEFINE VARIABLE c-dmDitherType AS INTEGER INIT 133 NO-UNDO.
DEFINE VARIABLE c-dmReserved1 AS INTEGER INIT 137 NO-UNDO.
DEFINE VARIABLE c-dmReserved2 AS INTEGER INIT 141 NO-UNDO.
DEFINE VARIABLE H-DeviceContext AS INTEGER NO-UNDO.
DEFINE VARIABLE H-Saved-DC AS INTEGER NO-UNDO.
DEFINE VARIABLE H-GetDC AS INTEGER NO-UNDO.
DEFINE VAR lpDevName AS MEMPTR NO-UNDO.
DEFINE VARIABLE R-formlength AS INTEGER INIT 0 NO-UNDO.
DEFINE VARIABLE R-formwidth AS INTEGER INIT 0 NO-UNDO.
DEFINE VARIABLE R-logpixelsx AS INTEGER INIT 0 NO-UNDO.
DEFINE VARIABLE R-logpixelsy AS INTEGER INIT 0 NO-UNDO.
DEFINE VARIABLE R-orientation AS INTEGER INIT 1 NO-UNDO.
/* DEFINE VARIABLE R-val AS INTEGER NO-UNDO. */

SET-SIZE(p-docname) = 0.
SET-SIZE(p-docname) = LENGTH("Report":U) + 1.
PUT-STRING(p-docname, 1) = "Report":U.

SET-SIZE(p-docinfo) = 0.
SET-SIZE(p-docinfo) = 12.
PUT-LONG(p-docinfo, 1) = 12.
PUT-LONG(p-docinfo, 5) = GET-POINTER-VALUE(p-docname).
PUT-LONG(p-docinfo, 9) = 0.

SET-SIZE(p-printdlg) = 0.
SET-SIZE(p-printdlg) = 66.
PUT-LONG(p-printdlg, 1) = 66. /* Structure Size DWORD - 4 Bytes */
PUT-LONG(p-printdlg, 5) = CURRENT-WINDOW:HWND. /* HwndOwner HWnd - 4 Bytes */
PUT-LONG(p-printdlg, 9) = 0. /* HDevMode Handle - 4 Bytes */
PUT-LONG(p-printdlg,13) = 0. /* HDevName Handle - 4 Bytes */
PUT-LONG(p-printdlg,17) = 0. /* HDC Hdc - 4 Bytes */
PUT-LONG(p-printdlg,21) = 256. /* Flags DWORD - 4 Bytes */
PUT-SHORT(p-printdlg,25) = 1. /* nfromPage Word - 2 Bytes */
PUT-SHORT(p-printdlg,27) = 1. /* ntoPage Word - 2 Bytes */
PUT-SHORT(p-printdlg,29) = 0. /* nMinPage Word - 2 Bytes */
PUT-SHORT(p-printdlg,31) = 9999. /* nMaxPage Word - 2 Bytes */
PUT-SHORT(p-printdlg,33) = 1. /* nCopies Word - 2 Bytes */
PUT-LONG(p-printdlg, 35) = 0. /* Hinstance Hinstance - 4 Bytes */
PUT-LONG(p-printdlg, 39) = 0. /* CustData DWORD - 4 Bytes */
PUT-LONG(p-printdlg, 43) = 0. /* lpfnPrintHook LP - 4 Bytes */
PUT-LONG(p-printdlg, 47) = 0. /* lpfnSetupHook LP - 4 Bytes */
PUT-LONG(p-printdlg, 51) = 0. /* lpPrintTemplateName LPCSTR - 4 Bytes */
PUT-LONG(p-printdlg, 55) = 0. /* lpSetupTemplateName LPCSTR - 4 Bytes */
PUT-LONG(p-printdlg, 59) = 0. /* HPrintTemplate Handle - 4 Bytes */
PUT-LONG(p-printdlg, 63) = 0. /* HSetupTemplate Handle - 4 Bytes */

RUN PrintDlgA (INPUT GET-POINTER-VALUE(p-printdlg), OUTPUT r-val).
MESSAGE "r-val" r-val
VIEW-AS ALERT-BOX INFO BUTTONS OK.

IF r-val > 0
THEN DO:

RUN GlobalLock(INPUT GET-LONG(p-printdlg,9),OUTPUT lp-memory).
SET-SIZE(lpDevName) = 0.
SET-SIZE(lpDevName) = 66.
SET-POINTER-VALUE(lpDevName) = lp-memory.
RUN GlobalLock(INPUT GET-LONG(p-printdlg,13),OUTPUT lp-memory).
MESSAGE "lp-memory" lp-memory
VIEW-AS ALERT-BOX INFO BUTTONS OK.
if lp-memory > 0 then
SET-POINTER-VALUE(lpDevName) = lp-memory.

message "Printer is" get-string(lpDevName, get-short(lpDevName,3) + 1) skip
view-as alert-box info button ok.
ASSIGN H-DeviceContext = GET-LONG(p-printdlg,17).

END.
ELSE DO:
ASSIGN
SET-SIZE(p-printdlg) = 0
SET-SIZE(p-docinfo) = 0
SET-SIZE(p-docname) = 0.
RETURN "PRINT-CANCEL":U.
END.

RUN SaveDC(H-DeviceContext, OUTPUT H-Saved-DC).
IF H-Saved-DC = 0
THEN DO:
/* RUN GetLastError(OUTPUT H-Saved-DC). */
MESSAGE "SaveDC:ERROR " H-Saved-DC VIEW-AS ALERT-BOX
TITLE "Win32API Call Error".
/* RETURN ERROR. */
END.

RUN GetDeviceCaps(H-DeviceContext, 8, OUTPUT R-formwidth).
RUN GetDeviceCaps(H-DeviceContext,10, OUTPUT R-formlength).
RUN GetDeviceCaps(H-DeviceContext,88, OUTPUT R-logpixelsx).
RUN GetDeviceCaps(H-DeviceContext,90, OUTPUT R-logpixelsy).

RUN StartDocA( H-DeviceContext,
GET-POINTER-VALUE(p-docinfo),
OUTPUT R-val).
IF ERROR-STATUS:ERROR OR r-val = 0
THEN DO:
/* RUN GetLastError(OUTPUT r-err). */
MESSAGE "StartDocA:ERROR " VIEW-AS ALERT-BOX
TITLE "Win32API Call Error".
/* RETURN ERROR. */
END.

ASSIGN
SET-SIZE(p-printdlg) = 0
SET-SIZE(p-docinfo) = 0
SET-SIZE(p-docname) = 0.

PROCEDURE PrintDlgA EXTERNAL "comdlg32.dll":U:

DEFINE INPUT PARAMETER Structure AS LONG.
DEFINE RETURN PARAMETER Result AS LONG.
END.

PROCEDURE GlobalLock EXTERNAL "Kernel32":U:
DEFINE INPUT PARAMETER hDevMod AS LONG.
DEFINE RETURN PARAMETER lpDevMod AS LONG.
END.

PROCEDURE SaveDC EXTERNAL "gdi32.dll":U:
DEFINE INPUT PARAMETER DChdl AS LONG.
DEFINE RETURN PARAMETER result AS LONG.
END.

PROCEDURE StartDocA EXTERNAL "gdi32.dll":U:
DEFINE INPUT PARAMETER DChdl AS LONG.
DEFINE INPUT PARAMETER DOCINFO AS LONG.
DEFINE RETURN PARAMETER jobnum AS LONG.
END.
PROCEDURE GetDeviceCaps EXTERNAL "gdi32.dll":U:
DEFINE INPUT PARAMETER hdl AS LONG.
DEFINE INPUT PARAMETER dcparm AS LONG.
DEFINE RETURN PARAMETER result AS LONG.
END.