Problem with file execution from a windows service

W

Wing

I have written a Windows Service in VB.net. Its quite simple, it just reads
some registry values and then based on the current time and a key value in
the registry called Execution_Time it is supposed to execute a visual basic
exe file.

I have tested the code in the Windows service inside normal Windows forms
app. It executes the file no problem. However, the file does not get
executed from the Windows service. I have used event logging to find out
what is being missed and it does seem that nothing is failing but the code
in the vb exe file does not seem to do its stuff - it should query an access
database adn then based on the results send some emails via smtp it also
writes some general info about what has just happened to the registry. It
just does not do any of this.

By the way, the vb exe file is a vb6 file. If started with command line
argument - "/nf" then no form is displayed. If no command line argument then
the settings form for the file is displayed.

Code for the windows service is as follows:

Protected Overrides Sub OnStart(ByVal args() As String)
tmr = New Timer
' Add code here to start your service. This method should set things
' in motion so your service can do its work.

bExecuted = False

'Add timer Elapsed event and start timer
AddHandler tmr.Elapsed, AddressOf OnTimerTick
tmr.Interval = 50000
tmr.AutoReset = True
tmr.Enabled = True
End Sub

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your
service.
tmr.Enabled = False
End Sub

Private Sub OnTimerTick(ByVal source As System.Object, ByVal e As
System.Timers.ElapsedEventArgs)

sExecutionTime = GetRegistryValue("Execution_Time")

'Test for timer
EventLog.WriteEntry("The AutoEMailer is ticking away")

'Get Current Time
sCurrentTime = Now.ToShortTimeString
'If current time = execution time then run autoemailer (but only if
it has not been run _
'already ie if bExecuted = false

EventLog.WriteEntry(sExecutionTime & " . " & sCurrentTime)

If sCurrentTime.Substring(0, 2) = sExecutionTime.Substring(0, 2) _
And Now.DayOfWeek <> DayOfWeek.Saturday _
And Now.DayOfWeek <> DayOfWeek.Sunday _
And bExecuted = False Then

'Run AutoEmailer with command line arguments
RunAutoEMailer()

'Set flag to true after running the mailer
bExecuted = True
EventLog.WriteEntry("The AutoEMailer just executed")
End If

'Set flag to false when the time is no longer equal to Execution
time
If sCurrentTime <> sExecutionTime Then
bExecuted = False
End If
End Sub

Private Sub RunAutoEMailer()
' p is a component and is declared at module level - friend
withevents p as process
EventLog.WriteEntry("Hi from RunAutoEMailer proc")
'Get the filename
With p
.StartInfo.FileName = "C:\Program
Files\AutoEMailer\AutoEMailer.exe"
.StartInfo.Arguments = "/nf"

'Execute
Try
.Start()
Catch ex As Exception
EventLog.WriteEntry("AutoEMailer caused the following
error:" & vbCrLf & ex.ToString)
End Try
End With
End Sub

Private Function GetRegistryValue(ByVal sValueName As String) As String
Dim RegKey As RegistryKey
Dim sValue As String
Try
RegKey =
Registry.LocalMachine.OpenSubKey("SOFTWARE\AutoEMailer")
sValue = DirectCast(RegKey.GetValue(sValueName), String)
Finally
If Not RegKey Is Nothing Then RegKey.Close()
End Try
Return sValue
End Function

Private Sub p_Exited(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles p.Exited
EventLog.WriteEntry(p.ExitCode.ToString)
End Sub


TIA

Julian
 
A

_Andy_

I have written a Windows Service in VB.net. Its quite simple, it just reads
some registry values and then based on the current time and a key value in
the registry called Execution_Time it is supposed to execute a visual basic
exe file.

I have tested the code in the Windows service inside normal Windows forms
app. It executes the file no problem. However, the file does not get
executed from the Windows service. I have used event logging to find out
what is being missed and it does seem that nothing is failing but the code
in the vb exe file does not seem to do its stuff - it should query an access
database adn then based on the results send some emails via smtp it also
writes some general info about what has just happened to the registry. It
just does not do any of this.

By the way, the vb exe file is a vb6 file. If started with command line
argument - "/nf" then no form is displayed. If no command line argument then
the settings form for the file is displayed.

Code for the windows service is as follows:

Does it work if you run the service with a profile? i.e. Configure the
"log on as" with your own credentials. It could be a security problem
- test to see if "p" is nothing. I take it from your notes that the
registry reading works correctly.

Rgds,
 
W

Wing

Does not run when the account is set to adminstrator. Value of p does not
seem to be nothing.

I just inserted the following and nothing was written to the event log:
Try

If Not IsNothing(p) Then

..Start()

..WaitForExit()

Else

EventLog.WriteEntry("P is nothing")

End If

Catch ex As Exception

EventLog.WriteEntry("AspenEMailer caused the following error:" & vbCrLf &
ex.ToString)

End Try

Any other clues?

Wing
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top