Blocking an application by name.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Blocking an application by name.

  1. #1
    Join Date
    Apr 2003
    Location
    Dodge City Kansas
    Posts
    23

    Blocking an application by name.

    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.

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    For example

    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
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,451
    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

  4. #4
    Join Date
    May 2002
    Location
    Montreal
    Posts
    450
    They could easily copy it back...
    Cheers,
    Laurent

    For an aviator, the three best things in life are a good landing, a good orgasm, and a good sh*t. A night carrier landing is one of the few opportunities to experience all three at the same time.

  5. #5
    Join Date
    Sep 2002
    Location
    England
    Posts
    530
    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.

  6. #6
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,451
    Originally posted by lsmeteor
    They could easily copy it back...
    Absolutely.

    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.



    Roger

  7. #7
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Thanks for positive comments...

    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...)
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  8. #8
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167
    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

  9. #9
    Join Date
    Apr 2003
    Location
    Dodge City Kansas
    Posts
    23
    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.
    Last edited by Detector; April 30th, 2003 at 07:14 AM.

  10. #10
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Find exe from window and kill it in Win2k...

    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)
    Attached Files Attached Files
    Last edited by Cimperiali; October 2nd, 2003 at 03:50 AM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  11. #11
    Join Date
    Dec 2002
    Posts
    55
    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

  12. #12
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Then better you do the contrary...

    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
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  13. #13
    Join Date
    Dec 2002
    Posts
    55
    WOW! Thanks, thats really nice.

    Rel

  14. #14
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Wink Thanks to Api-guide...

    Those Kpd-Team people made a great job...
    Have a nice day
    Cesare
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  15. #15
    Join Date
    Sep 2003
    Posts
    15

    Re: For example

    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??

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center