Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account



Photo

[VB6] Lauching a program or file with params

- - - - -

  • Please log in to reply
11 replies to this topic

#1
tarquel

tarquel

    The Well-wisher

  • Member
  • PipPipPipPipPip
  • 838 posts
  • Joined 03-March 04
Hi all

Before I fully ask the question I've got, I'll explain what I'm attempting to achive so you can see what I'm planing...

Basically, a CD menu system that runs off the cd [autorun's] (yes, I know there loads of those type of programs out there, I just want to design my own - a little exe app which can be run from cd :))

What I'm planing for in this app is to have different categories - etc. but the bulk of it will be command buttons which will... either launch a installation program, run a program direct from cd or run something from the system its being used on i.e. defrag

So, thats the plan so here's the question...

How can I launch a program, cmd, txt, html, bmp, etc. using VB6?
Also, how can I launch it (a program or cmd) with paramaters/switches also? i.e.

setup /s /noreboot /example


I know there has been posts that cover "some" aspects like this but I didnt find anything that gave me the right answers.

I can find my way around VB in a fairly basic way - but have only just picked up learning it and cant do the "hard stuff" yet so if there is a simple solution to this, please tell me.

And as a side question, I'm wondering how I'd build the final exe which would run on Win 98 and above? Would the VB Runtime files need to be installed in order to load up the program or is there a way around that?

Many thanks for your time,
Nath.
Enterprise Development Officer @ CCC (gov) - MS SB & "Freelance" repairer
My Rig: [color="purple"]Antec S Lanboy + 430W PSU[/color]-AMD "barton" CPU-MSI KT6-HyperX 512MB-SB Audigy 2 ZS P.Pro + TD7700-AIW 9800SE & My notebook: Acer TravelMate 8104WLMi - reviewed here
Posted Image


How to remove advertisement from MSFN

#2
Spyder2k

Spyder2k

    Member

  • Member
  • PipPip
  • 144 posts
  • Joined 07-July 04
'Run Application with parameters
Shell "C:\Directory\Program.exe Parameter1"

If I remember correctly, the ShellExecute API is used to start a file with it's registered application.

As for the runtimes, I believe they were shipped wih all the OS's besides 95, 98 (not sure about ME)

VB6 Runtimes Installation --> hxxp://www.microsoft.com/downloads/details.aspx?familyid=7b9ba261-7a9c-43e7-9117-f673077ffb3c&displaylang=en

#3
tarquel

tarquel

    The Well-wisher

  • Member
  • PipPipPipPipPip
  • 838 posts
  • Joined 03-March 04
I'll give it a go when I get home tonight - hope its as simple as that :)

I've tried something similar but i think maybe that was aimed at VB.NET code (someone pm me whether its worth getting VB.net - ie VB.NET VS VB6 thing)

I've used some very far fetched thing that works for me at the moment. I cant reel it off right now obviously, but it lauches txt and cmd/bat files EXCEPT that (on the exe I tried) it wouldnt load it and I couldnt put parameters/switches after the app name either.

Odd

Regards,
Nath
Enterprise Development Officer @ CCC (gov) - MS SB & "Freelance" repairer
My Rig: [color="purple"]Antec S Lanboy + 430W PSU[/color]-AMD "barton" CPU-MSI KT6-HyperX 512MB-SB Audigy 2 ZS P.Pro + TD7700-AIW 9800SE & My notebook: Acer TravelMate 8104WLMi - reviewed here
Posted Image

#4
jdoe

jdoe

    Advanced Member

  • Member
  • PipPipPip
  • 314 posts
  • Joined 02-May 04
The problem with Shell command in VB6 is that it don't wait before the launched program exit before executing next code line.

There's a piece of code that you can use as a ShellWait command.
I'm not sure but I think if you put MSVBVM60.DLL in the same directory as your APP.EXE then your program will work even if the runtime is not include with the OS (need to be confirm).


modShellWait.bas
Private Const INFINITE = &HFFFF
Private Const SYNCHRONIZE = &H100000

Private Declare Sub WaitForSingleObject Lib "kernel32.dll" _
    (ByVal hHandle As Long, ByVal dwMilliseconds As Long)

Private Declare Function OpenProcess Lib "kernel32.dll" _
    (ByVal dwDA As Long, ByVal bIH As Integer, ByVal dwPID As Long) As Long

Private Declare Sub CloseHandle Lib "kernel32.dll" (ByVal hObject As Long)

Public Sub RunCmd(CmdPath As String, _
    Optional WindowStyle As VbAppWinStyle = vbNormalFocus)

    Dim hProcess As Long

    On Error GoTo Err_RunCmd

    hProcess = OpenProcess(SYNCHRONIZE, 0, Shell(CmdPath, WindowStyle))

    If hProcess Then
        WaitForSingleObject hProcess, INFINITE
        CloseHandle hProcess
    End If

    Exit Sub

Err_RunCmd:

    Err.Clear

End Sub

Syntax
--------

RunCmd "setup.exe /s /noreboot /example", vbHide

vbHide - Hide your app execution
The default vbNormalFocus - Your program is visible and have focus


An other solution is to use WScript.Shell object
Dim WSS As Object
Set WSS = WScript.CreateObject("WScript.Shell")
WSS.Run "setup.exe /s /noreboot /example", 1, True



Hoping this help

#5
tarquel

tarquel

    The Well-wisher

  • Member
  • PipPipPipPipPip
  • 838 posts
  • Joined 03-March 04
Hi jdoe

Great stuff - the modShellWait.bas module worked and had no problem with running a exe file using that method - here's an example of what I tried:

RunCmd "R:\temp\build\test\sb.exe /silent", vbNormalFocus

which worked great and performed the silent mode it was meant to.

However, if I try:

RunCmd "c:\test.txt", vbNormalFocus

it doesnt work.

I'm guessing that I have to use a different way for that right? The way I have found that seems to work is as follows:

[B]Declarations[/B]
Const SW_SHOWNORM = 1
Const SW_SHOWMIN = 2 ' Edited this bit - seems to work
Const SW_SHOWMAX = 3 ' although its abit pointless really hehe

Private Declare Function ShellExecute Lib "shell32.dll" _
    Alias "ShellExecuteA" (ByVal hWnd As Long, _
    ByVal lpOperation As String, ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) As Long

[B]cmdOpen_examplehtml[/B]
Private Sub cmdOpen_examplehtml_Click()
    Dim handle As Long
    handle = ShellExecute(0, "Open", "c:\test.html", 0, 0, SW_SHOWNORM)
End Sub

but I have to put that in each form I want to use it. Is there a module alternative - not that its a problem - good old cut and paste aint hard lol ;)

