user can illegal kill the second app also. I want to forbid any illegal kill my app
Main Topics
Browse All TopicsHi Experts
How can I automaticly restart my app as soon as it is illegal killed by end process in TaskManager (XP/NT/2K).
Thank
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
http://www.thescarms.com/v
Not to sound to facetous, but could you not have an app A than monitors app B, while app B is monitoring app A ? Using ctrl-alt-del, a user can only close one app at a time.
I am also working on a several programs that users might want to shut down illegally (security programs to prevent employees from accessing certain programs or children from getting on certain sites and such), so I have encountered the same problem. I have also been thinking along the lines of having two programs monitor each other, and each starts up the other if the other is shut down. One way to do this I can see is to have each program write a file to the disk, and the other erase it. For example, every 15 seconds or so, program A writes file ggg.txt (the name is arbitrary) to the Windows folder (or any other folder). Every 20 seconds program B checks to see if ggg.txt exists. If the file exists, Program B deletes it. If it does not exist, program B reruns program A. That way, if program A stops, the file is not there the next time that program B looks for it and program A gets restarted. Program B writes a similar file for A to look for. Likewise, program B writes a different file every 15 seconds, and Program A checks for this file every 20 seconds.
I can see several problems with this, such as the fact that it consumes a bit of system resources, but it might be worth it if it is really important to keep the programs running.
The problem with two applications watching each other is that both could be killed within the alloted span of time leaving neither in a position to restart the other. That may be unlikely, but it is possible. After some thought I've come up with three other possibilities. The first option is to use Windows' built-in task scheduler. By scheduling the watching task, i.e. the program that's to check and see if the critical program is running, to run periodically it makes it more difficult for a user to find and dispose of it. The watcher program runs just long enough to check and see if the production app is running. If it's not, then it restarts it. If it is, then it takes no action and terminates until the next scheduled run. Option two will only work if the computers are all operating on a network. The idea here is to use the Winsock control and have the watcher program running on a server or some other remote machine. Periodically the central process can query user machiens to see if the required app is running. If it isn't, then the central watcher app can start it remotely. This approach is better in that it separates the watcher app from the required app and makes it impossible for a user to halt them both. The downside though is that it requires bandwidth and, of course, won't be of any use for a PC not connected to the network. The final possibility is to stick some code in the unload event of the production app to launch an app that'll restart it. That'll only work if the app is shut down in a normal fashion though. It won't work if the app is killed via Task Manager. Maybe a hybrid approach that uses multiple methods to keep the production app running is best.
Here's a link to some code that lists running tasks. One of them also includes code that is supposed to start a task remotely. I can't vouch for whether it works or not.
http://www.freevbcode.com/
http://www.andreavb.com/ti
I have this same problem (people illegally killing the program), so I have come up with a solution. It does involve running two copies of the program, each of which monitors the other.
When you first run the program, it runs another copy of itself. (When the program runs the second copy, it includes a command line to tell the second copy that it is a second copy.) The programs give themselves different title bars to distinguish between them. They then monitor each other using API calls. When one disappears, the other relaunches it. The monitoring is done with the timer control, which can be set to very short intervals, too short for anyone to kill both in time. I have tested this with half second intervals, and been totally unable to kill both. Someone who is really concerned can shorten it further. Since the programs use API calls, it is not as resource intensive as my original suggestion of using files to communicate. My solution works very well. I can post code if thuannc is still interested in this. Since I plan to use it in several programs, I have put all the code and global constants in one portable module, with directions where to put links to the subs. (I create security programs designed to limit access to computers.)
Hi all
I think BlueDeviFan have some good idea but I more prefer Leithauser solution be cause if I monitor app. remotely user easy to disconnect computer from network and then kill my app. I do it myself before (like vinnyd97 or Leithauser solution) but I have small problem with communicating beetween two app. Could you please give me your sample to learn more, Leithauser.
Thank
Create a module (VB4 or higher) with this code in it:
Declare Function FindWindow& Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String)
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetForegroundWindow Lib "user32" () As Long
Global Const Name1 = "APT1" ' Caption to go on main form of instance of the program to be run first. Use any string you like.
Global Const Name2 = "APT2" ' Caption to go on main form of second instance of the program (which will be run by the first instance of the program). Use any string you like, different from the first one.
Global OtherCaption$
Sub Protect(frm As Form)
' a call to this sub goes in a timer on the main form. The code is "Protect Me". Set timer interval to about 500 (.5 seconds between triggers)
Static HoldiRet As Long
Dim iRet As Long, iRet2 As Long
Dim Buffer$
Dim BufLen&, JunkL&
iRet = FindWindow&(vbNullString, OtherCaption$)
If iRet = 0 Then
Shell App.Path & "\" & App.EXEName & ".exe " & OtherCaption$ & ":" & frm.Caption, 4
DoEvents
HoldiRet = FindWindow&(vbNullString, OtherCaption$)
Else
If HoldiRet <> 0 And HoldiRet <> iRet Then
iRet2 = GetForegroundWindow()
Buffer$ = Space$(255)
BufLen& = 254
JunkL& = GetWindowText(iRet2, Buffer$, BufLen&)
Buffer$ = Left$(Buffer$, InStr(Buffer$, Chr$(0)) - 1)
On Error Resume Next
AppActivate OtherCaption$
If Err Then
Err = 0
Shell App.Path & "\" & App.EXEName & ".exe " & OtherCaption$ & ":" & frm.Caption, 4
DoEvents
HoldiRet = FindWindow&(vbNullString, OtherCaption$)
Else
AppActivate Buffer$
End If
End If
End If
If iRet <> 0 Then HoldiRet = iRet
End Sub
Sub SetCaption(frm As Form)
' A call to this sub goes in the Form_Load module on the main form. The code is "SetCaption Me".
Dim S$, N As Integer
If Len(Command$) Then
S$ = Command$
N = InStr(S$, ":")
frm.Caption = Left$(S$, N - 1)
OtherCaption$ = Mid$(S$, N + 1)
Else
frm.Caption = Name1
OtherCaption$ = Name2
End If
End Sub
Note: It is VERY important to be able to shut down the program legitimately in order to be able to upgrade or uninstall the program. I cannot stress this too much!
In order to be able to shut down the program, you will need to have the first instance send a signal to the second instance telling it to end first.
In the KeyPress module of the main form, I include something like
If KeyAscii = Asc("X") then
If Me.Caption = Name1 Then
Timer1.Enabled = False ' Stop timer from restarting other Instance of the program
AppActivate Name2
Sendkeys "X", True
End
Else
Timer1.Enabled = False
End
End if
End If
This enables you to shut down the program legitimately by giving it the focus and pressing the X key. You should also include
If Me.Caption = Name1 Then
Timer1.Enabled = False
AppActivate Name2
Sendkeys "X", True
End
End If
in the legitimate Exit function of the program for the same reason.
Another possibility is to have some code in the program that searches for a certain file. If it finds the file, the program ends. Something like
If Len(Dir$("C:\NoRun.txt")) then End
You can put this in the timer module before the
Protect Me
statement. This will allow your uninstall program to create this file and then wait a few seconds for the program to terminate before it deletes the files from the computer.
(Use code like
Open "C:\NoRun.txt" for Output as 1
Close 1
StartCount! = Timer
Do
DoEvents
Loop until Timer - StartCount >5
Kill "C:\NoRun.txt"
to create this file and then wait.)
One last comment from me:
It is a good idea to include an
On Error Resume Next
command at the beginning of the Protect Sub. Otherwise, it occasionally generates an error when you close down Windows if Windows happens to try to close the program down at just the right (or should I say, wrong) point in hte protect proceedure. This appears to happen because Windows closes down the main form before it closes down the code in Protect, so Protect tries to refer to the form when it does not exist.
David Leithauser
Business Accounts
Answer for Membership
by: vinnyd79Posted on 2004-03-04 at 19:45:39ID: 10519841
I think you would have to have a second app running to monitor the first app and launch it if the process doesn't exist anymore.