Here is my code

#include<iostream>
#include<Windows.h>
using namespace std;
#define BUFSIZE 4602
#define VARNAME TEXT("zzz")
int main()
{

    TCHAR chNewEnv[BUFSIZE];
    GetEnvironmentVariable(VARNAME, chNewEnv, BUFSIZE);
    if(GetLastError()==ERROR_ENVVAR_NOT_FOUND)
    {
        cout<<"Hello";
    }
    else
    {
        cout<<"Bye";
    }
return 0;
}

I am checking whether zzz environment variable is available or not. I need to do the same without using if(GetLastError()==ERROR_ENVVAR_NOT_FOUND). Is there any way?

I am doing the same program using C# in which I don't use GetLastError(). I need to make these two programs similar that is why I am asking without the use of GetLastError(). Here is my C# snippet

string abc =Environment.GetEnvironmentVariable("zzz"); 
if (abc == null || abc.ToUpper() == "NULL" || abc.ToUpper() == "NUL")
有帮助吗?

解决方案

You have to use GetLastError, as you may encounter cases where a variable is defined with no value.

Such cases can be emulated with:

SetEnvironmentVariable( "foo", "" );

When GetEnvironmentVariable return 0, it may be that the variable doesn't exist, or that is exists with an empty content. In the later case, GetLastError returns 0.

If you don't care (that is: a nonexistent variable or an empty one is the same for you), then don't bother with GetLastError.


Side note. You should always make a first call with a NULL nSize argument, as the documentation states:

If lpBuffer is not large enough to hold the data, the return value is the buffer size, in characters, required to hold the string and its terminating null character and the contents of lpBuffer are undefined.

And, I just tested (Windows 7), in such case GetLastError returns 0 (awkward, if you ask me)


setting a variable in 7

Edit: On Windows 7, when I set an user variable to "null", the value retrieved by GetEnvironmentVariable (MBCS) is a 5 bytes string 'n', 'u', 'l', 'l', '\0'


Code (UNICODE build):

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <Windows.h>


#define BUFSIZE 4602
#define VARNAME L"zzz"

const wchar_t * MyGetEnv( const wchar_t * pszVarName, wchar_t * pszVarValue,
                          size_t cbValue ) {

    DWORD dwCopied = GetEnvironmentVariable( pszVarName, pszVarValue, cbValue );
    // The line bellow MAY BE COMMENTED OUT IF YOU REALLY
    // DON'T LIKE GETLASTERROR, AS cbCopied WILL BE ZERO FOR
    // NON-EXISTENT VARIABLE
    if ( GetLastError() != NO_ERROR ) return NULL; // doesn't exist, or error
    if ( dwCopied == 0 ) return NULL; // var is empty
    return pszVarValue;

}

int main() {

    wchar_t szVarValue[ BUFSIZE ];
    const wchar_t * pszVarValue = MyGetEnv( VARNAME, szVarValue,
                                            _countof( szVarValue ) );
    if ( pszVarValue == NULL ) {
        printf( "No variable or empty value\n" );
    } else if ( ( _wcsicmp( pszVarValue, L"null" ) == 0 ) ||
                ( _wcsicmp( pszVarValue, L"nul" )  == 0 )    ) {
        printf( "Special 'null' or 'nul' value\n" );
    } else {
        wprintf( L"Value is %s\n", szVarValue );
    }
    return 0;

}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top