[VS] How to get environment variable (=macro)

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

Recommended Posts

Hello,

I know that with getenv, I can query the value of a regular environment variable:

auto pEnv = getenv("DXSDK_DIR");


However, I'm interested in querying visual studio-specific "macros" (thats how they put it) for my build-process, like this:

auto pEnv = getenv("MsBuildBinPath"); // always returns nullptr

If you're unsure what I mean with those macros, goto project properties >> C/C++ >> General >> Additional Include directories >> "Macros >>"-button in the bottom right (which includes all environment variables, but also said VS-specific tings which are not in the general environment variables, and appearently cannot be read with getenv. My google-search didn't yield me any results, so anybody got an idea how to read those specific "macros" in code?

And if thats not directly possible, how do I access the binary directory for the actual version of MSBuild, without manually defining an environment variable?

Edited by Juliean

Share on other sites
If your code is part of the actual build process, you can easily read these macros using the MSBuild infrastructure. It's large and complex and heavy, so be sure you know what you're asking for before diving in.

If your code is not intended to be part of the build process, Kylotan's advice is on point.

Share on other sites

And if thats not directly possible, how do I access the binary directory for the actual version of MSBuild, without manually defining an environment variable?

I think the registry contains entries that you can scan for. Last time I did this was > 8 months ago, so my memory has atrophied.

If you have a consistent deployment of MSBuild versions you can just put the path in a config file that your build pipeline code uses. This is what I do for most things since I can keep everything in one spot, regardless of what I'm actually using.

Share on other sites

Why do you want to do this? I expect there is a better way.

Well my main objective is to get the path of msbuild.exe on the current machine, without requiring any setup (except of course installing VS and MSBuild). I only just found that visual studio had those paths set, so I wanted to use them.

What I'm doing (in response to everyone) is, in my editor I have a "build" option, that generates a packed standalone of a game built with my engine. For the first step I actually have to invoke MSBuild.exe manually, since I can ie. create a shipping-build out of an editor that is started with a debug configuration.

I think the registry contains entries that you can scan for. Last time I did this was > 8 months ago, so my memory has atrophied.

I found some registry based suggestions later on, I'll have to keep looking though since none of them seems to be pointing to the correct version of MSBuild as far as I can tell.

If you have a consistent deployment of MSBuild versions you can just put the path in a config file that your build pipeline code uses. This is what I do for most things since I can keep everything in one spot, regardless of what I'm actually using.

Well the reason I was looking for the use of a (non-personally defined) environment variable was because the path changes, ie. I alone have three PCs that I use regularily, which all have different default install dirs (due to harddrive-setup), plus when I upgrade the VS studio version, I also have to change the path... so I know that I can make it work somehow, but I was looking for a better solution, and the MSBuildBinPath-macro seemed to be just what I was looking for... so I'll probably check with registry again, if there's any other suggestions, let me know.

Share on other sites
Part of the answer lurks in your own observations - MSBuild does not have to have a single canonical path on any given machine.

There can be (and often are!) multiple instances of MSBuild installed on a single box. This is a situation you have to handle.

Share on other sites

What I'm doing (in response to everyone) is, in my editor I have a "build" option, that generates a packed standalone of a game built with my engine. For the first step I actually have to invoke MSBuild.exe manually, since I can ie. create a shipping-build out of an editor that is started with a debug configuration.

But there is obviously no guarantee that the location of the MS build tools - or any other value that was set in Visual Studio when you compiled and built your engine - is still valid at some future time when you click the 'build' option. It would certainly never work if you ever distributed that editor. That's why these values are not designed to be read by code; they're specific to the state at the time of generation of your current program, nothing more.

For the current state of MSBuild, at any given time in the future, you will need to check the file system and/or the registry keys.

Share on other sites

But there is obviously no guarantee that the location of the MS build tools - or any other value that was set in Visual Studio when you compiled and built your engine - is still valid at some future time when you click the 'build' option.

Mhm, I understand that now. I originally thought of the VS-macros as some sort of hidden env-variable that could be read on runtime, but I see how thats not the case.

For the current state of MSBuild, at any given time in the future, you will need to check the file system and/or the registry keys.

This is a situation you have to handle.

I see, I will do that. Out of curiosty I checked how Unreal handles this, they have a batch-file for locating the msbuild-version, I should be able to use this as a help to figure out how to query the correct location of MSBuild for myself.

Share on other sites

You may also want to check out the CMake source code. It too has to locate various versions and installations of MSBuild (typically by checking well-known registry keys).

1. 1
2. 2
3. 3
frob
20
4. 4
Rutin
19
5. 5

• 32
• 13
• 10
• 9
• 18
• Forum Statistics

• Total Topics
632555
• Total Posts
3007065

×