I am looking for info on how to block an application from being run by application name. I need to write a program to block as Solitaire from being run on PC's here at work.
Printable View
I am looking for info on how to block an application from being run by application name. I need to write a program to block as Solitaire from being run on PC's here at work.
Code:Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const SW_SHOWNORMAL = 1
Const WM_CLOSE = &H10
Private Sub Form_Load()
Timer1.Enabled = True
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Timer1.Enabled = False
End Sub
Private Sub Form_Resize()
Me.Hide
End Sub
THis is just an idea...
set the timer interval around 200 msec...
Private Sub Timer1_Timer()
Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String
'Ask for a Window title
Ret = "Solitaire" '= InputBox("Enter the exact window title:" + Chr$(13) + Chr$(10) + "Note: must be an exact match")
'Search the window
WinWnd = FindWindow(vbNullString, Ret)
If WinWnd = 0 Then
Exit Sub
End If
'close it...
PostMessage WinWnd, WM_CLOSE, 0&, 0&
End Sub
Nice Code Cimperiali.
Just a suggestion, instead of running a program in the background that could kill the solitare program, why not just delete it.
Roger
They could easily copy it back... :)
This is more of a networking type thing, but couldn't you write some sort of script that deletes solitaire (if it is there) every time a user logs onto their workstation - they'd soon tire of copying it back...
Have to agree with sotoasty, that's a nice little bit of code Cimperalli - I've added it to my own little code library!
Cheers.
Absolutely.Quote:
Originally posted by lsmeteor
They could easily copy it back... :)
And they could kill the program that stops it from running.
However, in most cases like this, I find a well written policy coupled with the removal of the offending program usually suffices.
:D
Roger
About deleting the file: that might be done if file is in user pc and the code to be written is executed with sufficient priviledges. I have no time to write it right now, but I will think about it. Only remember it might be illegal, or used for a different purpouse...
A hint: you have to search for the name of exe binded to the process (it might have been renamed...)
Actually, it would be hard to use the DELETE idea. User could copy the file and renamed it to something else. Even better, they can even copy it to their own directory.
Unless, you set the policy to only allow user to run certain app, you're pretty much stuck.
Running a program on the background is the best way to do this. You can run the program as an administrator and therefore regular user won't be able to kill it. Even better if you can wrap it into Windows Service, this way user can really do nothing about it.
-Cool Bizs
Thanks Cimperiali the code works great! and thanks to everyone who replied.
I work(as IS supervisor) for a hospital with roughly 120 PC's, and some are Windows 95, some Windows 2000. I have a few pesky people that have been caught playing games(Solitaire mostly) which is against our policy.
Deleting/un-installing this game is futile because it can be reinstalled(windows) or even brought on a floppy and ran. I hope to run my app in the background to kill this, and other non-appropriate apps. I am now working on using a INI file making it easier for me to add new apps to the kill list as needed.
I am concerned about using a timer to check for the running process(Solitaire ECT) feeling it may impact performance. I am hoping to instead, check for the process only when a new process is started. This is why I had asked if there was an example of a Task Manager in VB, or a way to initiate my search for killable apps only when a new process is started.
Thanks again.
Here I post an example you should use with caution:
it deletes the Exe (whichever name it has) associated with the solitaire form that user is using.
It works on win 2000, professional and server
(the interesting part is how to retrieve path and name of exe from a window)
Hi Cimperiali,
Is there a way to use your first example for a window whose name changes... such as Internet Explorer. I can set the timer faster to kill IE before the name changes, but, as said before, I would be concerned with performance issues. Thank you for your code.
Relentless
You can do the contrary: list all processes, and when you find iexplorer, you can try to terminate the process...
A way to enumerate processes is shown in Api-Guide:
(to you the task to terminate the process you're intersted)
Code:Option Explicit
Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPTHREAD = &H4
Const TH32CS_SNAPMODULE = &H8
Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Const TH32CS_INHERIT = &H80000000
Const MAX_PATH As Integer = 260
Private 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
Private Declare Function CreateToolhelp32Snapshot Lib "Kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function Process32First Lib "Kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "Kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Sub CloseHandle Lib "Kernel32" (ByVal hPass As Long)
Private Sub Form_Load()
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
Dim hSnapShot As Long, uProcess As PROCESSENTRY32
Dim r As Variant
'Takes a snapshot of the processes and the heaps, modules, and threads used by the processes
hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0&)
'set the length of our ProcessEntry-type
uProcess.dwSize = Len(uProcess)
'Retrieve information about the first process encountered in our system snapshot
r = Process32First(hSnapShot, uProcess)
'set graphics mode to persistent
Me.AutoRedraw = True
Do While r
Me.Print Left$(uProcess.szExeFile, IIf(InStr(1, uProcess.szExeFile, Chr$(0)) > 0, InStr(1, uProcess.szExeFile, Chr$(0)) - 1, 0))
'Retrieve information about the next process recorded in our system snapshot
r = Process32Next(hSnapShot, uProcess)
Loop
'close our snapshot handle
CloseHandle hSnapShot
End Sub
WOW! Thanks, thats really nice.
Rel
Those Kpd-Team people made a great job...
Have a nice day
Cesare
Quote:
Originally posted by Cimperiali
Code:Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const SW_SHOWNORMAL = 1
Const WM_CLOSE = &H10
Private Sub Form_Load()
Timer1.Enabled = True
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Timer1.Enabled = False
End Sub
Private Sub Form_Resize()
Me.Hide
End Sub
THis is just an idea...
set the timer interval around 200 msec...
Private Sub Timer1_Timer()
Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String
'Ask for a Window title
Ret = "Solitaire" '= InputBox("Enter the exact window title:" + Chr$(13) + Chr$(10) + "Note: must be an exact match")
'Search the window
WinWnd = FindWindow(vbNullString, Ret)
If WinWnd = 0 Then
Exit Sub
End If
'close it...
PostMessage WinWnd, WM_CLOSE, 0&, 0&
End Sub
What if you only want to stop a certain process from starting? without deleting it by stoping it each time??