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');

$text=" " x $buffer;
print "name of printer : $text\n";


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


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.


Popular posts from this blog

Insider's Guide To Udacity Android Developer Nanodegree Part 3 - Making the Baking App

Continuing to chart my experience of Udacity's Android Developer Nanodegree we step up in level, embarking on the advanced part of the super-course.
Completing project "Popular Movies" (see Part 2 of this series) signaled the end of "Android Developer". Now we are ready to tackle the second element of the program "Advanced Android Developer", a new class with a new syllabus and project. Continuing to chart my experience of Udacity's Android Developer Nanodegree we step up in level, embarking on the advanced part of the super-course.

Completing project "Popular Movies" (see Part 2 of this series) signaled the end of "Android Developer". Now we are ready to tackle the second element of the program "Advanced Android Developer", a new class with a new syllabus and project.

"Advanced Android Developer" is a mixed bag of self contained material and of coding seven different sample apps to learn about the…

Export your Wunderlist tasks with XPath

As brought up in this ProductHunt thread, the news is that Wunderlist is going to be deprecated in favor of the new Microsoft To-Do note taking platform.

This is what Wunderlist support had to say in response to my inquiry on Wunderlist's future:

"Now that the next evolution of Wunderlist is here, in the form of Microsoft To-Do Preview (, Wunderlist will no longer receive any updates or bug fixes and will eventually be retired. It won’t happen in the next few months and we’ll be sure to give our users plenty of notice beforehand. In the meantime, you can continue to use Wunderlist normally. Of course, we’d also love for you to try To-Do and let us know how you like it – and how we can improve it. While Wunderlist will continue to exist alongside To-Do for the time being, support for Wunderlist will eventually be removed. Not to worry, though! We will inform all Wunderlist users prior to shutting down service. You'll have ample opport…

AWS and Ionic Team Up In Starter Project

Amazon is quick in recognizing that just offering support for a number of popular programing languages is not enough to lure hoards of developers to the platform. That's why we are seeing a move towards wrapping its AWS services with greater user-friendliness.

The start was made with the introduction of CodeStar, which aimed to simplify the setting up of a project's AWS infrastructure, especially  with regard to policy and authorization, as we examined in CodeStar to Simplify Development On AWS. 

It continues this trend with the release of the open source Ionic AWS starter project Mobile Web and Hybrid Application which aims to act as a skeleton, or boilerplate, Ionic application tweaked in such a way to give developers a headstart in configuring their mobile Ionic front-end applications in relation to an AWS backend.

full article on i-programmer