AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[solved] Run as a service, and stay alive after logging off

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
interiot



Joined: 06 Nov 2005
Posts: 64

PostPosted: Tue May 20, 2008 1:41 am    Post subject: [solved] Run as a service, and stay alive after logging off Reply with quote

RunAsSvc goes a long way towards allowing AutoHotkey scripts to run as services. (be sure to mark the service as "allow service to interact with desktop")

However, if the current user ever logs off, AutoHotkey dies. Long story short, AutoHotkey is responding to WM_ENDSESSION, but we need interrupt that because services survive logoffs.
Code:
OnMessage(WM_ENDSESSION := 0x0016,  "ignore_logoff")

ignore_logoff(wParam, lParam)) {
    ENDSESSION_LOGOFF = 0x80000000
    if (lParam & ENDSESSION_LOGOFF)
        return true     ; somehow this makes MainWindowProc() fail to see this.
    else
        ExitApp     ; allow graceful shutdown
}

Some problems I've had so far include:
  • `Reload` forks a new process, which makes RunAsSvc.exe think we've exited completely, and as a result, our service gets put in the "stop" state. Unfortunately, this means that the still-running script can't be ended by stopping the service (since the service is "stopped already"). This is particularly unfortunate if your service is delivered with an installer/uninstaller, because this means the uninstaller can't just "sc stop", it has to go hunt down the errant process, before it's able to delete the .exe and installed directory.
  • `RunAs` doesn't seem to work.



GIANT HUGE OMFG SECRET: RestrictRun machines are a serious pain in the butt to deal with. It turns out that RestrictRun is totally bypassed by interactive services running as SYSTEM... those services can start any interactive application they want (since the launched applications run as a different user, and RestrictRun only applies to programs running as the current user). And what's the best way to have such a powerful service know when you want it to launch applications? Via global hotkeys, of course.

(okay, sure, this is easiest when you already have admin on the machine, and need to retain RestrictRun part of the time for a particular reason. ie. boring. However, even when you don't own admin, there are still ways to get your service going)

(caveat: this is only tested in XP, it probably fails spectacularly in Vista)


Last edited by interiot on Fri May 23, 2008 5:46 pm; edited 6 times in total
Back to top
View user's profile Send private message
Rhys



Joined: 17 Apr 2007
Posts: 722
Location: Florida

PostPosted: Tue May 20, 2008 2:32 am    Post subject: Reply with quote

This will be handy for me one day.
_________________
[Join IRC!]
Back to top
View user's profile Send private message
Guest






PostPosted: Tue May 20, 2008 3:17 am    Post subject: Reply with quote

so if i switch users, the script will still be running?
Back to top
k3ph



Joined: 21 Jul 2006
Posts: 123

PostPosted: Tue May 20, 2008 3:25 am    Post subject: Reply with quote

you can get Resource Kit from microsoft website to create your own services without the need of relying on sharewares or uknown third party tools.

guide: http://www.tacktech.com/display.cfm?ttid=197 | reference: http://support.microsoft.com/kb/251192

tip: using 'Local Service' or 'System' would not be affected by logout
Back to top
View user's profile Send private message
interiot



Joined: 06 Nov 2005
Posts: 64

PostPosted: Tue May 20, 2008 4:07 am    Post subject: Reply with quote

k3ph wrote:
using 'Local Service' or 'System' would not be affected by logout

Strange, my service was configured like that, but still got closed. Perhaps it's safe as long as it's not admin who is logging out?

Anonymous wrote:
so if i switch users, the script will still be running?

Yup. It stops working in between (during login) though, for obvious security reasons.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group