Page 1 of 1

printing from .exe running as a service

Posted: Wed Apr 05, 2006 5:22 pm
by don lowenstein
I have an exe loaded as a service on NT. Many thanks to the help received from this forum in creating exe's to run as a service.

Now, I need this application to print to the default printer. My application uses the tprinter class and the Fivewin report class.

During the print attempt I receive the following message.

MsgStop( "There are no printers installed!" + CRLF + ;
"Please exit this application and install a printer." )

This comes from the printer.prg (tprinter class) from the following lines of code.

if ::hDC != 0
aOffset := PrnOffset( ::hDC )
::nXOffset := aOffset[1]
::nYOffset := aOffset[2]
::nOrient := ::GetOrientation()
elseif ComDlgXErr() != 0
MsgStop( "There are no printers installed!" + CRLF + ;
"Please exit this application and install a printer." )
::nXOffset := 0
::nYOffset := 0
else
::nXOffset := 0
::nYOffset := 0
::nOrient := DMORIENT_PORTRAIT
endif

My question is, how can I utilize the printer class from an application running as a service? Or, how can I "install" a printer which will be available to services that are running?

Thanks in advance.

Posted: Fri Mar 23, 2007 3:18 pm
by AngelSalom
Can you tell me how you make the application run as a service?

run exe as a service

Posted: Fri Mar 23, 2007 4:51 pm
by don lowenstein

Posted: Mon Mar 26, 2007 4:40 pm
by James Bott
Don,

Search google for this phrase, "printing from windows service" and look at the first article listed. I think it will answer your question.

James

Posted: Mon Mar 26, 2007 5:09 pm
by don lowenstein
Thanks.

Currently I perform my printing using a .pdf tool that I think you may have alerted me about.

Image2PDF.dll -- works wonderfully. I use the FWH report object - modified 2 lines of code to control the location and names of the .emf files, and then convert to .pdf documents and deliver via the web.

I will checkout your link as well.

Posted: Tue Mar 27, 2007 6:32 am
by AngelSalom
Don, thanks for your help. I make the application right, but now i've a problem ...

I've installed the exe as a service with the srvany.

Into the .prg i use these functions :

//------------------------------
Function ServiceProcess( mode )
//------------------------------
Local nProcessId := 0

Default mode := 0

nProcessId := GCP( )

If Abs( nProcessId ) > 0
RSProcess( nProcessId, mode )
Endif

RETURN

//----------------------------------------------------
DLL32 FUNCTION RSProcess(npID AS LONG ,;
nMode AS LONG ) AS LONG ;
FROM "RegisterServiceProcess" LIB "kernel32.DLL"
//----------------------------------------------------

//----------------------------------------------------
DLL32 FUNCTION GCP() AS LONG;
FROM "GetCurrentProcessId" LIB "kernel32.dll"
//----------------------------------------------------

... and in then Function Main()
ServiceProcess(1)


Well, it's allright, but when i try to start the service, i've got an error message : (error 1053 : service is not responding) (windows 2003 server).

Curiously, the application starts (i make a log file in the app and really works), but the service ends and the application is out!


Thanks ... and sorry for my english ! ;-)

Posted: Tue Mar 27, 2007 5:50 pm
by don lowenstein
I think the .exe which is spawned by the service should run continously controlled by some sort of loop or timer. I usually terminate the .exe via a 'trigger' such as writing a txt file from another process to instruct the loop to end.

Note that the srvany.exe service process simply loads yourapplication.exe automatically, upon server startup. It does not continously load the application. If you terminate the .exe the service may still continue to run. Terminating the service will terminate your .exe, but terminating your .exe will not terminate the service necessarily.

I use the service (srvany.exe) as a method to automatically load an exe process upon server startup.