What's my path?

Started by
5 comments, last by ToxicSoul 21 years, 2 months ago
Is there a function in c++ for retriving the applications path?
Advertisement
argv[0] and GetModuleFileName(0).
or
char *_getcwd( char *buffer, int maxlen );
_getcwd (get current working directory) will NOT always be the path where your app resides in.

Imagine starting an exe from dos-box like this:

c:\program files\>d:\yourapp.exe

argv[0] or GetModuleFileName(0) is your way to go, but you have to strip the filename of your app from the string.

Regards,
Endurion

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Ah, but it''s possible that argv[0] is empty (I had this last week when trying to insert a custom executable into a make process.) I''m not certain of exactly when it''s empty (I couldn''t really be bothered checking) but I think calling an exe through a batch process might result in an empty argv[0].

Also, GetModuleFileName(0) is a Windows call. The original poster didn''t specify an operating system.
IIRC, argv[0] isn''t required to include the full path to the executable.


"I know very well who Satan is: He is freedom. He is the uncontrolled, the incalculable, the antithesis of order and discipline, the antithesis of the legalism of outer space.... We know where a planet will be in twelve years, four months and nine days. But we don''t know where a butterfly will have flown one minute hence. Therefore the butterfly is of Satan."
-- Jens Bjørneboe
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Thanks to all of you, I think I''ll use GetModuleName, I''m working with dx so I can''t port to any other system anyway...

This topic is closed to new replies.

Advertisement