Jump to content
  • Advertisement
Sign in to follow this  
Super Llama

C++ equivalent to Application.StartupPath?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...).

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!