Solved

Too easy VB question...

Posted on 2000-03-20
4
164 Views
Last Modified: 2010-05-02
Hey ppl! I'm developing a VB application but the true is that I know nothing about it... I was using it and I think I can do something but I have a question for you, the experts. I think it's going to be very easy for you to answer so here is the question:
How can I run an .exe or a .zip file with some code in a Sub Button_Click()??
Thanks
 
0
Comment
Question by:atramb
4 Comments
 
LVL 1

Accepted Solution

by:
tkuppinen earned 10 total points
Comment Utility
The easiest way is to use the ShellExecute API.  Take a look in the help menu.  This function will open any file with an associated program.

or for exe's only try

Shell Function Example
This example uses the Shell function to run an application specified by the user.

' Specifying 1 as the second argument opens the application in
' normal size and gives it the focus.
Dim RetVal
RetVal = Shell("C:\WINDOWS\CALC.EXE", 1)   ' Run Calculator.


This is straight from the help file.
0
 
LVL 1

Expert Comment

by:avitalk
Comment Utility
you have two choices:
Use the VB function Shell
Shell sFilePath

or ( I prefer this way ) use the API function ShellExecute.
to know more about how to use it goto the example in MSDN:
http://support.microsoft.com/support/kb/articles/Q170/9/18.asp

good luck
0
 
LVL 3

Expert Comment

by:Foyal
Comment Utility
   
SUMMARY
This article shows you how use API functions to programmatically close a single instance of most Windows programs through your Visual Basic program. You can use this feature to start a program, run the program without any user interaction, and shut down the program when all the program tasks are completed.
MORE INFORMATION
To programmatically close a single instance of a Windows program, you begin by getting the handle to the Window that you want to close with the FindWindow function. The FindWindow API function returns the handle of a top-level window whose class name and window name matches the string parameters. This function returns the handle of the window as a long value if it is successful and a null if it fails. To use this function, you must supply two parameters:
lpClassName - a pointer to a null-terminated string that specifies the class name or is an atom that identifies the class-name string. In this application, you can pass vbNullString.

lpWindowName - a pointer to a null-terminated string that specifies the window name (the window's title).

Use the handle to send a message to close the Window with the PostMessage API. The PostMessage API function sends a message to an application message queue and returns a value immediately. This function returns a non-zero value if the function is successful and zero if the function fails. To use this function, you must specify four parameters:
hWnd - the handle of the window you want to close. This long value is available as the result of the FindWindow function.

uInt - the message to post to the message queue. In this application, you would use the WM_CLOSE message.

wParam - the first message parameter. For this message, you should pass a null value.

lParam - the second message parameter. For this message, you should pass a null value.

A synchronization problem can occur when you send a message to close in the event and the program terminates before the message is processed. The WaitForSingleObject function makes sure the message is processed. To use this function, you must specify two parameters:
hHandle - the handle of the object you want to monitor.

dwMilliseconds - specifies the time-out interval, in milliseconds. If you set this parameter to INFINITE, the function's time-out interval never elapses.

To make sure that the program is closed, use the IsWindow function to determine if the handle still exists. The IsWindow function determines if the handle is valid. The function returns a non-zero value if the handle is valid and zero if the handle does not exists. To use this function, you just have to specify the handle to check.

If the handle still exists, then you can use the TerminateProcess function to stop the handle process. However, this extreme approach is not recommended because while you may have stopped the handle process, the DLL's used by the handle may still reside in memory, causing a memory leak. Use the TerminateProcess function with caution.

The next section shows how to create a sample program that demonstrates how to close a running application from a Visual Basic program. The program starts and stops the Windows calculator program.








 Option Explicit

      Private Declare Function WaitForSingleObject Lib "kernel32" _
         (ByVal hHandle As Long, _
         ByVal dwMilliseconds As Long) As Long

      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, _
         ByVal lParam As Long) As Long

      Private Declare Function IsWindow Lib "user32" _
         (ByVal hwnd As Long) As Long

      'Constants used by the API functions
      Const WM_CLOSE = &H10
      Const INFINITE = &HFFFFFFFF

      Private Sub Form_Load()
         Command1.Caption = "Start the Calculator"
         Command2.Caption = "Close the Calculator"
      End Sub

      Private Sub Command1_Click()
      'Starts the Windows Calculator
         Shell "calc.exe", vbNormalNoFocus
      End Sub

      Private Sub Command2_Click()
      'Closes the Windows Calculator
         Dim hWindow As Long
         Dim lngResult As Long
         Dim lngReturnValue As Long

         hWindow = FindWindow(vbNullString, "Calculator")
         lngReturnValue = PostMessage(hWindow, WM_CLOSE, vbNull, vbNull)
         lngResult = WaitForSingleObject(hWindow, INFINITE)

         'Does the handle still exist?
         DoEvents
         hWindow = FindWindow(vbNullString, "Calculator")
         If IsWindow(hWindow) = 1 Then
            'The handle still exists. Use the TerminateProcess function
            'to close all related processes to this handle. See the
            'article for more information.
            MsgBox "Handle still exists."
         Else
            'Handle does not exist.
            MsgBox "Program closed."
         End If
      End Sub
0
 

Author Comment

by:atramb
Comment Utility
Only comments
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now