scott8
08-26-2009, 05:46 PM
im new to visual basic, and well im looking to have a program launcher where when you click it two options appear, say one option says main.exe and the other main2.exe and when they click go the .exe they choose to launch will open.
can anyone help me do this? and explain how to open a file using the coding there? as each time i've tried it says the file is already open when it isn't, will appreciate any help
one2fight4
08-26-2009, 07:22 PM
what you could do is...
Dim SystemShell
Set SystemShell = CreateObject ("WSCript.shell")
SystemShell.run ("ProgramPath/Name.exe")
AtmaWeapon
08-26-2009, 08:00 PM
That's not even VB .NET, one2fight4.
scott8, have a look at the documentation for the System.Diagnostics.Process class. It has a good example and thorough discussion of its use. There's probably been 4 or 5 threads about using it in the past 2 weeks, so search these forums for further examples.
I'm curious what you mean by the program telling you a file is already open. This doesn't happen for executables. Could you elaborate what you mean?
scott8
08-27-2009, 04:10 AM
i tried
Private Sub Command2_Click()
System.Diagnostics.Process.Start ("C:\filename.exe")
End sub
now its coming back with "runtmie error 424 object required"
how would i launch the file as a hidden process? yet solving this problem?
AtmaWeapon
08-27-2009, 08:03 AM
That... makes no sense. VB .NET doesn't throw runtime errors like that. Also, a button click handler in VB .NET would have looked like this:
Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2_Click
End Sub
Could you confirm which version of Visual Basic you are using? If it's VB6 you should have posted in the "legacy VB" forums farther down.
one2fight4
08-27-2009, 12:48 PM
That's not even VB .NET, one2fight4.
umm, im not sure what u would call it then becouse I use it in Visual Basic 2008 all the time...
AtmaWeapon
08-27-2009, 12:58 PM
Let me clarify then.
Dim SystemShell
.NET naming guidelines suggest naming variables using camelCase. .NET usage guidelines recommend always specifying a type for a variable, even if that type is Object. If you had Option Strict on, this line would cause a compiler error.
Set SystemShell = CreateObject ("WSCript.shell")
There's no reason to use Set in VB .NET. I may be mistaken, but I think it wasn't necessary in VB6 either. CreateObject() is used to create COM objects. WSCript.shell is a Windows Script Host object. Not one bit of this line looks like VB .NET code; this is how a VB6 programmer would solve the problem in VB6 (though most people who post the VB6 method seem to use the Shell() function instead.)
I have no doubts this could compile and run with the appropriate settings in VB .NET, but that doesn't make it good VB .NET code. When you use this code, VB .NET has to perform interop with COM, which carries a little bit of a performance penalty. This isn't a case where it matters much because it's a matter of milliseconds, but it's still not correct. The bad analogy for this situation is as follows: You wanted to drive a nail. You know your toolbox has a hammer in it, but instead you drive to your friend's house 5 miles away and borrow his hammer. Then you drive back home and use your friend's hammer to drive the nail. "It works" isn't always a good excuse.
Still, it seems like your psychic powers are stronger than mine. I'm almost positive that the OP is using VB6 at this point, and as far as I know that makes your answer more correct, though I suspect VB6 veterans would suggest the Shell() function instead.
one2fight4
08-27-2009, 03:16 PM
Ok, I just pulled it from a .net program i had and saw that it worked.
Cerian Knight
08-28-2009, 08:03 PM
@scott8: To get back on track, since you are apparently using VB5 or VB6, have you tried something like this:
Option Explicit
Private Sub Command1_Click()
Dim ret As Variant 'task ID
ret = Shell("c:\path\filename1.exe", vbNormalFocus)
End Sub
Private Sub Command2_Click()
Dim ret As Variant 'task ID
ret = Shell("c:\path\filename2.exe", vbNormalFocus)
End Sub