2022-05-21 19:58:09 +02:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "Environment.h"
|
|
|
|
#include "Path.h"
|
|
|
|
#include <shellapi.h>
|
|
|
|
|
|
|
|
namespace System
|
|
|
|
{
|
2022-05-21 21:51:35 +02:00
|
|
|
const String Environment::NewLine = "\r\n";
|
2022-05-21 19:58:09 +02:00
|
|
|
|
|
|
|
void Environment::Exit(int32_t ExitCode)
|
|
|
|
{
|
|
|
|
std::exit(ExitCode);
|
|
|
|
}
|
|
|
|
|
2022-05-21 21:51:35 +02:00
|
|
|
String Environment::GetFolderPath(SpecialFolder Folder)
|
2022-05-21 19:58:09 +02:00
|
|
|
{
|
|
|
|
char Buffer[MAX_PATH + 1]{};
|
|
|
|
auto Result = SHGetFolderPathA(NULL, (int)Folder, NULL, SHGFP_TYPE_CURRENT, Buffer);
|
|
|
|
|
|
|
|
if (Result < 0)
|
|
|
|
return "";
|
|
|
|
|
2022-05-21 21:51:35 +02:00
|
|
|
return String(Buffer);
|
2022-05-21 19:58:09 +02:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:51:35 +02:00
|
|
|
String Environment::GetApplicationPath()
|
2022-05-21 19:58:09 +02:00
|
|
|
{
|
|
|
|
char Buffer[MAX_PATH + 1]{};
|
|
|
|
auto mResult = GetModuleFileNameA(NULL, Buffer, MAX_PATH);
|
|
|
|
|
|
|
|
if (mResult != 0)
|
|
|
|
return IO::Path::GetDirectoryName(Buffer);
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-05-21 21:51:35 +02:00
|
|
|
String Environment::GetApplication()
|
2022-05-21 19:58:09 +02:00
|
|
|
{
|
|
|
|
char Buffer[MAX_PATH + 1]{};
|
|
|
|
auto mResult = GetModuleFileNameA(NULL, Buffer, MAX_PATH);
|
|
|
|
|
|
|
|
if (mResult != 0)
|
|
|
|
return Buffer;
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-05-21 21:51:35 +02:00
|
|
|
String Environment::GetCommandLine()
|
2022-05-21 19:58:09 +02:00
|
|
|
{
|
2022-05-21 21:51:35 +02:00
|
|
|
return String(GetCommandLineA());
|
2022-05-21 19:58:09 +02:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:51:35 +02:00
|
|
|
List<String> Environment::GetCommandLineArgs()
|
2022-05-21 19:58:09 +02:00
|
|
|
{
|
2022-05-21 21:51:35 +02:00
|
|
|
auto Result = List<String>();
|
2022-05-21 19:58:09 +02:00
|
|
|
|
|
|
|
int nArgs = 0;
|
|
|
|
auto szArgList = CommandLineToArgvW(GetCommandLineW(), &nArgs);
|
|
|
|
if (szArgList == nullptr)
|
|
|
|
return Result;
|
|
|
|
|
|
|
|
for (int i = 0; i < nArgs; i++)
|
2022-05-21 21:51:35 +02:00
|
|
|
Result.EmplaceBack(std::move(WString(szArgList[i]).ToString()));
|
2022-05-21 19:58:09 +02:00
|
|
|
|
|
|
|
LocalFree(szArgList);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2022-05-21 21:51:35 +02:00
|
|
|
String Environment::GetUserName()
|
2022-05-21 19:58:09 +02:00
|
|
|
{
|
|
|
|
char Buffer[1024]{};
|
|
|
|
DWORD BufferSize = 1024;
|
|
|
|
GetUserNameA(Buffer, &BufferSize);
|
|
|
|
|
2022-05-21 21:51:35 +02:00
|
|
|
return String(Buffer);
|
2022-05-21 19:58:09 +02:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:51:35 +02:00
|
|
|
String Environment::GetComputerName()
|
2022-05-21 19:58:09 +02:00
|
|
|
{
|
|
|
|
char Buffer[MAX_COMPUTERNAME_LENGTH + 1]{};
|
|
|
|
DWORD BufferSize = MAX_COMPUTERNAME_LENGTH;
|
|
|
|
GetComputerNameA(Buffer, &BufferSize);
|
|
|
|
|
2022-05-21 21:51:35 +02:00
|
|
|
return String(Buffer);
|
2022-05-21 19:58:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t Environment::GetTickCount()
|
|
|
|
{
|
|
|
|
return GetTickCount64();
|
|
|
|
}
|
|
|
|
|
2022-05-21 21:51:35 +02:00
|
|
|
void Environment::SetEnvironmentVariable(const String& Key, const String& Value)
|
2022-05-21 19:58:09 +02:00
|
|
|
{
|
|
|
|
if (Key.Length() == 0 || Value.Length() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SetEnvironmentVariableA((const char*)Key, (const char*)Value);
|
|
|
|
}
|
|
|
|
|
2022-05-21 21:51:35 +02:00
|
|
|
String Environment::GetEnvironmentVariable(const String& Key)
|
2022-05-21 19:58:09 +02:00
|
|
|
{
|
|
|
|
char Buffer[1024]{};
|
|
|
|
GetEnvironmentVariableA((const char*)Key, Buffer, 1024);
|
|
|
|
|
2022-05-21 21:51:35 +02:00
|
|
|
return String(Buffer);
|
2022-05-21 19:58:09 +02:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:51:35 +02:00
|
|
|
String Environment::ExpandEnvironmentVariables(const String& Path)
|
2022-05-21 19:58:09 +02:00
|
|
|
{
|
|
|
|
char Buffer[4096]{};
|
|
|
|
ExpandEnvironmentStringsA((const char*)Path, Buffer, 4096);
|
|
|
|
|
|
|
|
// In theory, this should be ok, since, most paths will be used for actual file paths, < 260 chars anyways...
|
|
|
|
|
2022-05-21 21:51:35 +02:00
|
|
|
return String(Buffer);
|
2022-05-21 19:58:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool Environment::Is64BitProcess()
|
|
|
|
{
|
|
|
|
return (sizeof(uintptr_t) == 8);
|
|
|
|
}
|
|
|
|
}
|