However, it does seem to open files that are registered in windows in their associated program which is what I'm after too.

So am i right that using both of these two method will allow me to open whatever I want - be it a app with switches, or a cmd/bat/html/jpg/txt/etc. file?

Or have I missed something? (please remember I'm basically a noob at VB at the moment - seems so much different 9 years ago - but that probably because it is.... well.... never did anything like this then lol ;)

Thanks so much for the help so far. Any other suggestions are welcome - including if you know whether jdoe's thought about putting MSVBVM60.DLL in the same directory as your APP.EXE, then please post a quick reply here for me/us.

Many thanks,
Nathan H.
Enterprise Development Officer @ CCC (gov) - MS SB & "Freelance" repairer
My Rig: [color="purple"]Antec S Lanboy + 430W PSU[/color]-AMD "barton" CPU-MSI KT6-HyperX 512MB-SB Audigy 2 ZS P.Pro + TD7700-AIW 9800SE & My notebook: Acer TravelMate 8104WLMi - reviewed here
Posted Image

#6
jdoe

jdoe

    Advanced Member

  • Member
  • PipPipPip
  • 314 posts
  • Joined 02-May 04
There's no need to copy paste ShellExecute API in all forms of your project.

Remove it from your forms and create a new module or copy it in modShellWait.bas and replace Private by Public.

Option Explicit

Public Const SW_SHOWNORM = 1
Public Const SW_SHOWMIN = 2
Public Const SW_SHOWMAX = 3

Public Declare Function ShellExecute Lib "shell32.dll" _
   Alias "ShellExecuteA" (ByVal hWnd As Long, _
   ByVal lpOperation As String, ByVal lpFile As String, _
   ByVal lpParameters As String, _
   ByVal lpDirectory As String, _
   ByVal nShowCmd As Long) As Long


But you will get the same wait problem. ShellExecute don't wait for command to terminate before executing next code line (but it seem to not be a problem on your project).

#7
tarquel

tarquel

    The Well-wisher

  • Member
  • PipPipPipPipPip
  • 838 posts
  • Joined 03-March 04
Hi again.

Right - gotcha :)

