File: n_addprinter.sru
Size: 5067
Date: Tue, 13 Apr 2021 20:48:06 +0200
$PBExportHeader$n_addprinter.sru
forward
global type n_addprinter from nonvisualobject
end type
type process_information from structure within n_addprinter
end type
type startupinfo from structure within n_addprinter
end type
end forward

type process_information from structure
   long     hprocess
   long     hthread
   unsignedlong      dwprocessid
   unsignedlong      dwthreadid
end type

type startupinfo from structure
   unsignedlong      cb
   string      lpreserved
   string      lpdesktop
   string      lptitle
   unsignedlong      dwx
   unsignedlong      dwy
   unsignedlong      dwxsize
   unsignedlong      dwysize
   unsignedlong      dwxcountchars
   unsignedlong      dwycountchars
   unsignedlong      dwfillattribute
   unsignedlong      dwflags
   unsignedinteger      wshowwindow
   unsignedinteger      cbreserved2
   long     lpreserved2
   long     hstdinput
   long     hstdoutput
   long     hstderror
end type

global type n_addprinter from nonvisualobject autoinstantiate
end type

type prototypes
Function boolean CreateProcess ( &
   string lpApplicationName, &
   Ref string lpCommandLine, &
   long lpProcessAttributes, &
   long lpThreadAttributes, &
   boolean bInheritHandles, &
   ulong dwCreationFlags, &
   long lpEnvironment, &
   string lpCurrentDirectory, &
   STARTUPINFO lpStartupInfo, &
   Ref PROCESS_INFORMATION lpProcessInformation &
   ) Library "kernel32.dll" Alias For "CreateProcessW"

Function Boolean CloseHandle ( &
   long hObject &
   ) Library "kernel32.dll"

Function long WaitForSingleObject ( &
   long hHandle, &
   long dwMilliseconds &
   ) Library "kernel32.dll"

end prototypes

type variables
Private:

Constant UInt SW_HIDE = 0
Constant ULong STARTF_USESHOWWINDOW  = 1
Constant ULong CREATE_NEW_CONSOLE    = 16
Constant ULong NORMAL_PRIORITY_CLASS = 32
Constant ULong INFINITE = 4294967295   // 0xFFFFFFFF

end variables
forward prototypes
public function boolean of_addprinter (string as_infname, string as_drvname, string as_ptrname)
public function boolean of_adddatawindowps (string as_infname)
private function long of_runandwait (string as_cmdline)
end prototypes

public function boolean of_addprinter (string as_infname, string as_drvname, string as_ptrname);// -----------------------------------------------------------------------------
// SCRIPT:     n_addprinter.of_AddPrinter
//
// PURPOSE:    This function adds a printer using an .inf file.
//
// ARGUMENTS:  as_infname  - Full path name of the .inf file
//             as_drvname  - The name of the driver in the .inf
//             as_ptrname  - The name of the printer
//
// RETURN:     True=Process Created, False=Create Process failed
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 02/11/2011  RolandS     Initial Coding
// -----------------------------------------------------------------------------

String ls_cmdline

// build command line
ls_cmdline = "rundll32.exe printui.dll,PrintUIEntry /if /f ~"" + &
               as_infname + "~"" + " /r ~"file:~" /m ~"" + &
               as_drvname + "~" /b ~"" + as_ptrname + "~""

// execute the command
If of_RunAndWait(ls_cmdline) < 0 Then
   Return False
End If

Return True

end function

public function boolean of_adddatawindowps (string as_infname);// -----------------------------------------------------------------------------
// SCRIPT:     n_addprinter.of_AddDataWindowPS
//
// PURPOSE:    This function adds the DataWindow Ghostscript printer.
//
// ARGUMENTS:  as_infname  - Full path name of the .inf file
//
// RETURN:     True=Process Created, False=Create Process failed
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 02/11/2011  RolandS     Initial Coding
// -----------------------------------------------------------------------------

Return of_AddPrinter(as_infname, "Ghostscript PDF", "Sybase DataWindow PS")

end function

private function long of_runandwait (string as_cmdline);// run a program and wait for it to finish

STARTUPINFO lstr_si
PROCESS_INFORMATION lstr_pi
Long ll_null, ll_ExitCode
ULong lul_CreationFlags
String ls_null

// initialize arguments
SetNull(ls_null)
lstr_si.cb = 68
lstr_si.dwFlags = STARTF_USESHOWWINDOW
lstr_si.wShowWindow = SW_HIDE
lul_CreationFlags = CREATE_NEW_CONSOLE + NORMAL_PRIORITY_CLASS

// create process/thread and execute the passed program
If CreateProcess(ls_null, as_cmdline, ll_null, &
         ll_null, False, lul_CreationFlags, ll_null, &
         ls_null, lstr_si, lstr_pi) Then
   // wait until process ends or timeout period expires
   ll_ExitCode = WaitForSingleObject(lstr_pi.hProcess, INFINITE)
   // close process and thread handles
   CloseHandle(lstr_pi.hProcess)
   CloseHandle(lstr_pi.hThread)
Else
   // return failure
   ll_ExitCode = -1
End If

Return ll_ExitCode

end function

on n_addprinter.create
call super::create
TriggerEvent( this, "constructor" )
end on

on n_addprinter.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on