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

Started by
7 comments, last by Zipster 7 years, 2 months ago

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?

Advertisement

Visual C++ macros are only exposed to the build process, not to the environment or the end program that you create. Generally speaking you shouldn't want to access these variables in code. A program isn't meant to know or care about how it gets built.

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

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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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.

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.

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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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.

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.

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).

This topic is closed to new replies.

Advertisement