Solved

How to Print from an NT Service ?

Posted on 2000-04-06
8
160 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
ID: 2692334
Adjusted points from 50 to 100
0
 
LVL 15

Expert Comment

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

Expert Comment

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

Expert Comment

by:NickRepin
ID: 2692397
I mean, StartDoc() etc
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:fidodido
ID: 2692451
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
ID: 2692469
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
ID: 2692636
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
ID: 2692638
That is the answer.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
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…
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…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

27 Experts available now in Live!

Get 1:1 Help Now