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

Threaded View

  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.

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