• Quick note - the problem with Youtube videos not embedding on the forum appears to have been fixed, thanks to ZiprHead. If you do still see problems let me know.

Opening progs from inside VisualBasic

Smike

Master Poster
Joined
Jan 22, 2004
Messages
2,095
Thanks for dropping in.

I would like to create a program that opens specific programs when it runs, then ends. Then when I run it again, it closes the progs.

(that is, assuming there isn't something already available, but I did have a look and couldn't see anything)

I want this so that I can set it up to load the various anti-virus/firewall etc. when I'm connected, but allow me to turn them off again to get best performance while playing games (with the cable pulled out of the wall).

I can do simple programming in VisualBasic6, and can probably do the neccessary IF's and whatever, but I don't know how (if it's possible) to open/close programs from inside VB.

Unfortunately, I don't have any books or the MSDN help CD.

Anyone?
 
Last edited:
Shell is a fine way to open the apps in VB6. If you are using VB.NET, the .NET API for doing this is System.Diagnostics.Process.Start(). The System.Diagnostics.Process object has a CloseMainWindow() function which will attempt to close the application gracefully.

If you are using VB6, you will need to send a WM_CLOSE message to the main window of the process to close it. VB6 code to do that would look _something_ like this:

----

Private Const WM_CLOSE As Long = &H10

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal Msg As Long, ByVal lParam As Long, ByVal wParam As Long) As Long

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Public Sub CloseWindow(ByVal WindowName As String)
Dim hwnd As Long
hwnd = FindWindow("", "Notepad")
If hwnd > 0 Then
SendMessage hwnd, WM_CLOSE, 0, 0
End If
End Sub

FindWindow is just one way to find the right window to close. But as you've probably figured out by now, this is a lot easier using one of the .net languages.
 
Shouldn't this:
hwnd = FindWindow("", "Notepad")

...read like this:
hwnd = FindWindow("", WindowName)

?
 
You're correct.

That code depends on the title of the window so it's far from foolproof--there are better ways to do it that are more involved.

You can get to MSDN online here:
http://msdn.microsoft.com

You don't need the CD to get the platform documentation.
 

Back
Top Bottom