Skip to main content

Unix to Windows interoperability by manipulating the printer queue with Perl using the win32 api

The development of 4GL Ingres applications happens on Unix.Access to the Unix environment is made possible through windows terminals using a terminal emulator .

When printing a report from inside the 4GL application, the application calls the 'system' command which passes arguments to the 'report' command.The actual report is run and the output is being redirected to the local printer on windows by sending escape sequences to the terminal emulator which open the local printer port and send the data (pure text) to it in RAW format with no translation whatsoever by the windows driver.
The problem is that the report looks not 'pretty' as it consists of dots and dashes acting as placeholders for the textual data.







The presentation would be much better in another format like HTML.

The plan was to :
add HTML tags inside the actual report file,before it is run,so the dot and dashes would be replaced by the tags. In practice each .prln or .pr report command should be tweaked to include the HTML tags.
Then the newly formated file would be send to windows and internet explorer would open it without user intervention as to aid the user in previewing it before it is actually send for printing.

The problem is achieving interoperability,interception and subsequently manipulation of that data by windows, between unix and windows and one solid way is by opening a DBI connection to the Ingres server from windows hence getting and formatting the data on windows. One disadvantage of this approach is that you should swift/rewrite the report logic and queries already there in a certain format/language into equivalent DBI format/language thus creating a new programm for each report (and in a language different than that of the report generation language), in essence this leads to do the work again and in another environment which has cost in time and effort.

Another approach was to write a program that monitors and manipulates the printer queue as to avoid the situation described above.
It would function by checking every job added to the queue and if that job is a text file that contains a unique identifier set by the report then it is forwarded to internet explorer and deleted from the printer queue.

With the queue manipulation approach you don't alter any of the logic ,don't do any of the work again,don't write a new programm; you can tweak the report or use it as is and just capture and manipulate the report's output .

The printer monitoring and job manipulation was done using Perl and the Win32::API module which interfaces with the system dlls and calls the functions residing in them (stopping,resuming a job etc).Since the dlls are written in C it meant getting deep into unmanaged code with,of course, all its associated risks.
It was really hard doing that in Perl as you had to allocate and deallocate memory manually and then parse the printing structures using memory padding specific to the the windows architecture, which amongst others means abusing the 'pack' and 'unpack' functions!Pack to create the structures needed and subsequently passing them into the dll's and 'unpack' for parsing the resulting structures.

At the heart of the programm lies the 'FindFirstPrinterChangeNotification' and 'FindNextPrinterChangeNotification' of the winspool.drv dll, which take note of any printer events and would fire up whenever a new job was added to the queue.

The Win32::Registry module was used to check if there is a default spooling directory and if not it sets the registry entry to $systemroot.'\system32\spool\PRINTERS. That's needed because when there is time to read the file created by the spooler,the programm must know where to look for finding it.

The communication with Internet explorer was achieved using the Win32::OLE module.

A snippet on how to get the default printer :

sub getprinter {
$getdefault=new Win32::API('winspool.drv','GetDefaultPrinterA','PP','N');
$buffer= pack('x4');
$getdefault->Call(0,$buffer);

$buffer=unpack('L',$buffer);
$text=" " x $buffer;
$getdefault->Call($text,$buffer);
print "name of printer : $text\n";

check_registry();

$OpenPrinter=new Win32::API('winspool.drv','OpenPrinterA','PPP','N');
$handle = pack('x4');
$pdefaults = pack('x8L',0x4);

$OpenPrinter->Call($text,$handle,$pdefaults);
$handle=unpack('L',$handle);
}

The Win32::API module is cool but maybe a better alternative would be using the Inline C module,which as they say provides a better interface.I haven't tried it yet so I don't have a personal opinion on it.

Or maybe use Visual Basic as it is native to the windows environment and has wrapper functions which are translated to api calls.

The downside of this approach is that you have to spend effort in designing the report like it is a valid HTML page but perhaps instead of using HTML the report could be exported in XML ,so to shift away from the presentation layer. But the advantage is that that you can capture the data from unix and alter it in any form desirable.

This was an experiment with mixed opinions on its applicability in the real world but still remaining an interesting approach.





Comments

Popular posts from this blog

Book Review : How To Create Pragmatic, Lightweight Languages

At last, a guide that makes creating a language with its associated baggage of lexers, parsers and compilers, accessible to mere mortals, rather to a group of a few hardcore eclectics as it stood until now.

The first thing that catches the eye, is the subtitle:

The unix philosophy applied to language design, for GPLs and DSLs"
What is meant by "unix philosophy" ?. It's taking simple, high quality components and combining them together in smart ways to obtain a complex result; the exact approach the book adopts.
I'm getting ahead here, but a first sample of this philosophy becomes apparent at the beginnings of Chapter 5 where the Parser treats and calls the Lexer like  unix's pipes as in lexer|parser. Until the end of the book, this pipeline is going to become larger, like a chain, due to the amount of components that end up interacting together.

The book opens by putting things into perspective in Chapter 1: Motivation: why do you want to build lan…

How Much Gameplay Can You Pack In Just 13K?

Given our expectations of Xbox games, you might consider writing a game within a 13K limit, which is the challenge for the annual js13K competition far too restrictive. Its results are now out and prove that it is possible to produce a game that is fun to play. 

Back in the tape loading days and on platforms the likes of Commodore64 games came in sizes of 4K or less. As proof of concept, here's a list of a few such 4K titles, copied over from Lemon64 's archive:
Alien SidestepBug CrusherDot GobblerClose EncountersDot Gobbler v2GridrunnerLaser CyclesMarios BrewerySpace ActionSpace RicoshayTank WarsHesmon64Retro Ball  Fast forward to now, at a time when Javascript's eating the world by making all sorts of applications or  games available to everyone through the medium of the browser, rendering the need of dedicated platforms and Operating systems obsolete, 13K is sufficient enough to pack both gameplay AND cool graphics due to the advanced browser engines and HTML5.

Hour of Code 2017 Introduces App Lab

t's the time of year when the world-class Hour of Code once more commences; just an hour for introducing coding to the uninitiated, having them complete self guided tutorials. But is a hour sufficient? What can a beginner actually code within this limit? The answer is a bit more complicated than that, so let's find out all about it! Integrated into the larger, worldwide, annual Computer Science Education week, this year taking place December 4-10, Hour of Code's novel mission has always been to get everybody coding, aged from 4 to 104, by providing: "a one-hour introduction to computer science, designed to demystify code, showing that anybody can learn the basics, and broadening participation in the field of computer science". But first of all, why this obsession with Computer Science, in particular in getting  kids as young as 4 to learn to code? The answer is simple. Nowadays code is everywhere around us, from desktop computers to mobile phones and, thanks to w…