开发者

How to find out if enviroment variable is not set?

开发者 https://www.devze.com 2023-03-17 06:43 出处:网络
So I try cha开发者_JS百科r buffer[1000]; GetEnvironmentVariable(\"PATH\",(char*)&buffer,sizeof(buffer));

So I try

cha开发者_JS百科r buffer[1000];
GetEnvironmentVariable("PATH",(char*)&buffer,sizeof(buffer));
std::cout << buffer << std::endl;

to check if it exists but I do not see how to see if its empty? And It couts some really bad output=(


MSDN has this to say about GetEnvironmentVariable:

If the function fails, the return value is zero. If the specified environment variable was not found in the environment block, GetLastError returns ERROR_ENVVAR_NOT_FOUND.

(Source: http://msdn.microsoft.com/en-us/library/ms683188.aspx)

So I guess you should test the function's return value:

char buffer[1000];
if(GetEnvironmentVariable("PATH", buffer, sizeof(buffer)))
{
    // non-zero, go ahead!
    std::cout << buffer << std::endl;
}
else
{
    // zero, something went wrong
}


You can always use getenv which returns null if the variable doesn't exist and a blank string if it's blank.


When the environment variable is not set GetEnvironmentVariable returns 0 and GetLastError() returns ERROR_ENVVAR_NOT_FOUND (203).

Have a look at the documentation: http://msdn.microsoft.com/en-us/library/ms683188%28v=vs.85%29.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消