Console App Crashes as System Service?

I created a WIndows console app/system service with the intention of using it as a system service. If I run the app with the console window open the app runs properly. If I install it as a system service it runs for a few seconds and then the service stops. Is there some way to get the information from the system as to why it’s crashing? I tried looking in the event logs and found nothing.

Is stdOut allowed if the console app is installed as a service? I also have code that writes a log file to the desktop but that is also not working as a service?

Any advice? Thanks.

Is there a way to tell my app to write error messages to the Windows system logs so I can debug this console app installed as a system service?

system.debuglog
DebugView can then watch those

It doesn’t appear that my app is even starting as a service. The first thing it does it write a temp file to the local temp directory and if I click on the exe that’s installed in program files I can immediately see the temp file created. But if I start the service it never gets created and the service stops within 5 seconds.

When I look at the properties of the service I installed it is indeed the correct path to my exe. Is there something else I can check to see if the service is indeed installed properly?

I’m not sure my app is even starting or even open long enough to write anything to the log.

Here is what I have in my events for the console/service app. Could anything here be causing it to fail on start as a service but running fine as a console app?

Pause:

 isPaused = true

Resume:

 isPaused = false

Run:

[code] // This is the program//s main execution point
if (not isPaused) then

' Initialize the server
AppOpen

' Start the listening serverSocket

'SocketStuff

' Start the timer

'Timer stuff

Do
  DoEvents 
Loop

end

Quit(0)[/code]

Stop:

[code]
’ Clear ALL temp files
CleanTemp

’ Close the shell and exit all instances of plink.exe
'Dim Shell As mShell
'Shell.Execute(“taskkill /F /IM plink*”)
'Shell.Close

’ Close the service
Quit(0)[/code]

Unhandled Exception:

[code] If error <> Nil Then
Dim type As String = Introspection.GetType(error).Name
WriteStatus(type + EndOfLine + EndOfLine + Join(error.Stack, EndOfLine))
End If

Return True[/code]

Is your app class a ServiceApplication or is it just a ConsoleApplication?

Super is ServiceApplication.

I’ve isolated the issue. I have a database ID number that is pre encrypted in the registry. The Xojo service app tries to decrypt the ID number from the registry from a method in the run event of the system service. If I bypass this the service runs fine, and if I add the ID manually the other encryption in the app works fine. I’m not sure why it works in the debugger or when the service is ran in the console but it doesn’t work as a service. I may try and move this code elsewhere to see if I can find a way to get it working. Any ideas as to why a system service would need a few seconds on start before it can use encryption?

Here is the code that causes my service to crash when it’s first started, any ideas?

[code]
’ Decrypt the ID from the registry
Dim s as string = cPrivIdKey

dim encodedkey as memoryblock

s = DecodeBase64(s)

encodedkey = crypto.BERDecodePrivateKey(s)

Dim privateKey As String = encodedKey

Dim encryptedData As MemoryBlock

Dim strMsg As String
strMsg = strParam

encryptedData = DecodeBase64(strMsg)
encryptedData = ConvertEncoding(encryptedData, Encodings.UTF8)

Dim strId As String

If encryptedData <> Nil Then
Dim decryptedData As MemoryBlock = Crypto.RSADecrypt( encryptedData, privateKey )
strId = decryptedData
end

Return strId[/code]

Ok so it appears I was wrong my problem is much simpler. For some reason the system service in unable to read from the registry at all. Here is a sample of the way I am reading from the registry using the system service. I must be doing something wrong for a service to read from the registry. Any ideas? Thanks!!!

Dim r as new RegistryItem("HKEY_CURRENT_USER\\Software\\MyApp\\Settings\",True) strID = r.Value("ID")

A service runs independently of the logged in user, so hkey_current_user isn’t available. That’s why running as a regular console app works for you. You’ll need to read that data from another location.

@Travis Hill I thought that was probably the case. Can you tell me how to modify the code i posted so that it will work as a service? Will HKEY_LOCAL_MACHINE work? Thanks!

You’ll need to read that data from a file or local machine registry that isn’t impacted by the logged in user.

Thanks I will give that a try! :slight_smile: