Game Development Community

GetEnvironmentVariable() help

by Dan Phillips · in Torque Game Builder · 08/06/2007 (9:22 am) · 4 replies

I'm trying to write a function that returns the result of getEnvironmentVariable. Unfortunately I don't know c++, could someone help me out? Here's what I have so far.

ConsoleFunction(getEnvVar, S32, 2, 2, "getEnvVar(string)")
{
	S32 envReturn;
	GetEnvironmentVariable(argv[1], envReturn, 255);
	if (envReturn!=NULL)
		return(S32(envReturn));
	return 0;
}

But I get the error 1>..\..\source\console\consoleFunctions.cc(1260) : error C2664: 'GetEnvironmentVariableW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


I'm guessing I need to somehow cast argv[1] to LPCWSTR but I can't figure out how.

thanks

#1
08/06/2007 (10:08 am)
Try this, works for me:

ConsoleFunction(getEnvVariable, const char *, 2, 2, "getEnvVariable(varname)")
{
   argc;
   const char *varname = argv[1];
   if(!varname)
      return "";

    char value[256] = "";

    if (!GetEnvironmentVariableA(varname, value, 255)) {  
        return "";  
    }
	
	char *ret = Con::getReturnBuffer(dStrlen(value));
    dStrcpy( ret, value );

    return ret;
}
#2
08/06/2007 (10:47 am)
Be aware, I believe GetEnvironmentVariableA is a windows specific call and won't work on OS X or Linux.
#3
08/06/2007 (10:56 am)
Good catch Guy, thanks.

My implementation is used exclusively for our Windows builds for the Armadillo trial-time wrapper. Substitute the appropriate getenv call as necessary.
#4
08/06/2007 (7:44 pm)
Thanks! That's exactly what I'm using it for, Armadillo on windows and it works fine.