Link to home
Start Free TrialLog in
Avatar of fidodido
fidodido

asked on

How to Print from an NT Service ?

Hi,
I have a program which runs as NT service. It generates log messages and these messages are to be printed.

Can anyone tell me how I can print from a program running as service(even in the absence of any user loggin to that machine)

thanx
Prashanth
Avatar of fidodido
fidodido

ASKER

Adjusted points from 50 to 100
If you mean displaying the message on the screen, use MessageBox(... MB_SERVICE_NOTIFICATION )
If you want to use a printer, use the standard spooler API like WritePrinter or StartJob etc.
I mean, StartDoc() etc
Hi,

Yeah ! I want to print and not display a message box !

Let me eloborate my problem further.
The program prints properly when run in non-service mode. The printer routine querie the follwoing registry key "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\" and the "Device" string value.

But when I run in service mode, the follwoing registry key is queried "HKEY_USERS\.DEFAULT\Software\Microsoft\Windows NT\CurrentVersion\Windows" and the Device string value. If this registry entry is present in the system, printing happens properly even in the service mode.

Now my question is, is there any way I can install a printer at system level ("Default user") so that the later registry key gets created and my application works fine?

thanx
Prashanth
Hi,

Yeah ! I want to print and not display a message box !

Let me eloborate my problem further.
The program prints properly when run in non-service mode. The printer routine querie the follwoing registry key "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\" and the "Device" string value.

But when I run in service mode, the follwoing registry key is queried "HKEY_USERS\.DEFAULT\Software\Microsoft\Windows NT\CurrentVersion\Windows" and the Device string value. If this registry entry is present in the system, printing happens properly even in the service mode.

Now my question is, is there any way I can install a printer at system level ("Default user") so that the later registry key gets created and my application works fine?

thanx
Prashanth
Now I see...

Well, if you write a program for sale, then you definitely have to provide the special dialog in the setup program (or in the separate configuration program) that will ask the user to specify the printer which will be used for the service. Store the printer name under HKLM\Software\Your company\Your program\.
Keep in mind that there may be several printers installed and the user may use a one printer to print the documents and another one to print the log.

It's impossible to setup the printer in such way that it will appear under HKCU\.DEFAULT key.
But you may enter the Device value under HKCU\.DEFAULT manually by the registry editor or by using the method described in:

"Modifying Ntuser.dat Hive So New Users Get Defined Settings"
http://support.microsoft.com/support/kb/articles/Q146/0/50.asp

"How to Add a Default Printer for All New Users"
http://support.microsoft.com/support/kb/articles/Q252/3/88.ASP

Alternatively, when you program runs in the 'normal' mode, it can check if there is any value under HKCU\.DEFAULT\Device. If no, your program may copy to that key the value from the HKCU\CurrentUser\Device.
ASKER CERTIFIED SOLUTION
Avatar of NickRepin
NickRepin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial