tarquel Posted January 4, 2005 Posted January 4, 2005 Hi allBefore 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. defragSo, 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 /exampleI 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.
Spyder2k Posted January 4, 2005 Posted January 4, 2005 'Run Application with parametersShell "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
tarquel Posted January 4, 2005 Author Posted January 4, 2005 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.OddRegards,Nath
jdoe Posted January 4, 2005 Posted January 4, 2005 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.basPrivate Const INFINITE = &HFFFFPrivate Const SYNCHRONIZE = &H100000Private 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 LongPrivate 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 SubErr_RunCmd: Err.ClearEnd SubSyntax--------RunCmd "setup.exe /s /noreboot /example", vbHidevbHide - Hide your app executionThe default vbNormalFocus - Your program is visible and have focusAn other solution is to use WScript.Shell objectDim WSS As ObjectSet WSS = WScript.CreateObject("WScript.Shell")WSS.Run "setup.exe /s /noreboot /example", 1, TrueHoping this help
tarquel Posted January 5, 2005 Author Posted January 5, 2005 Hi jdoeGreat 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", vbNormalFocuswhich worked great and performed the silent mode it was meant to.However, if I try:RunCmd "c:\test.txt", vbNormalFocusit 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 = 1Const SW_SHOWMIN = 2 ' Edited this bit - seems to workConst SW_SHOWMAX = 3 ' although its abit pointless really hehePrivate 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 Subbut 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.
jdoe Posted January 5, 2005 Posted January 5, 2005 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 ExplicitPublic Const SW_SHOWNORM = 1Public Const SW_SHOWMIN = 2Public Const SW_SHOWMAX = 3Public 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 LongBut 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).
tarquel Posted January 5, 2005 Author Posted January 5, 2005 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 heheNow 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
samuel-t Posted January 15, 2005 Posted January 15, 2005 Keep them private in the module, but make the sub 1.) not a sub but a function, and 2.) public. Like this:Option ExplicitPrivate Const SW_SHOWNORM = 1Private Const SW_SHOWMIN = 2Private Const SW_SHOWMAX = 3Private 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 LongPublic Function OpenThisFile(ByVal WhichFilePath As String) As Long OpenThisFile = ShellExecute(0, "Open", WhichFilePath, 0, 0, SW_SHOWNORM)End SubAnd 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 IfEnd SubOh, 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
tarquel Posted January 25, 2005 Author Posted January 25, 2005 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 IfThat opened up the file great but i also got the message box too hehe :SWeird 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 heheRegarding 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/installingIs 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 heheI'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.
goodsyntax Posted January 27, 2005 Posted January 27, 2005 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 IfThat opened up the file great but i also got the message box too hehe :SconsoleConvertVB6.zip
kwhitefoot Posted February 27, 2005 Posted February 27, 2005 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
recordd Posted July 20, 2006 Posted July 20, 2006 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 IfThat opened up the file great but i also got the message box too hehe :STried 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 IfHope that helps.~Scott
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now