User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » Run apps on power connect / disconnect Page [1]  
DirtyGreek
All American
29309 Posts
user info
edit post

Does something like this exist? I've googled and can't find anything. I want to be able to preserve power by killing / starting processes depending on whether my laptop is plugged in or not. It doesn't seem like that would be difficult to find, but I can't find it.

7/15/2007 10:41:45 AM

Aficionado
Suspended
22518 Posts
user info
edit post

probably have to write something to do what you want

7/15/2007 10:47:12 AM

FenderFreek
All American
2805 Posts
user info
edit post

I know in Linux Gnome you can use the external power on/off as a trigger to run scripts and stuff. If this is in Windows, idk, but I'd say you may have to just roll your own.

7/15/2007 10:55:48 AM

DirtyGreek
All American
29309 Posts
user info
edit post

I may have to roll my own. I forgot that windows actually has alarms for certain battery power limits, so I can have it at least run an app when the battery reaches a certain level. I can set that to 100%, so that it actually just runs the alarm when it's unplugged immediately. No idea how to make it do something when plugged back in, though...

7/15/2007 11:07:10 AM

scud
All American
10804 Posts
user info
edit post

http://www.pbus-167.com/

Pretty decent application

7/15/2007 11:55:58 AM

Noen
All American
31346 Posts
user info
edit post

im pretty sure you can do this with built-in functions in Visual Studio. But yeah, havent seen a product to do it yet

^I dont think this can do what George is talking about

[Edited on July 15, 2007 at 11:58 AM. Reason : .]

7/15/2007 11:56:09 AM

scud
All American
10804 Posts
user info
edit post

Noen - it provides a API that you can write hooks for.

http://www.pbus-167.com/nhc/nhc_advanced.htm

[Edited on July 15, 2007 at 12:04 PM. Reason : -]

7/15/2007 12:01:57 PM

DirtyGreek
All American
29309 Posts
user info
edit post

I'm actually downloading visual studio right now. I've never really done any actual windows development, but this should be simple, and I've found some code that shows how to detect battery level / external power.

also going to check this out ^

[Edited on July 15, 2007 at 12:09 PM. Reason : .]

7/15/2007 12:09:38 PM

DirtyGreek
All American
29309 Posts
user info
edit post

Well, it's certainly a hack and a half, but I got what I basically want going. I need it to sit in the system tray which it doesn't do yet, but it seems pretty functional.

It also utilizes a program called "process" that I got here
http://www.beyondlogic.org/consulting/processutil/processutil.htm

