Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 167
  • Last Modified:

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
0
fidodido
Asked:
fidodido
  • 5
  • 3
1 Solution
 
fidodidoAuthor Commented:
Adjusted points from 50 to 100
0
 
NickRepinCommented:
If you mean displaying the message on the screen, use MessageBox(... MB_SERVICE_NOTIFICATION )
0
 
NickRepinCommented:
If you want to use a printer, use the standard spooler API like WritePrinter or StartJob etc.
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
NickRepinCommented:
I mean, StartDoc() etc
0
 
fidodidoAuthor Commented:
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
0
 
fidodidoAuthor Commented:
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
0
 
NickRepinCommented:
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.
0
 
NickRepinCommented:
That is the answer.
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now