Solved

How to convert $(SolutionDir) into a C++ string?

Posted on 2009-07-10
6
1,810 Views
Last Modified: 2013-12-14
I need to access the compile-time path of the solution/project in C++ code. This seems to be easy to solve by way of compiler option /D ("preprocessor definitions"), which allows to add C++ macros that are treated the same way as regular "#define" statements. Using the /D compiler switch, I can use environment variables such as $(SolutionDir) or $(ProjectName), which are resolved to their actual values before being passed to the preprocessor.

Example: /D _SOLUTIONPATH_="$(SolutionDir)"
Resolves to: #define _SOLUTIONPATH_ "C:\svn\solutions\mysolution\"

Unfortunately, the solution path contains backslashes which create unintended escape sequences in the string that is resolved from the environment variable. I did not find a way to create a properly escaped string. The problem has been discussed elsewhere without any solution:
http://bytes.com/sitemap/t-788402.html

Any of the following would qualify as a solution to my question:
- A way to create a properly escaped C++ string from the $(SolutionDir) environment variable.
- A way to make $(SolutionDir) a path with forward slashes instead of backslashes, which would eliminate the necessity to escape anything.
- A way to define a C++ macro that contains the length of the $(SolutionDir) string, rather than the string itself.
- Any other way to make the compile-time solution path available in the C++ code without going through compiler option /D.
0
Comment
Question by:think-cell
  • 3
  • 2
6 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 24824886
Hi think-cell,

I guess I found a quite tricky way, take a look at it if you like it - it works by generating a include file ...

First set a PreBuild-event like this:

> set __TEMP__=$(OutDir)
> set __TEMP__=%__TEMP__:\=\\%
> echo #define _SOLUTION_PATH_ "%__TEMP__%"  > tmp.h

Then in the 'C/C++' settings 'Command Line' add this:

> /FI tmp.h

If you want you can delete the temporary include file in a post-build step.


Hope that helps,

ZOPPO
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 24835223
Really cool! :) Just nice. It is a solution.
Maybe the question just looks so strange and the problem is somewhere in the application design?
Maybe an API like GetModuleFileName / GetCurrentDirectory could help?
 
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 24835448
BTW, set the working folder in the project settings as the $(SolutionDir) and then from the code call GetCurrentDirectory. Or $(SolutionDir)Bin.
But, I'd say, this folder name is always known - predefined/calculated somehow.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 31

Expert Comment

by:Zoppo
ID: 24837575
@pgnatyuk: Please re-read the question - think-cell wants to have the value of the macro $(OutDir) as a string at compile-time, not at run-time ...
0
 

Author Closing Comment

by:think-cell
ID: 31602006
Crazy, and works! Thanks.

Why would you use the compiler switch to include the file? I use a regular include statement next to the only place where I refer to the _SOLUTION_PATH_.
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 24838949
You're welcome, I'm glad I could help ...

And, I used this command line option for include so it 'feels' as if the macro was declared through preprocessor-settings - of course you can even include the created file instead using #include's where you need it.

Have a nice day,

best regards,

ZOPPO
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual Studio 2013  Distorts Chrome Browser 2 162
error when i compile 5 569
eclipse ide not starting 16 79
Angular JS Route 3 47
In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now