Create an vb exe without a form, just porcess code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bryan Holmstrom

    #1

    Create an vb exe without a form, just porcess code

    If have a vb project with a form and a button to process a kill application (shuts down acroRd32.exe). I need some help creating a project/exe that does not require a form to pop up, or a button to be clicked.

    Here is the code:
    Code:
    Private Sub Command1_Click()
     
    ' Sub TerminateProcess()
         '---------------------------------------------------------------------------------------
         '              : Terminates a process. First checking to see if it is running or not.
         '              : Uses WMI (Windows Management Instrumentation) to query all running processes
         '              : then terminates ALL instances of the specified process
         '              : held in the variable strTerminateThis.
         '              :
         '              : ***WARNING: This will terminate a specified running process,use with caution!.
         '              : ***Terminating certain processes can effect the running of Windows and/or
         '              : ***running applications.
         '---------------------------------------------------------------------------------------
        Dim strTerminateThis As String 'The variable to hold the process to terminate
        Dim objWMIcimv2 As Object
        Dim objProcess As Object
        Dim objList As Object
        Dim intError As Integer
         
        strTerminateThis = "acroRd32.exe" 'Process to terminate,
         'change notepad.exe to the process you want to terminate
         
        Set objWMIcimv2 = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\.\root\cimv2") 'Connect to CIMV2 Namespace
         
        Set objList = objWMIcimv2.ExecQuery _
        ("select * from win32_process where name='" & strTerminateThis & "'") 'Find the process to terminate
         
         
        If objList.Count = 0 Then 'If 0 then process isn't running
            MsgBox strTerminateThis & " is NOT running." & vbCr & vbCr _
            & "Exiting procedure.", vbCritical, "Unable to continue"
            Set objWMIcimv2 = Nothing
            Set objList = Nothing
            Set objProcess = Nothing
            Exit Sub
        Else
             'Ask if OK to continue
            Select Case MsgBox("Are you sure you want to terminate this running process?:" _
                & vbCrLf & "" _
                & vbCrLf & "Process name: " & strTerminateThis _
                & vbCrLf & "" _
                & vbCrLf & "Note:" _
                & vbCrLf & "Terminating certain processes can effect the running of Windows" _
                & "and/or running applications. The process will terminate if you OK it, WITHOUT " _
                & "giving you the chance to save any changes in anything that is running in the specified process above." _
                , vbOKCancel Or vbQuestion Or vbSystemModal Or vbDefaultButton1, "WARNING:")
                 
            Case vbOK
                 'OK to continue with terminating the process
                For Each objProcess In objList
                     
                    intError = objProcess.Terminate 'Terminates a process and all of its threads.
                     'Return value is 0 for success. Any other number is an error.
                    If intError <> 0 Then
                        MsgBox "ERROR: Unable to terminate that process.", vbCritical, "Aborting"
                        Exit Sub
                    End If
                Next
                 'ALL instances of specified process (strTerminateThis) has been terminated
                Call MsgBox("ALL instances of process " & strTerminateThis & " has been successfully terminated.", _
                vbInformation, "Process Terminated")
                 
                Set objWMIcimv2 = Nothing
                Set objList = Nothing
                Set objProcess = Nothing
                Exit Sub
                 
            Case vbCancel
                 'NOT OK to continue with the termination, abort
                Set objWMIcimv2 = Nothing
                Set objList = Nothing
                Set objProcess = Nothing
                Exit Sub
            End Select
        End If
         
    End Sub
    Of course I will remove the messageboxes once its running correctly without intervention.

    I thank anyone in advance for their help.

    Bryan Holmstrom
    Last edited by Frinavale; Oct 15 '10, 05:54 PM.
  • Guido Geurs
    Recognized Expert Contributor
    • Oct 2009
    • 767

    #2
    This Exe will create a "Settings.i ni" file in the folder of the program without seeing the window of the exe.

    Code:
    Private Sub Form_Load()
    Dim OUTPUTFN As Integer
       Visible = False
       OUTPUTFN = FreeFile
       On Error GoTo ErrWriting
       Open App.Path & "\settings.ini" For Output As #OUTPUTFN
          Write #OUTPUTFN, "This is a test"
       Close #OUTPUTFN
       Unload Me
    Exit Sub
    ErrWriting:
       Close #OUTPUTFN
       MsgBox ("There is an error writing the settings file !")
       Unload Me
    End Sub

    Comment

    Working...