Solved

How to Print from an NT Service ?

Posted on 2000-04-06
8
159 Views
Last Modified: 2013-12-03
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
Comment
Question by:fidodido
  • 5
  • 3
8 Comments
 

Author Comment

by:fidodido
Comment Utility
Adjusted points from 50 to 100
0
 
LVL 15

Expert Comment

by:NickRepin
Comment Utility
If you mean displaying the message on the screen, use MessageBox(... MB_SERVICE_NOTIFICATION )
0
 
LVL 15

Expert Comment

by:NickRepin
Comment Utility
If you want to use a printer, use the standard spooler API like WritePrinter or StartJob etc.
0
 
LVL 15

Expert Comment

by:NickRepin
Comment Utility
I mean, StartDoc() etc
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:fidodido
Comment Utility
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
 

Author Comment

by:fidodido
Comment Utility
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
 
LVL 15

Expert Comment

by:NickRepin
Comment Utility
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
 
LVL 15

Accepted Solution

by:
NickRepin earned 100 total points
Comment Utility
That is the answer.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now