Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Too easy VB question...

Posted on 2000-03-20
4
Medium Priority
?
173 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 30 total points
ID: 2637376
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
ID: 2637405
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
ID: 2638601
   
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
ID: 2673298
Only comments
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

578 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