Will give that ago - in other words - use both types in order to achive everything.

- your method - will launch a app and wait for the application to end before returning/moving on to the next line of code
- the method i had - launches files based on what application they are registered with in windows i.e .txt, reg, bat, etc. but doesnt wait before executing the next line of code (Think it will launch a app with switches too - I never fully tested it)

Thanks for your help and hope this helps others too. Indeed your right when you say that i dont need it to wait as mostly everything I want to accomplish wont need to wait for the next line of code - as its mainly a slightly glorified app/tool launcher hehe

Now if only I knew whether adding the library file to the same directory as the app will work (negating the need for VB6 Runtime Files to be installed in windows). As I wont be doing anything more complex than what I've mentioned here - the app isnt gonna depend on much :) If anyone can comment on this, please feel free :)

Regards & many thanks again,
Nath
Enterprise Development Officer @ CCC (gov) - MS SB & "Freelance" repairer
My Rig: [color="purple"]Antec S Lanboy + 430W PSU[/color]-AMD "barton" CPU-MSI KT6-HyperX 512MB-SB Audigy 2 ZS P.Pro + TD7700-AIW 9800SE & My notebook: Acer TravelMate 8104WLMi - reviewed here
Posted Image

#8
samuel-t

samuel-t
  • Member
  • 5 posts
  • Joined 14-January 05
Keep them private in the module, but make the sub 1.) not a sub but a function, and 2.) public. Like this:

Option Explicit

Private Const SW_SHOWNORM = 1
Private Const SW_SHOWMIN = 2
Private Const SW_SHOWMAX = 3

Private Declare Function ShellExecute Lib "shell32.dll" _
  Alias "ShellExecuteA" (ByVal hWnd As Long, _
  ByVal lpOperation As String, ByVal lpFile As String, _
  ByVal lpParameters As String, _
  ByVal lpDirectory As String, _
  ByVal nShowCmd As Long) As Long

Public Function OpenThisFile(ByVal WhichFilePath As String) As Long
   OpenThisFile = ShellExecute(0, "Open", WhichFilePath, 0, 0, SW_SHOWNORM)
End Sub

And call it from every place that you want to call it from, such as your Command Button, like this:

Private Sub cmdOpen_examplehtml_Click()
    Dim r as Long
    r = OpenThisFile("c:\some\file\somewhere.txt")
    If r <> 0 Then
        MsgBox "Failed to open file."
    End If
End Sub

Oh, and NO NO NO -- it doesn't help anything in any way if you put your application in the same directory as the VB6 runtimes. It has nothing to do with that at all. Either the VB6 runtimes are available somewhere on the system where your application gets installed (and it is quite likely that they are) and then your application can run on that system, or they aren't available, in which case your application can't run on the system. You should, however, never (well, if you distribute on CD, sure, but not if you distribute over the web) include the VB6 runtimes in an installation file, but let the people who download your application know that if they don't have the VB6 runtimes installed (tell them how to check for it -- such things are always appreciated :) and provide them with a link fr download.

