Dumb question: where's my GetEnvironmentVariable?

Hello, Raw Material People.

This is only peripherally a Juce question (except why doesn’t Juce have a correct crossplatform “get environment variables” function? If this one worked, it would be useful in Juce, hint hint…)

I’ve been using getenv up until now, which usually works everywhere, but on Windows, this unfortunately does not read environment variables that are set after the program has started, and this is the only way that our copy protection program communicates with our main executable…

In the code below, GetEnvironmentVariable should be defined by windows.h (which, indeed, I shouldn’t even need to include because Juce always includes it…)

But when I actually try to compile it, I get an error: GetEnvironmentVariable: identifier not found.

(And I get a similar error if I try to call ::GetEnvironmentVariable.)

My Windows skills are still only basic, and this seems so obvious I’m not sure how to debug it…

Thanks in advance!

[code]#if JUCE_WINDOWS
#include <windows.h>

#include “rec/util/GetEnv.h”

namespace rec {
namespace util {

String getEnv(const char* name, const String& dflt) {
static const int MAX_ENV = 1024;
char buffer[MAX_ENV];

if (int len = GetEnvironmentVariable(name, buffer, MAX_ENV))
return str(string(buffer, buffer + len));

const char* res = getenv(name);
if (res && *res)
return String(res);

return dflt;

} // namespace util
} // namespace rec

I’ve been meaning to write one of those for years… So thanks for the nudge, I’ve done one now. Let me know if it works!

Oh, fast work, but that doesn’t quite do it.

You should really be using GetEnvironmentVariable to get the environment variables on Windows - mainly because getenv doesn’t reflect any changes in the environment variables after your application has started - you can see more information here: http://stackoverflow.com/a/631717

In particular, the copy protection wrapper gives us data through variables it sets, and I have no other way to get 'em. So getenv won’t work…

Yes, that’s exactly what I’ve done…



Quite so. hehehe. Well, VERY fast work then. Stupid me!

I’ll try it out and see if it does the trick. I should never have doubted you…