How to remove environment variable programmatically in windows

This simple tutorial is for MS Visual C++. You remove an environment variable in C++ almost the same way you set it.

How to set environment variable?

char envName[256] = "Test";
SetEnvironmentVariable("VAR_NAME", envName);


How to remove environment variable? Setting the value to NULL literal deletes environment variable.
1.
2.

SetEnvironmentVariable("VAR_NAME", NULL);



How to get environment variable?

LPTSTR buffer1 = new TCHAR[256];
DWORD dwRet = GetEnvironmentVariable("VAR_NAME", buffer1, 256);

if (dwRet == 0)
{
// Environment variable does not exist
}



Post a comment

Registered users do not have to enter captcha. A line in the code tag is currently limited to maxium of 160 characters.
Posting guidelines: You may not post any personal information. When you report an issue: Always mention which version and operating system and briefly describe the issue. Any support request post that does not include this information will be removed as spam without a reply.
Title
Tags You may use the following tags: [QUOTE] [/QUOTE] [B] [/B] [URL] [/URL] [CODE] [/CODE]
Captcha Please enter the text you see (case insensitive). The listed characters must be entered clockwise starting from twelve o'clock.
Comments are moderated N