/Samuel

#9
tarquel

tarquel

    The Well-wisher

  • Member
  • PipPipPipPipPip
  • 838 posts
  • Joined 03-March 04
hehe now i'm getting real confused ;)

@Samuel:

  Dim r As Long
   r = OpenThisFile("c:\test.txt")
   If r <> 0 Then
       MsgBox "Failed to open file."
   End If

That opened up the file great :) but i also got the message box too hehe :S

Weird thing tho - when complied to a exe, it will open a exe or txt file no problem but it wont open a exe by just running/testing the project. Ah well, can live with that hehe

Regarding what you said about the VB runtimes, yes, i understand all that and i wouldnt install a app to where they are installed - it would be stupid yes hehe - but remember, that this is a standalone exe file which isnt gonna be "installed" anywhere. It's a exe on a CD as being used as a menu system for running stuff from the cd or from windows, so we were refering to placing the vb dll file on the cd where the app is located (at the root) to get the app to run on machines where the vb runtimes arent installed....

....how would i perform a check when my app opens/runs and how do i get it to install the vb runtimes if they arent present?

The trouble is a) This is all new to me B) i need it to open all sorts of files - txt, exe, jpg, bat, cmd, html, etc. c) some things will have switches after them, and d) will either wait or not wait for the app to finish running/installing

Is there some easy piece of code that will work for all this? I've got snippets from this page and now I cant remember which is what and what works best hehe

I'd like it in module form - thanks guys for showing the benefit of this and how it works - but i'm still at a loss really.

One seems to work for one thing where another doesnt.

Any full-proof code for all the above?

Yours hopefully,
Nath.
Enterprise Development Officer @ CCC (gov) - MS SB & "Freelance" repairer
My Rig: [color="purple"]Antec S Lanboy + 430W PSU[/color]-AMD "barton" CPU-MSI KT6-HyperX 512MB-SB Audigy 2 ZS P.Pro + TD7700-AIW 9800SE & My notebook: Acer TravelMate 8104WLMi - reviewed here
Posted Image

#10
goodsyntax

goodsyntax
  • Member
  • 4 posts
  • Joined 26-January 05
[quote name='it_ybd' date='Jan 25 2005, 02:07 PM'] hehe now i'm getting real confused ;)

@Samuel:

  Dim r As Long
   r = OpenThisFile("c:\test.txt")
   If r <> 0 Then
       MsgBox "Failed to open file."
   End If

That opened up the file great :) but i also got the message box too hehe :S

Attached Files



#11
kwhitefoot

kwhitefoot
  • Member
  • 1 posts
  • Joined 27-February 05
Thanks for the modShellWait.bas code. Just what I was looking for. I had tried the example from the API Guide but it didn't reliably wait. Mind you the API Guide is pretty good for most things APIGuide

#12
recordd

recordd
  • Member
  • 1 posts
  • Joined 20-July 06
[quote name='goodsyntax' date='Jan 26 2005, 10:30 PM' post='260644']
[quote name='it_ybd' date='Jan 25 2005, 02:07 PM'] hehe now i'm getting real confused ;)

@Samuel:

  Dim r As Long
   r = OpenThisFile("c:\test.txt")
   If r <> 0 Then
       MsgBox "Failed to open file."
   End If

That opened up the file great :) but i also got the message box too hehe :S
[/quote]

Tried this code on my own program. Got the same thing. Did some debugging and found that if it successfuly opens the file then 'r' will return '33' not '0', if it unsuccessfully opens the file, then 'r' will return '2'. The following code works flawlessly for me:

Dim r As String 'Variable to store the response string
	Msg = "Failed to open file!   "
	Style = vbOKOnly + vbCritical + vbDefaultButton1 + vbApplicationModal
	Title = "ERROR!"
	r = OpenThisFile("c:\test.txt")
	If r <> 33 Then
		Response = MsgBox(Msg, Style, Title)
	End If

Hope that helps.
~Scott




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users