to terminate processes when you unplug, because I couldn't figure out an easy way to make vb do that. If anyone's an expert on that or finding out if a process is already running to see if it needs to be restarted (right now it just starts applications when you plug in whether they're already running or not), hit me up.

Anyway, if you want to check it out, here you go:
http://www.dirtygreek.org/stuff/BatteryApp.zip

Just unzip all of those files into one place, put the programs you want to start on plug in into startprograms.txt and the process names you want to end in endprograms.txt, then run BatteryApp.exe and/or have windows start it up automatically.

7/16/2007 9:54:58 AM

DirtyGreek
All American
29309 Posts
user info
edit post

added system tray functionality and some little tweaks. if anyone tries this, let me know what you think.

7/16/2007 12:22:26 PM

Noen
All American
31346 Posts
user info
edit post

Be carefuly how you are ending the process. If you are terminating the process instead of doing a graceful WM_CLOSE call, it could result in memory leaks and data loss.

But this is an amazing idea. Just a couple of other things you could add to see an even bigger effect: have this load as a system service on startup, so if you are booting on battery, it'll never load the stuff to begin with. and have the option to Mobile stop system services too. There are a lot of services that you may not "need" on during battery time. Might also be worth having a "no wifi" setting, where if you arent on the internet and are on battery (airplane, train, car trip, etc) it can shut off all the related windows services and applications to save a good chunk of memory footprint.

If you get this going, I think there'd be a market for it

also check out this thread on experts exchange for some process killing code
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20231530.html

[Edited on July 16, 2007 at 12:58 PM. Reason : .]

7/16/2007 12:49:03 PM

DirtyGreek
All American
29309 Posts
user info
edit post

Yeah, I've thought of some of that. I actually am just using that program (http://www.beyondlogic.org/consulting/processutil/processutil.htm) to terminate processes, and I'm not sure what method it uses. I Think the source is available, though, so I can find out.

UPDATE - i'm using the kill option on that program now, but there's actually a close option that uses WM_CLOSE. It also has some process suspend/resume abilities that I should check out:

Quote :
"Suspending a process causes the threads to stop executing user-mode (application) code. It also increments a suspend count for each thread. Therefore if a process is suspended twice, two resume operations will be required to resume the process (Decrement the suspend count to zero)."


It actually wouldn't let processes start anyway if booting on battery, as long as it's running at startup. As soon as they tried to open, it would kill them, since it's constantly monitoring for the power change.

It's not a service, but my understanding is that installing as a service isn't too difficult.

Do you really think people would pay for this? Provided it is updated to be more user friendly, etc? That would be cool, but I just figured it was too simple to cost anything. Then again, I've seen less complicated software going for way too much money before...

I don't have an account on experts-exchange, but I'm sure I can find something equivalent.

[Edited on July 16, 2007 at 1:12 PM. Reason : k]

7/16/2007 1:07:53 PM

Noen
All American
31346 Posts
user info
edit post

I think as-is, it's pretty much a freeware app. But yes, if you make it robust and configurable, I know I would definitely put down $9.95 for it. The joy of being able to get another 10-20 minutes per charge would be amazing.

Here was the pertinent solution from the experts-exchange link:

If you know the name of the exe then you can try this code

Add this code to a module
' Start Module code
Option Explicit
Const MAX_PATH& = 260

Declare Function TerminateProcess _
Lib "kernel32" (ByVal ApphProcess As Long, _
ByVal uExitCode As Long) As Long
Declare Function OpenProcess Lib _
"kernel32" (ByVal dwDesiredAccess As Long, _
ByVal blnheritHandle As Long, _
ByVal dwAppProcessId As Long) As Long
Declare Function ProcessFirst _
Lib "kernel32" Alias "Process32First" _
(ByVal hSnapshot As Long, _
uProcess As PROCESSENTRY32) As Long
Declare Function ProcessNext _
Lib "kernel32" Alias "Process32Next" _
(ByVal hSnapshot As Long, _
uProcess As PROCESSENTRY32) As Long
Declare Function CreateToolhelpSnapshot _
Lib "kernel32" Alias "CreateToolhelp32Snapshot" _
(ByVal lFlags As Long, _
lProcessID As Long) As Long
Declare Function CloseHandle _
Lib "kernel32" (ByVal hObject As Long) As Long

Private Type LUID
lowpart As Long
highpart As Long
End Type

Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
LuidUDT As LUID
Attributes As Long
End Type

Const TOKEN_ADJUST_PRIVILEGES = &H20
Const TOKEN_QUERY = &H8
Const SE_PRIVILEGE_ENABLED = &H2
Const PROCESS_ALL_ACCESS = &H1F0FFF

Private Declare Function GetVersion _
Lib "kernel32" () As Long
Private Declare Function GetCurrentProcess _
Lib "kernel32" () As Long
Private Declare Function OpenProcessToken _
Lib "advapi32" (ByVal ProcessHandle As Long, _
ByVal DesiredAccess As Long, _
TokenHandle As Long) As Long
Private Declare Function LookupPrivilegeValue _
Lib "advapi32" Alias "LookupPrivilegeValueA" _
(ByVal lpSystemName As String, _
ByVal lpName As String, _
lpLuid As LUID) As Long
Private Declare Function AdjustTokenPrivileges _
Lib "advapi32" (ByVal TokenHandle As Long, _
ByVal DisableAllPrivileges As Long, _
NewState As TOKEN_PRIVILEGES, _
ByVal BufferLength As Long, _
PreviousState As Any, _
ReturnLength As Any) As Long

Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szexeFile As String * MAX_PATH
End Type
'---------------------------------------
Public Function KillApp(myName As String) As Boolean
Const TH32CS_SNAPPROCESS As Long = 2&
Const PROCESS_ALL_ACCESS = 0
Dim uProcess As PROCESSENTRY32
Dim rProcessFound As Long
Dim hSnapshot As Long
Dim szExename As String
Dim exitCode As Long
Dim myProcess As Long
Dim AppKill As Boolean
Dim appCount As Integer
Dim i As Integer
On Local Error GoTo Finish
appCount = 0

uProcess.dwSize = Len(uProcess)
hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
rProcessFound = ProcessFirst(hSnapshot, uProcess)
Do While rProcessFound
i = InStr(1, uProcess.szexeFile, Chr(0))
szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
If Right$(szExename, Len(myName)) = LCase$(myName) Then
KillApp = True
appCount = appCount + 1
myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
If KillProcess(uProcess.th32ProcessID, 0) Then
'For debug.... Remove this
MsgBox "Instance no. " & appCount & " of " & szExename & " was terminated!"
End If

End If
rProcessFound = ProcessNext(hSnapshot, uProcess)
Loop
Call CloseHandle(hSnapshot)
Exit Function
Finish:
MsgBox "Error!"
End Function

'Terminate any application and return an exit code to Windows.
Function KillProcess(ByVal hProcessID As Long, Optional ByVal exitCode As Long) As Boolean
Dim hToken As Long
Dim hProcess As Long
Dim tp As TOKEN_PRIVILEGES


If GetVersion() >= 0 Then

If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken) = 0 Then
GoTo CleanUp
End If

