Help with ShellExecute to open program

Jan 19, 2017 at 8:41pm
Hi all. Fairly new to C++ first off. So I wrote a program that has a main menu with several calculators to choose from and after the user has input the data and the calculations have run, I have a prompt that shows up asking the user if they would like to return to the main menu or close the program. From reading on the forum I copied a command into my program that opens itself up and closes the old one if the user selects "Main Menu." The only way this works though is if I manually enter the programs file path into the ShellExecute command.

My question is, is there a way to make the program automatically recognize where it is saved on the computer that way I can reference the file path to open itself up in a new window as opposed to manually typing the file path that is it stored in? I want to be able to save the program anywhere on the computer and not have to worry about editing the code to manually input the file path to where it's stored. Below is an excerpt of the code I'm currently using. Again, I'm new so I'm not too familiar with what the ShellExecute command is actually doing because I copied and pasted it from another post on this forum.

cout << "What would you like to do?" << endl;
cout << endl;
cout << "1 - Main Menu" << endl;
cout << "2 - Exit" << endl;
cout << endl;
cin >> end_decision;

if (end_decision < 1 || end_decision > 3)
{
cout << "Invalid option. Please choose from one of the menu options and press enter." << endl;
cout << endl;
cin >> end_decision;

if (end_decision == 1)
{
ShellExecuteA(NULL, "open", "C:/VSCPP_Programs/Voltage_Divider_Calculator.exe", NULL, NULL, SW_NORMAL);
return 0;
}

else if (end_decision == 2)
{
return 0;
}
}

else if (end_decision == 1)
{
ShellExecuteA(NULL, "open", "C:/VSCPP_Programs/Voltage_Divider_Calculator.exe", NULL, NULL, SW_NORMAL);
return 0;
}

else if (end_decision == 2)
{
return 0;
}
Jan 20, 2017 at 3:39am
The only way I know to do that is put it in the path, such as C:\WINDOWS, or add the path where you place the command to the path variable.

type path /? for help on the path command.
Jan 20, 2017 at 1:01pm
If you want to find out the name and path of your program try this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <Windows.h>
#include <tchar.h>

using namespace std;

string GetAppName()
{
  string buffer(255, ' ');
  
  DWORD len = GetModuleFileNameA(0, &buffer[0], 255);
  buffer.resize(len);

  return buffer;
}

int main()
{
  string exe = GetAppName();
  cout << "Executable name: " << GetAppName() << "\n\n";
  system("pause");
  return 0;
}
Last edited on Jan 20, 2017 at 1:01pm
Jan 20, 2017 at 2:54pm
@Thomas1965:

That works on Windows only.

@OP:

This Stack Overflow question has some suggestions:

http://stackoverflow.com/questions/1528298/get-path-of-executable
Jan 20, 2017 at 3:47pm
That works on Windows only.

@MikeBoy,
since he uses ShelleExecute that should be fine.

I had a quick look at your link but it seems there is no reliable cross platform way.
Jan 20, 2017 at 7:15pm
@ Thomas1965

That works perfect for identifying the location of where the program is stored. However, I'm still a little trumped on how to incorporate that into the ShellExecute command so that the program opens itself in a new window and closes the old one.

Again, the overall intention is that when the user selects "Main Menu" that the program opens itself in another window, and closes the old one. And my current code works perfect for that except like I explained earlier, it only works if I manually type the file path into the ShellExecute command. Is there a way to assign the file path from the code you posted to a variable and insert that variable into the shellexecute command?

I also probably should have asked if there might be an easier way to accomplish what I'm trying to accomplish. I don't care how I accomplish it, I just want to basically restart the program when the user chooses main menu but I also want to be able to save the program anywhere on my computer without having to go in and manually edit the code to reflect the file path every time I move the file. End goal is i'd like to write a program, build an .exe version of it, and e-mail it to family members and friends. I want them to be able to download the program and not have to worry about going in and editing the source code to edit the file path where it is saved. I want to program to simply open itself again in another window and close the old window.
Jan 20, 2017 at 9:01pm
Is there a way to assign the file path from the code you posted to a variable and insert that variable into the shellexecute command?


Yes that's the easiest part.
1
2
3
4
std::string exename = GetAppName();
ShellExecuteA(0, "open", exename.c_str(), 0, 0, SW_NORMAL);
// try to close the old one
exit(0);

I also probably should have asked if there might be an easier way to accomplish what I'm trying to accomplish. I don't care how I accomplish it, I just want to basically restart the program when the user chooses main menu

If you show us the whole code someone might find an easier way.
Topic archived. No new replies allowed.