If I wanted to get the full path of my running program is there a C++ equivalent to App.Path or does this have to be done in a long and tedious process?
If I wanted to get the full path of my running program is there a C++ equivalent to App.Path or does this have to be done in a long and tedious process?
Luckily, the answer to this is quite, too easy! It should work fine with what you are looking for, it's a special "#define" that will tell you the whole path to your application... Then you just have to place that piece of code where you need it:
There are probably plenty of those special #defines, though the other one I know are these, and what they do is enough relevent:Code:__FILE__
Code:__LINE__ __DATE__ __TIME__
That would be the source file, not the name of the executable.Originally Posted by mikahell
RedZone: What OS?
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
You are definitely wrong. The __FILE__ macro returns the path of the current file (.h, .hpp, .c, .cpp, .cxx ....) and not the path of the current process:Code:HANDLE ThisProcess = GetCurrentProcess(); TCHAR FilePath[MAX_PATH + 1] = {0}; GetFinalPathNameByHandle(ThisProcess, FilePath, MAX_PATH, 0);
Windows XPOriginally Posted by Dave_Sinkula
This is helpful but MSDN tells me that I need Vista.Originally Posted by Desolation
You have to search the MSDN a bit, I have found some of their stuff is for Win CE, then when i search and click again on a function, I get Win XP
Really, It's not that hard....It comes all in one function:
In code like this:Code:DWORD GetModuleFileName( HMODULE hModule, LPTSTR lpFilename, DWORD nSize );
Code:char myFilename[MAX_PATH]; GetModuleFileName(NULL,myFilename,MAX_PATH); //myFilename now has the directory
Be easy on me...only 14
im not sure but wont
work?Code:#include<iostream> int main(int argc,char *argv[]){ if(argc !=2){ cout<<argv[0]<<endl; } }
>im not sure but wont <snip code> work?
No, argv[0] isn't required to be meaningful at all (ie. it can be an empty string), and even if it is meaningful, it isn't required to contain the full path.
My best code is written with the delete key.
It is a Win32 thread I think.
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
On Windows it always has the full path as far as I can tell. But not with other OSes.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Only because explorer and visual studio put in the full path. Try calling the program from the console and you'll see that you're wrong.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Or if you call it using an exec() function, you can completely lie about it.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
On Windows 98, the full path is put in no matter how I execute the program; console or explorer or otherwise. (Except for exec(). ) I guess it isn't the same for other versions of windows?Only because explorer and visual studio put in the full path. Try calling the program from the console and you'll see that you're wrong.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.