2.1.25.14 GetEnvironmentVariable


Description

This function retrieves the value of the specified variable from the environment block of the calling process. The value is in the form of a null-terminated string of characters.

Syntax

DWORD GetEnvironmentVariable( LPCSTR lpcszName, LPSTR lpBuffer, DWORD nSize )

Parameters

lpcszName
[input] environment variable name
lpBuffer
[input] buffer for variable value
nSize
[input] size of buffer

Return

If the function succeeds, the return value is the number of characters stored into the buffer pointed to by lpBuffer, not including the terminating null character.

If the specified environment variable name was not found in the environment block for the current process, the return value is zero.

If the buffer pointed to by lpBuffer is not large enough, the return value is the buffer size, in characters, required to hold the value string and its terminating null character.

Examples

EX1

// usage:
// show_environment_str path;
// show_environment_str temp;
int GetEnvironmentVariable_ex1(string strName)
{
    char szTemp[MAXFULLPATH];
    
    int    nResult = GetEnvironmentVariable(strName, szTemp, MAXFULLPATH);
    
    if(nResult > 0)
    {
        if(nResult == lstrlen(szTemp))
            out_str(szTemp);
        else
            printf("The %s environment variable is too big (%d bytes)\n",strName,nResult);
    }
    else
        out_str("Error: Environment variable not found.");
    return 1;
}

Remark

See Also

Header to Include

origin.h

Reference