Wednesday, August 26, 2009

Print images directly to a printer with ShellExecute (in XP)

If you need to write code in AX to print an image file directly to a printer, your first thought might be to call shellExecute like this:

winapi::shellExecute(filename.jpg, '', '', 'print');

This would be fine with other types of files, but in XP image files will be printed via the Photo Printing Wizard, so in this case you simply don't get the file printed.

If you open Tools / Folder Options from an Explorer, go to the tab page File Types and find the type of image you want to print, you can click Advanced button to see that the operating system does when you right click on a file of that type and select print. If available the information will be under "printto". For TIF images on my box, I can see that this is what it will call:

rundll32.exe C:\WINDOWS\system32\shimgvw.dll,ImageView_PrintTo /pt "%1" "%2" "%3" "%4"

So in reality this is what you have to call through shellExecute.

Here is a sample job for that:

static void TestPrint(Args _args)
winapi::shellExecute(@'C:\WINDOWS\System32\rundll32.exe', @'C:\WINDOWS\System32\shimgvw.dll,ImageView_PrintTo /pt "C:\MYIMAGEFILE.TIF" "\\PRINTSERVER\PRINTERNAME" "%3" "%4"');

No comments: