Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
4.50/5 (3 votes)
See more:
Hello all,

I'm porting an application from "Visual C++ 6" to "Visual C++ 2010".

In that application I'm using:

char    szAppPath[MAX_PATH] = "";
CString strAppDirectory;

::GetModuleFileName(0, szAppPath, sizeof(szAppPath) - 1);
strAppDirectory = szAppPath;
m_csPathApp = strAppDirectory.Left(strAppDirectory.ReverseFind('\\'));


To get the applciation path, but I'm getting something like: "c:\projects\my_app\.\Debug" and of course nothing is working from that point. (The part in bold and big fonts should be replaced by \).

It was working before (VC++6), and, of course I can search for "\.\" and replace it for "\" but I would like to avoid that as I'm guessing I'm doing something wrong here...

Searched google using "visual C++ 2010 get application path" Without luck, I've even seen FAQs and tutorials in several pages (Codeguru, stack overflow,...) in which they were suggesting my approach...

Any idea or advice?

Thank you in advance,
Posted
Updated 11-Apr-12 4:32am
v2
Comments
Jochen Arndt 11-Apr-12 10:40am    
What do you mean by 'nothing working'? You excuted your app from within VS and VS build the path according to the project settings which includes '\.' somewhere. However, the path is valid ('\.' just refers to the directory specified on the left).
Joan M 11-Apr-12 10:50am    
Yes, you are right, it is in the debug and release settings: .\Debug and .\Release.
In the other hand, I know that the . usually refers to the same directory one's in, but if you try to open one file using something like c:\folder\.\folder2\file.txt it is not working... :(

I'll modify that setting in the project settings and let's see.

regarding your question, I'm loading files that are in the same folder than the application and without the right path I can't load them so the application is not working.

Anyway... after replacing the .\Debug\ for \Debug or \Debug\ it is not capable to build the solution.
Jochen Arndt 11-Apr-12 11:00am    
That does not care when loading files. The path is resolved by Windows when passing it to API functions. Open a command shell and execute 'type c:\folder\.\folder2\file.txt'. If your app can't open such files, there must be something strange with your code.
Joan M 11-Apr-12 11:03am    
You are right... I've been making the test on the address bar in any explorer window... probably the error has nothing to do with the path but it has something to do with the loading of the XML file I'm handling... I'll continue searching... thank you. :thumbsup:
Joan M 11-Apr-12 11:04am    
If you would post your comments as a solution I would mark it as solved.

The path "c:\projects\my_app\.\Debug" is perfectly legal and is equivalent to "c:\projects\my_app\Debug".

Try the following commands:
cd c:\projects\my_app\.\Debug
and
cd c:\projects\my_app\Debug

You will see that they change working directory to the same place, if the path exists.

If you feel that "nothing is working", the reason is different.

—SA
 
Share this answer
 
Comments
Joan M 11-Apr-12 11:06am    
Thank you SAKryukov, probably I'm being too paranoic, but this app is killing my nerves... it was a VC++6 application that worked towards an access database under windows 98 using DAO and using XML files to get some configuration parameters... so imagine...

PS: accepted as solution and 5ed.
Sergey Alexandrovich Kryukov 11-Apr-12 11:10am    
You are welcome. Please follow my advice and run things under the debugger before asking questions like that and, actually, in case if slightest concern about your run time behavior; that should greatly stabilize the development and prevent accumulation of problems...

Good luck, call again.
--SA
Joan M 11-Apr-12 11:12am    
You are sooo right, I've been too sure that it would work than I've not handled things properly here... :(
Espen Harlinn 11-Apr-12 18:27pm    
a 5 as usual :-D
Sergey Alexandrovich Kryukov 11-Apr-12 18:35pm    
Thank you as always :-)
--SA
I have gone through the above discussion and I suggest you to use the macros like $(OutDir), $(IntDir) - which are predefined in Visual studio to set the path of output and other intermediate files, instead of manually renaming to some /.Debug
By this way you can create legal folder paths.

Also to load a file which is at the same location as exe/dll, i think giving the file name itself will do (rather than the full path) as it will search initially in that exe/dll path itself
 
Share this answer
 
Comments
Joan M 11-Apr-12 11:10am    
5ed, thank you...
Lakamraju Raghuram 11-Apr-12 11:12am    
Good luck
Sergey Alexandrovich Kryukov 11-Apr-12 12:08pm    
Those are not "macros" and formally not related to Visual Studio, they are standard MSBuild attributes. Their default values are defined the way the build "just works" from the very beginning, but they are by far not most optimal in typical cases, in particular, if there are more then one project in the solution. So, right approach is modification the path in $(OutDir) (one simple approach is to through all output from different projects in one directory on some outer directory level, and the path should be relative to the project file location; otherwise the solution would be rendered non-movable). It's important not to make other paths "manually"; as they should be based on the value of $(OutDir). So, I feel your advise goes in right direction, but I don't think it is clear enough...
--SA
Did you find a solution to this issue? I just ran into it and I am not able to figure out how to get the absolute path from
char szModuleName[1024];
::GetModuleFileName(NULL, szModuleName, 1023);
char szLongPathName[1024];
CString sModuleName(szLongPathName);
m_sAppPath = sModuleName.Left(sModuleName.ReverseFind('\\'));

It is returning the relative path of the exe file something like C:\Project\App\..\Bin and I am looking for C:\Project\Bin

Any ideas?

Thanks,
 
Share this answer
 
Comments
Joan M 8-Apr-14 1:23am    
Not at the office now, I can't give you an answer as I can't check anything. (This is being posted from a blackberry). And I won't be able to check anything until some hours so your best bet is to put a new question in the q&a section to receive help from anyone.

Good luck!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900