CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2013
    Posts
    5

    Talking call a module from a form

    Can I call this module function form a form?

    Code:
    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
        (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function GetWindowThreadProcessId Lib "user32" _
        (ByVal hwnd As Long, ByRef lpdwProcessId As Long) As Long
    Private Declare Function OpenProcess Lib "Kernel32" _
        (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
        ByVal dwProcessID As Long) As Long
    Private Declare Function CloseHandle Lib "Kernel32" _
        (ByVal hObject As Long) As Long
    Private Declare Function TerminateProcess Lib "Kernel32" _
        (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    
    Private Const SYNCHRONIZE = &H100000
    Private Const PROCESS_TERMINATE As Long = &H1
    
    Public Sub terminateApp(ByVal sWindowTitle As String, ByVal fSilent As Boolean)
    
        ' Forcefully terminate a running application
        
        Dim lHwnd As Long
        Dim lProc As Long
        Dim lProcHnd As Long
        
        On Error GoTo errError1
        
        sWindowTitle = "Example"
        sWindowTitle = "Test"
        
        ' Get the target's window handle.
        lHwnd = FindWindow(vbNullString, sWindowTitle)
        If lHwnd = 0 Then
            If fSilent Then
                Exit Sub
            Else
                Err.Raise 1, , "Can't find window handle of application to terminate"
            End If
        End If
    
        ' Get the process
        GetWindowThreadProcessId lHwnd, lProc
        If lProc = 0 Then
            If fSilent Then
                Exit Sub
            Else
                Err.Raise 1, , "Can't get process ID of window handle"
            End If
        End If
    
        lProcHnd = OpenProcess(SYNCHRONIZE Or PROCESS_TERMINATE, 0, lProc)
        If lProcHnd = 0 Then
            If fSilent Then
                Exit Sub
            Else
                Err.Raise 1, , "Can't get process handle"
            End If
        End If
    
        ' Terminate Process
        If TerminateProcess(lProcHnd, 0&) <> 0 Then
            If Not fSilent Then
                Err.Raise 1, , "Failed to terminate process"
            End If
        End If
    
        ' Close the process.
        CloseHandle lProcHnd
        
        Exit Sub
    
    errError1:
        Err.Raise Err.Number, , Err.Description
    End Sub
    I need to call that module when the specified app is running, any suggestion for codes in the form?
    Last edited by blumoon; June 4th, 2013 at 12:09 PM.

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: call a module from a form

    Well, it is a Public sub, so, by definition, you should be able to call and use it from anywhere.

    If you give us a bigger picture of what you're actually trying to achieve, we could advise better.

  3. #3
    Join Date
    May 2013
    Posts
    5

    Unhappy Re: call a module from a form

    Quote Originally Posted by HanneSThEGreaT View Post
    Well, it is a Public sub, so, by definition, you should be able to call and use it from anywhere.

    If you give us a bigger picture of what you're actually trying to achieve, we could advise better.
    I want to put codes in "my application" that can terminate a process/processes when the specified processes is running, I want it active all the time when "my application" is running.

    but then I found this code that more simple than the above codes:

    Code:
    Private Sub TerminateProcess(app_exe As String)
            Dim Process As Object
            For Each Process In GetObject("winmgmts:").ExecQuery("Select Name from Win32_Process Where Name = '" & app_exe & "'")
                    Process.Terminate
            Next
    End Sub
    and put this code in cmdStart:
    Code:
    TerminateProcess ("notepad.exe")
    TerminateProcess ("explorer.exe")
    TerminateProcess ("blablabla.exe")
    my problem now is I don't know how to make cmdStart... (Feeling stupid)
    Last edited by blumoon; June 5th, 2013 at 03:18 AM.

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: call a module from a form

    The code in your first post is VB6 and is using the Windows API
    The code in the second post looks like Vb.Net

    So are you using VB6 or are you using one of the newer .Net VB versions?

    as for how to make cmdStart that is not much of a description but it implies the creation of a command button which you do simply by clicking on the button int he toolbox and then draging out a rectangle on the form for your button. You would then double click on the button and the code window for it should appear which is where you would put some code to make the button do what you want.
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: call a module from a form

    Wanting to terminate ANY process should be a RED FLAG!
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured