Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can you create button on a form to call another executable

Status
Not open for further replies.

Mandy_crw

Programmer
Jul 23, 2020
578
PH
Hi everyone...may i ask help from you? I have a form that i want to include a button that when clicked it will close that form and open another executable? thanks...
 
Hi Mandy,

What do you understand by "open another executable"?

MarK
 
Hi mark... I want to close the form when a button is clicked and it will open new application/program that is build in VFP9.
 
Use ShellExecute and then quit

Code:
BROWSER("C:\MYPATH\TheAppIWantToRun.EXE")
QUIT


** BROWSER IS THE ENCAPSULATION OF ShellExecute that I use
FUNCTION BROWSER
	PARAMETER m.FILENAME,m.LINEPARAMS,m.TESTFILE
	LOCAL LNRETVAL, LCOPERATION
	PRIVATE m.FILENAME,m.LINEPARAMS,m.TESTFILE
	LCOPERATION = "Open"
	**
	DECLARE INTEGER ShellExecute IN SHELL32.DLL ;
		INTEGER handle,;
		STRING @sFile,;
		STRING @lp,;
		STRING @DIR,;
		STRING @dir1,;
		INTEGER ncmd
	**
	IF PCOUNT() < 3
		m.TESTFILE = .T.
	ENDIF
	IF PCOUNT() < 2
		m.LINEPARAMS = ""
	ENDIF
	IF m.TESTFILE
		IF MYFILE(m.FILENAME)
			LNRETVAL = SHELLEXECUTE(0, LCOPERATION, m.FILENAME, m.LINEPARAMS, "", 1)
		ELSE
			MESSAGEBOX("Unable to locate the File:"+m.FILENAME,48,"Problem")
		ENDIF
	ELSE
		LNRETVAL = SHELLEXECUTE(0, LCOPERATION, m.FILENAME, m.LINEPARAMS, "", 1)
	ENDIF
	CLEAR DLLS SHELLEXECUTE
	RETURN(.F.)

** BROWSER relies on MyFile
FUNCTION MYFILE
	PARAMETER m.FILENAME,m.RETNAME,m.RETSIZE
	PRIVATE m.FILENAME,m.RETNAME,m.RETVAL
	IF PCOUNT() < 3
		m.RETSIZE = .F.
	ENDIF
	IF PCOUNT() < 2
		m.RETNAME = .F.
	ENDIF
	IF m.RETSIZE
		m.RETVAL = 0
	ELSE
		IF m.RETNAME
			m.RETVAL = ""
		ELSE
			m.RETVAL = .F.
		ENDIF
	ENDIF
	IF !EMPTY(m.FILENAME)
		IF ADIR(TMPDIRFILES,m.FILENAME) > 0
			IF m.RETSIZE
				m.RETVAL = TMPDIRFILES(1,2)
			ELSE
				IF m.RETNAME
					m.RETVAL = TMPDIRFILES(1,1)
				ELSE
					m.RETVAL= .T.
				ENDIF
			ENDIF
		ENDIF
		RELEASE TMPDIRFILES
	ENDIF
	RETURN(m.RETVAL)

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
ShellExecute() is definitely the way to do this. As Griff says, just call ShellExecute() and then quit. You would do that in the Click event of the button.

For more information about how to do this, see my artilce, Introducing ShellExecute().

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
On second thoughts, it's not quite that simple.

For ShellExecute(), all you need to do is this:
Code:
DECLARE INTEGER ShellExecute IN shell32.dll ; 
  INTEGER hndWin, ; 
  STRING cAction, ; 
  STRING cFileName, ; 
  STRING cParams, ;  
  STRING cDir, ; 
  INTEGER nShowWin

lcFile = "MyExec.EXE"  && put the name of your executable here

ShellExecute((0,cAction,lcFile,"","",1)

That will take care of launching the executable. You would then simply QUIT. However, before you do so:

- If you have a modal form open, close it first.

- If you are in an event loop (with READ EVENTS), come out of it (with CLEAR EVENTS).

- Check for any unsaved data.

There might be one or two other issues, but those are the main ones to consider.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Steve is right that RUN /N can be used instead of ShellExecute(). But ShellExecute() is usually preferred because it has a couple of advantages over RUN /N. In particular:

- You can open or play a file or a document without knowing which application it is associated with. For example, you can play an MP3 without knowing which music player is installed.

- You can find out if the operation worked or not, and, if not, what caused it to fail. You can't do that with RUN /N.

Neither of those advantages are relevant in this case, but they are worth keeping in mind. It's why most developers prefer to use ShellExecute().

Mandy, whichever of the two methods you use, my remarks (above) about using QUIT still apply.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Thanks, Mike!

1. I deleted the wrong one. Oh well..

2. I see the file association and error trapping advantages you mention. I have always gone the long way around and preceded the file to run with the application name, e.g. something like this:
Code:
cCmd = 'C:\Windows\Notepad.exe E:\Docs\MyFile.txt'
RUN /N &cCmd

Steve
 
Steve,

There is nothing wrong with your approach. If it's just a question of running a named executable (as opposed to opening or "playing" a document), RUN /N is as goos as any.

Come to think of it, the RUN command has one advantage over ShellExecute(): You can use it to run a program modally. By leaving out the /N, you can tell the VFP application to pause until the executed program finishes. There might be cases where you would want to do that. As far as I know, you can't achieve that with ShellExecute().

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Mike,

Other than the "pause", there's apparently nothing to lose using ShellRxecute() instead of RUN, as you pointed out. I had just not known enough about it. Thanks for all your help on so many things!

Steve
 
Mandy,

Mandy said:
it will open new application/program

What is this? An .app or an .exe ?
If it is an .app you can simply start it with do myApp.app
If it is an .exe you should use the shelleexcute() approach
Stay healthy,
Koen
 
Thanks everyone for all you answers.... I will be trying to use the Shell.... Thanks again...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top