Welcome! Log In Create A New Profile

Advanced

[WD23] Creating a Service - Part 2

Posted by JP 
JP
[WD23] Creating a Service - Part 2
October 19, 2018 11:59AM
Hi All

I have written a small test service project which just loops and creates some files on disk so I can see it working. I then have another exe that uses ServiceInstall() to install the service (or uninstall it). That seems to work OK and the service is created (or removed) in the services list.

But when I try and start the service Windows says it started and then stopped straight away and my test loop code does not run at all - no code runs, not even code in the initialization of the project/exe.

I can run the exe as a normal exe and it works fine. I can create the service using WinDev or sc commands and it is created OK. There are no errors when it tries to run. If I use WinDev to run it then WinDev reports that the start command was OK, no errors. But nothing runs.

Any suggestions appreciated !
Re: [WD23] Creating a Service - Part 2
October 19, 2018 01:24PM
Hi JP, did you sign the exe? This may be a reason for the malfunction ...
pao
Re: [WD23] Creating a Service - Part 2
October 19, 2018 01:30PM
JP,
If it's a small project why don't you share the code, it's easier to help you if we can see the code.

Regards

Paulo Oliveira
JP
Re: [WD23] Creating a Service - Part 2
October 19, 2018 01:43PM
Paulo, Harvey

OK so I now have it running and it stays running (I can see in Task Manager and the Service control panel says it is running) but my code (inside the "Running the Service" section) does not seem to execute. All I'm doing in this code is a simple test as follows:

fSaveText( "test_file.txt" , "test")


That's it. But the file is not created so I assume the code is not being run?
Stefan Kern
Re: [WD23] Creating a Service - Part 2
October 19, 2018 01:46PM
Hi JP,

check the Windows event viewer, the windows service control manager writes errors in the log.

Services are not allowed to have a gui, make sure that you do not have any graphical interaction. Even iPrinreport() lets stop a service if the print dialog shows up.

And check user rights, services run in the context "local System account".
Change the user under the properties tab of the Service to an other user acount.

Best Regards

Stefan.
Re: [WD23] Creating a Service - Part 2
October 19, 2018 01:50PM
JP,

The Windows Event Log could give you more info. Check that first.


Also I would put everything you run in a exception trap and in case of an exception weite the error to disk. That should give info whenever something goes wrong.
I have something like this in the "when running the service of..." section

WHEN EXCEPTION IN
	
	// Run the service
	YourCode()	
	
	// Pause during 10 seconds
	ServiceWait(1000)
DO
	// In case of exception, adds an error into the log of events
	WriteAscii( fCurrentDir(fExeDir()) , "error.log", ExceptionInfo)
	ServiceWriteEventLog(ExceptionInfo, elError)
	
	// Stops the service
	//EndService(esFailure)
	
END

Note1; WriteAscii() is a procedure to add text to an asciifile for debuging purposes
Note2: The service is running from C:\Windows\system32 where you do not have write rights. Be aware of that.

Arie
JP
Re: [WD23] Creating a Service - Part 2
October 19, 2018 01:56PM
Hi All

OK, so, if I run the service exe in test mode it works fine, my test files are created - so the service code is working. However, when I run as a service no files are created. However, by using no wait in the code I can see the CPU go up hard which indicates that something is happening but no files being created.

I have tried running under local system account and also under my own login name (which should have admin rights to the folder I am writing to since I can do it when in test mode).

Any ideas?

UPDATE - sorry, the event logs show no errors.



Edited 1 time(s). Last edit at 10/19/2018 01:56PM by JP.
JP
Re: [WD23] Creating a Service - Part 2
October 19, 2018 02:01PM
Arie

Thank you! This was the problem: "Note2: The service is running from C:\Windows\system32 where you do not have write rights. Be aware of that."

By adding fully qualified drive and path to the test files it works fine. Thanks!

Thanks everyone!
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: