_tgetenv_s example
It is used to get a value of a particular environmental variable.
Syntax:
Parameters:
pReturnValue: The buffer size that is required to store the variable
buffer: Buffer in which the value of the environmental variable will be stored
numberOfElements: Size of Buffer
varname: Name of the Environmental Variable.
Example:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
int _tmain(int argc, _TCHAR* argv[])
{
size_t requiredSize;
TCHAR *value;
_tgetenv_s(&requiredSize, NULL, 0, _T("SystemRoot"));
printf("Size of the buffer required is %d\n", requiredSize);
value = (TCHAR *)malloc(requiredSize * sizeof(TCHAR));
_tgetenv_s(&requiredSize, value, requiredSize, _T("SystemRoot"));
printf("Value of SystemRoot Environmental Variable:%ls\n", value);
return 0;
}
First we will pass NULL in the buffer and get the length of the buffer required for the value of the environmental variable and then we will allocate memory and pass the buffer.
Syntax:
errno_t _tgetenv_s( size_t *pReturnValue, char* buffer, size_t numberOfElements, const char *varname );
Parameters:
pReturnValue: The buffer size that is required to store the variable
buffer: Buffer in which the value of the environmental variable will be stored
numberOfElements: Size of Buffer
varname: Name of the Environmental Variable.
Example:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
int _tmain(int argc, _TCHAR* argv[])
{
size_t requiredSize;
TCHAR *value;
_tgetenv_s(&requiredSize, NULL, 0, _T("SystemRoot"));
printf("Size of the buffer required is %d\n", requiredSize);
value = (TCHAR *)malloc(requiredSize * sizeof(TCHAR));
_tgetenv_s(&requiredSize, value, requiredSize, _T("SystemRoot"));
printf("Value of SystemRoot Environmental Variable:%ls\n", value);
return 0;
}
First we will pass NULL in the buffer and get the length of the buffer required for the value of the environmental variable and then we will allocate memory and pass the buffer.
Comments
Post a Comment