Skip to content

Instantly share code, notes, and snippets.

@Porges
Created April 9, 2017 12:28
Show Gist options
  • Save Porges/4f110d2b3b3d3b28987f49d60b145859 to your computer and use it in GitHub Desktop.
Save Porges/4f110d2b3b3d3b28987f49d60b145859 to your computer and use it in GitHub Desktop.
#include <Windows.h>
#include <array>
#include <iostream>
#include <exception>
[[noreturn]] void throw_last_error() {
throw std::system_error{static_cast<int>(GetLastError()), std::system_category()};
}
#define die (throw_last_error(), false)
int main()
{
constexpr auto varName = L"USERNAME";
constexpr auto altName = L"uSeRNaME"; // sO RaNDoM?~
std::array<wchar_t, 1024> str;
try
{
GetEnvironmentVariable(varName, str.data(), str.size()) || die;
std::wcout << varName << ": " << str.data() << '\n';
GetEnvironmentVariable(altName, str.data(), str.size()) || die;
std::wcout << altName << ": " << str.data() << '\n';
return 0;
}
catch (const std::exception& err)
{
std::cerr << err.what() << '\n';
return -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment