C++ equivalent to Application.StartupPath?

Started by
22 comments, last by Super Llama 15 years, 6 months ago
Just a quick question: What is the Native C++ (non .NET) equivalent to Application.StartupPath? I've googled it with no luck...
It's a sofa! It's a camel! No! It's Super Llama!
Advertisement
There is no standard equivalent. argv[0] is all you have.
In Windows, you can use GetModuleFileName() and lop off the file name to get just the path.
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
It's a sofa! It's a camel! No! It's Super Llama!
Quote:Original post by Super Llama
also 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.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Quote:Original post by swiftcoder
The 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.
Quote:Original post by SiCrane
Quote:Original post by swiftcoder
The 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...).

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

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.
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
It's a sofa! It's a camel! No! It's Super Llama!
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);

This topic is closed to new replies.

Advertisement