# C++ equivalent to Application.StartupPath?

This topic is 3634 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Just a quick question: What is the Native C++ (non .NET) equivalent to Application.StartupPath? I've googled it with no luck...

##### Share on other sites
There is no standard equivalent. argv[0] is all you have.

##### Share on other sites
In Windows, you can use GetModuleFileName() and lop off the file name to get just the path.

##### Share on other sites
Okay, with GetModuleFileName, what do I pass as the hModule? I've never used that type before... also what is argv[0]? I'm kind of new to C++, I understand it well but I don't know all the functions yet (switched from VB and C#)

EDIT: Nevermind, I googled it and found out it should be NULL. Thanks for telling me about the function [smile]

EDIT AGAIN:
I've implemented it successfully, thanks for the help. It took me a while to figure out the LPCH buffer and Size but that's what MSDN is for :D

##### Share on other sites
Quote:
 Original post by Super Llamaalso what is argv[0]?
In a standard C++ application, your main function should look something like this:
#include <iostream>int main(int argc, char **argv) {	std::cout << argv[0] << '\n'; // This will print the application's launch path}

The first item in the argv array (i.e. argv[0]) is your program's launch path. However, if you are using WinMain instead of main, things will be a little different.

##### Share on other sites
Quote:
 Original post by swiftcoderThe first item in the argv array (i.e. argv[0]) is your program's launch path.

That's not guaranteed. According to the C++ standard (section 3.6.1 paragraph 2), argv[0] is "the name used to invoke the program". This may or may not include the path to the application, and in fact is allowed to be an empty string.

##### Share on other sites
Quote:
Original post by SiCrane
Quote:
 Original post by swiftcoderThe first item in the argv array (i.e. argv[0]) is your program's launch path.

That's not guaranteed. According to the C++ standard (section 3.6.1 paragraph 2), argv[0] is "the name used to invoke the program". This may or may not include the path to the application, and in fact is allowed to be an empty string.
Fair enough, but in all the command line environments I have encountered, it has given exactly the (expanded) string I typed to launch the application, which is the relative path from the working directory.

It doesn't work so often in GUI environments - in particular, it is always the root directory for a Mac GUI application. However, a GUI application probably shouldn't be messing around in the application's install location - there are well defined directories on each platform reserved for program data ('~/Library/Application Support' on Mac, 'Documents & Settings' on Windows...).

##### Share on other sites
What argv[0] contains is not just dependent on command line environment, but also dependent on compiler and standard library implementation. For example compiling this program
#include <iostream>int main (int, char ** argv) {  std::cout << argv[0] << std::endl;}

with gcc 3.4.4 in cygwin to the executable a.exe and running it from Vista's command prompt produces the output "a". No path included or even any path delimiters. It's not even the full name of the executable.

##### Share on other sites
I'm trying to load bitmaps from a folder in the application's install folder, so SiCrane's method worked perfectly for me. I'm using WinMain instead of just Main, so there is no argv parameter.

This is my first GUI application in Native C++, EasilyConfused taught me how to use GDI+ in a different help topic yesterday, now I'm trying to load a bitmap from the same folder.

GetModuleFileName() works nicely. Now I just need to figure out how to chop off the end of it... I'm addicted to the VBA String class XD

##### Share on other sites
You can use std::string (or std::wstring if compiling for Unicode) for this like so:
  char module_name[MAX_PATH];  GetModuleFileName(0, module_name, MAX_PATH);  std::string path(module_name);  path.erase(path.find_last_of('\\'), std::string::npos);

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
13
5. 5

• 13
• 10
• 9
• 17
• 21
• ### Forum Statistics

• Total Topics
632601
• Total Posts
3007348

×