-
June 4th, 2013, 11:38 AM
#1
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.
-
June 5th, 2013, 01:17 AM
#2
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.
-
June 5th, 2013, 02:16 AM
#3
Re: call a module from a form
Originally Posted by HanneSThEGreaT
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.
-
June 7th, 2013, 01:33 AM
#4
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.
-
June 8th, 2013, 05:15 PM
#5
Re: call a module from a form
Wanting to terminate ANY process should be a RED FLAG!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|