If LookupPrivilegeValue("", "SeDebugPrivilege", tp.LuidUDT) = 0 Then
GoTo CleanUp
End If

tp.PrivilegeCount = 1
tp.Attributes = SE_PRIVILEGE_ENABLED

If AdjustTokenPrivileges(hToken, False, tp, 0, ByVal 0&, ByVal 0&) = 0 Then
GoTo CleanUp
End If
End If

hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, hProcessID)
If hProcess Then

KillProcess = (TerminateProcess(hProcess, exitCode) <> 0)
' close the process handle
CloseHandle hProcess
End If

If GetVersion() >= 0 Then
' under NT restore original privileges
tp.Attributes = 0
AdjustTokenPrivileges hToken, False, tp, 0, ByVal 0&, ByVal 0&

CleanUp:
If hToken Then CloseHandle hToken
End If

End Function
' End Module code

'Example on how to use the code' this is the click event of a button named cmdKill
Private Sub cmdKill_Click()
' Usage:
Dim pID As Long
Dim i As Integer
Dim strExe As String
strExe = "Notepad.Exe"
For i = 0 To 4
pID = Shell(strExe, vbNormalFocus)
Next i
'Five instances of notpade.exe is now created
Debug.Assert False
MsgBox "It is " & _
KillApp(strExe) & _
" that all instances of " & vbCrLf & _
strExe & _
" have been terminated!"
End Sub

7/16/2007 3:11:31 PM

DirtyGreek
All American
29309 Posts
user info
edit post

UPDATE: I've added a few features, including some little system tray options to disable and exit, and the ability to start and stop windows services as well as programs.

Important: Remember to put "quotes" around any file paths or service names that have spaces.

7/17/2007 11:44:02 PM

AntecK7
All American
7755 Posts
user info
edit post

I know you can enable or disable services/devices based on different profiles (ie on battery, not on battery) im not sure if its active or just on boot, but you might wnat to look at that.

7/18/2007 7:29:53 AM

Sayer
now with sarcasm
9841 Posts
user info
edit post

Don't UPS systems usually come with software that does this sort of thing for desktops and servers?

I know it's not exactly the same thing, and maybe the program is reliant on coordination from the UPS to shut stuff down, or even the whole system for that matter.

Just a thought.

7/18/2007 7:34:21 AM

DirtyGreek
All American
29309 Posts
user info
edit post

I think with a UPS, there's a usb connection between the ups and computer that tells the computer it's now being run by the ups.

Not sure what kind of things that software can do, but it would be different from what this does anyway.

^ I'll look into the services on battery thing

7/18/2007 9:11:03 AM

DirtyGreek
All American
29309 Posts
user info
edit post

I've added more new features, such as the ability to disable during certain times of the day. Also made some visual tweaks.

7/19/2007 11:46:43 AM

quagmire02
All American
44225 Posts
user info
edit post

this is really cool

7/19/2007 12:05:58 PM

DirtyGreek
All American
29309 Posts
user info
edit post

glad to hear it. It's certainly been helping me out a bunch. Everyone with any ideas let me know!

7/19/2007 12:53:31 PM

MiniMe_877
All American
4414 Posts
user info
edit post

very nice, i'll check this out when I get home on my laptop.

what language did you write this in?

7/19/2007 1:10:21 PM

DirtyGreek
All American
29309 Posts
user info
edit post

vb using visual basic 2005 express

[Edited on July 19, 2007 at 1:17 PM. Reason : .]

7/19/2007 1:16:57 PM

 Message Boards » Tech Talk » Run apps on power connect / disconnect Page [1]  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.39 - our disclaimer.