Get path to current module/EXE? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
JohnD
Earned some good credits
Earned some good credits
Posts: 118
Joined: Fri Nov 21, 2008 2:18 pm

Get path to current module/EXE?

Post by JohnD »

I can easily get the current working dir, but how can I get the path to the EXE that is running? I can remember Win32 functions for it, but can't see the wx equivalent... does it exist? (v2.8.10)
frank_frl
Earned some good credits
Earned some good credits
Posts: 139
Joined: Sat Feb 18, 2006 1:41 pm
Location: Germany

Post by frank_frl »

Hi JohnD,


http://docs.wxwidgets.org/2.8/wx_wxstandardpaths.html

try this:

Code: Select all

wxStandardPaths std;
wxString exePath = std.GetExecutablePath();
Frank
WinXp SP3, OS X10.5.5; CodeLite, Dialog::Blocks, wxWidgets 2.8.10
jfm429
Experienced Solver
Experienced Solver
Posts: 78
Joined: Thu Mar 11, 2010 11:30 pm
Contact:

Re: Get path to current module/EXE?

Post by jfm429 »

JohnD wrote:I can easily get the current working dir, but how can I get the path to the EXE that is running? I can remember Win32 functions for it, but can't see the wx equivalent... does it exist? (v2.8.10)
On Windows you may have name.exe as a single binary file, but on the Mac the executable is in the app bundle, in "name.app/Contents/MacOS/name" - if you're doing something with the executable you need to make sure it will work on any platform. If you're trying to do something like move the .exe to a new location, make sure you get the .app bundle on the Mac for the analogous operation, and whatever is appropriate for Linux. If you need to actually access the executable, you need to use the file in *.app/Contents/MacOS/ and the appropriate file on Linux.

Hope this helps a bit. Maybe if you explained what you need to do with the executable?
"I invented the term Object-Oriented, and I can tell you I did not have C++ in mind."
- Alan Kay, inventor of Object Oriented Programming
JohnD
Earned some good credits
Earned some good credits
Posts: 118
Joined: Fri Nov 21, 2008 2:18 pm

Post by JohnD »

I think Frank gave me the answer, but sure I can give more detail.

When the app is run, the working dir might not be the location where the EXE (or *nix equivalent) is located - for example in Windows if you double-click a file associated with an app, the working dir is set as the dir of the file. I want to be able to get the path of the running EXE to make sure I can load the data files.
And because I might have multiple versions of the app installed, I can't just look in the registry, I want to know exactly which EXE is running so I can obtain a path information from that.
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

JohnD wrote:I think Frank gave me the answer, but sure I can give more detail.

When the app is run, the working dir might not be the location where the EXE (or *nix equivalent) is located - for example in Windows if you double-click a file associated with an app, the working dir is set as the dir of the file. I want to be able to get the path of the running EXE to make sure I can load the data files.
And because I might have multiple versions of the app installed, I can't just look in the registry, I want to know exactly which EXE is running so I can obtain a path information from that.
Good, then wxStandardPaths is really the way to go. It has methods to get data path, which you likely want to use over simply getting the executable path
"Keyboard not detected. Press F1 to continue"
-- Windows
Allonii
Experienced Solver
Experienced Solver
Posts: 83
Joined: Wed Jun 24, 2009 1:28 pm

Post by Allonii »

frank_frl wrote:Hi JohnD,


http://docs.wxwidgets.org/2.8/wx_wxstandardpaths.html

try this:

Code: Select all

wxStandardPaths std;
wxString exePath = std.GetExecutablePath();
Frank
You shouldn't allocate and instance of wxStandardPaths, use

Code: Select all

::wxStandardPaths::Get().GetExecutablePath();
wxDocs
Note that you don't allocate an instance of class wxStandardPaths, but retrieve the global standard paths object using wxStandardPaths::Get on which you call the desired methods.
Thanks

Allonii
JohnD
Earned some good credits
Earned some good credits
Posts: 118
Joined: Fri Nov 21, 2008 2:18 pm

Post by JohnD »

Thanks for that. It seemed a bit weird to instance the class but I hadn't spotted that in the docs.
Post Reply