Link to home
Start Free TrialLog in
Avatar of chadmanvb
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.InteropServices

Public Class Form1

    <DllImport("Kernel32.DLL", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Private Shared Function SetThreadExecutionState(ByVal 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(EXECUTION_STATE.ES_SYSTEM_REQUIRED Or _
               EXECUTION_STATE.ES_CONTINUOUS Or EXECUTION_STATE.ES_DISPLAY_REQUIRED)
    End Function
End Class
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chadmanvb
chadmanvb

ASKER

Yep, I was accually testing that before I read this.  I setup the task to run as the generic id it logs in with.  This worked perfect!
Thanks!