Created
April 9, 2017 12:28
-
-
Save Porges/4f110d2b3b3d3b28987f49d60b145859 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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