chadmanvb
asked on
run exe as system not working
I have the following application below that keeps a screen/computer from sleeping for a kiosk machine. If I run this manually as myself, it works. I wanted to run it as system and have it start when the kiosk auto-logs in on a reboot. When I run it as system, I log in, I see it running, but the application is not working. I can kill the task, restart it as myself, and then it works. Any ideas?
Imports System.Runtime.InteropServ ices
Public Class Form1
<DllImport("Kernel32.DLL", CharSet:=CharSet.Auto, SetLastError:=True)> _
Private Shared Function SetThreadExecutionState(By Val state As EXECUTION_STATE) As EXECUTION_STATE
End Function
' API call to prevent sleep (until the application exits)
Private Declare Function uses Lib "kernel32" (ByVal esflags As EXECUTION_STATE) As EXECUTION_STATE
' Define the API execution states
Private Enum EXECUTION_STATE
' Stay in working state by resetting display idle timer
ES_SYSTEM_REQUIRED = &H1
' Force display on by resetting system idle timer
ES_DISPLAY_REQUIRED = &H2
' Force this state until next ES_CONTINUOUS call
' and one of the other flags are cleared
ES_CONTINUOUS = &H80000000
End Enum
' Prevents sleep as form loads
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
No_Sleep()
End Sub
' Call API - force no sleep and no display turn off
Private Function No_Sleep() As EXECUTION_STATE
Return SetThreadExecutionState(EX ECUTION_ST ATE.ES_SYS TEM_REQUIR ED Or _
EXECUTION_STATE.ES_CONTINU OUS Or EXECUTION_STATE.ES_DISPLAY _REQUIRED)
End Function
End Class
Imports System.Runtime.InteropServ
Public Class Form1
<DllImport("Kernel32.DLL",
Private Shared Function SetThreadExecutionState(By
End Function
' API call to prevent sleep (until the application exits)
Private Declare Function uses Lib "kernel32" (ByVal esflags As EXECUTION_STATE) As EXECUTION_STATE
' Define the API execution states
Private Enum EXECUTION_STATE
' Stay in working state by resetting display idle timer
ES_SYSTEM_REQUIRED = &H1
' Force display on by resetting system idle timer
ES_DISPLAY_REQUIRED = &H2
' Force this state until next ES_CONTINUOUS call
' and one of the other flags are cleared
ES_CONTINUOUS = &H80000000
End Enum
' Prevents sleep as form loads
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
No_Sleep()
End Sub
' Call API - force no sleep and no display turn off
Private Function No_Sleep() As EXECUTION_STATE
Return SetThreadExecutionState(EX
EXECUTION_STATE.ES_CONTINU
End Function
End Class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!
ASKER