Solved

run exe as system not working

Posted on 2013-10-23
3
294 Views
Last Modified: 2013-10-23
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
0
Comment
Question by:chadmanvb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39594444
I think the problem is that this API is specific to user session. What login is used to auto login? You need to use that same account to run this application.
0
 

Author Comment

by:chadmanvb
ID: 39594621
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!
0
 

Author Closing Comment

by:chadmanvb
ID: 39594623
Thanks!
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question