示例#1
0
文件: userenv_main.c 项目: bpon/wine
static void set_registry_variables(WCHAR **env, HKEY hkey, DWORD type, BOOL set_path)
{
    static const WCHAR SystemRootW[] = {'S','y','s','t','e','m','R','o','o','t',0};
    static const WCHAR SystemDriveW[] = {'S','y','s','t','e','m','D','r','i','v','e',0};
    static const WCHAR PATHW[] = {'P','A','T','H'};

    UNICODE_STRING us_name, us_value;
    WCHAR name[1024], value[1024];
    DWORD ret, index, size;

    for (index = 0; ; index++)
    {
        size = sizeof(name)/sizeof(WCHAR);
        ret = RegEnumValueW(hkey, index, name, &size, NULL, NULL, NULL, NULL);
        if (ret != ERROR_SUCCESS)
            break;

        if (!memicmpW(name, SystemRootW, sizeof(SystemRootW)/sizeof(WCHAR)))
            continue;
        if (!memicmpW(name, SystemDriveW, sizeof(SystemDriveW)/sizeof(WCHAR)))
            continue;

        RtlInitUnicodeString(&us_name, name);
        us_value.Buffer = value;
        us_value.MaximumLength = sizeof(value);
        if (!memicmpW(name, PATHW, sizeof(PATHW)/sizeof(WCHAR)) &&
                !RtlQueryEnvironmentVariable_U(*env, &us_name, &us_value))
        {
            if (!set_path)
                continue;

            size = strlenW(value)+1;
            if (!get_reg_value(*env, hkey, name, value+size,
                        sizeof(value)-size*sizeof(WCHAR)))
                continue;

            value[size] = ';';
            RtlInitUnicodeString(&us_value, value);
            RtlSetEnvironmentVariable(env, &us_name, &us_value);
            continue;
        }

        if (!get_reg_value(*env, hkey, name, value, sizeof(value)))
            continue;

        if(!value[0])
            continue;

        RtlInitUnicodeString(&us_value, value);
        RtlSetEnvironmentVariable(env, &us_name, &us_value);
    }
}
示例#2
0
文件: env.c 项目: kika123/nativeshell
/*
 * @implemented
 */
BOOL
WINAPI
SetEnvironmentVariableA (
	LPCSTR	lpName,
	LPCSTR	lpValue
	)
{
	ANSI_STRING VarName;
	ANSI_STRING VarValue;
	UNICODE_STRING VarNameU;
	UNICODE_STRING VarValueU;
	NTSTATUS Status;

	DPRINT("SetEnvironmentVariableA(Name '%s', Value '%s')\n", lpName, lpValue);

	RtlInitAnsiString (&VarName,
	                   (LPSTR)lpName);
	RtlAnsiStringToUnicodeString (&VarNameU,
	                              &VarName,
	                              TRUE);

	if (lpValue)
	{
		RtlInitAnsiString (&VarValue,
		                   (LPSTR)lpValue);
		RtlAnsiStringToUnicodeString (&VarValueU,
		                              &VarValue,
		                              TRUE);

		Status = RtlSetEnvironmentVariable (NULL,
		                                    &VarNameU,
		                                    &VarValueU);

		RtlFreeUnicodeString (&VarValueU);
	}
	else
	{
		Status = RtlSetEnvironmentVariable (NULL,
		                                    &VarNameU,
		                                    NULL);
	}
	RtlFreeUnicodeString (&VarNameU);

	if (!NT_SUCCESS(Status))
	{
		SetLastErrorByStatus (Status);
		return FALSE;
	}

	return TRUE;
}
示例#3
0
文件: env.c 项目: kika123/nativeshell
/*
 * @implemented
 */
BOOL
WINAPI
SetEnvironmentVariableW (
	LPCWSTR	lpName,
	LPCWSTR	lpValue
	)
{
	UNICODE_STRING VarName;
	UNICODE_STRING VarValue;
	NTSTATUS Status;

	DPRINT("SetEnvironmentVariableW(Name '%S', Value '%S')\n", lpName, lpValue);

	RtlInitUnicodeString (&VarName,
	                      lpName);

	RtlInitUnicodeString (&VarValue,
	                      lpValue);

	Status = RtlSetEnvironmentVariable (NULL,
	                                    &VarName,
	                                    &VarValue);

	if (!NT_SUCCESS(Status))
	{
		SetLastErrorByStatus (Status);
		return FALSE;
	}

	return TRUE;
}
示例#4
0
文件: env.c 项目: genbtc/UltraDefrag
/**
 * @brief Sets an environment variable.
 * @param[in] name the environment variable name.
 * @param[in] value the null-terminated value string.
 * NULL pointer causes a variable deletion.
 * @return Zero for success, negative value otherwise.
 * @note value buffer size must not exceed 32767 characters,
 * including terminal zero, as mentioned in MSDN. This is
 * because unsigned short data type can hold numbers
 * less than or equal to 32767.
 */
int winx_setenv(wchar_t *name, wchar_t *value)
{
    UNICODE_STRING n, v;
    NTSTATUS status;

    DbgCheck1(name,-1);

    RtlInitUnicodeString(&n,name);
    if(value){
        if(value[0]){
            RtlInitUnicodeString(&v,value);
            status = RtlSetEnvironmentVariable(NULL,&n,&v);
        } else {
            status = RtlSetEnvironmentVariable(NULL,&n,NULL);
        }
    } else {
        status = RtlSetEnvironmentVariable(NULL,&n,NULL);
    }
    if(!NT_SUCCESS(status)){
        strace(status,"cannot set %ws",name);
        return (-1);
    }
    return 0;
}
示例#5
0
static
BOOL
AppendUserEnvironmentVariable(PWSTR* Environment,
                              LPWSTR lpName,
                              LPWSTR lpValue)
{
    NTSTATUS Status;
    UNICODE_STRING Name, Value;

    RtlInitUnicodeString(&Name, lpName);

    Value.Length = 0;
    Value.MaximumLength = 1024 * sizeof(WCHAR);
    Value.Buffer = LocalAlloc(LPTR, Value.MaximumLength);
    if (Value.Buffer == NULL)
        return FALSE;

    Value.Buffer[0] = UNICODE_NULL;

    Status = RtlQueryEnvironmentVariable_U(*Environment,
                                           &Name,
                                           &Value);
    if (NT_SUCCESS(Status))
        RtlAppendUnicodeToString(&Value, L";");

    RtlAppendUnicodeToString(&Value, lpValue);

    Status = RtlSetEnvironmentVariable(Environment,
                                       &Name,
                                       &Value);
    LocalFree(Value.Buffer);
    if (!NT_SUCCESS(Status))
    {
        DPRINT1("RtlSetEnvironmentVariable() failed (Status %lx)\n", Status);
        return FALSE;
    }

    return TRUE;
}
示例#6
0
VOID AppendPackage()
{
    PLDR_MODULE Self;
    UNICODE_STRING SelfPath;

    static WCHAR PythonZip[] = L"python.zip";

    ml::MlInitialize();

    //Py_IgnoreEnvironmentFlag = TRUE;
    //Py_NoSiteFlag = TRUE;
    Py_DontWriteBytecodeFlag = TRUE;
    //Py_NoUserSiteDirectory = TRUE;

    Self = FindLdrModuleByHandle(nullptr);

    SelfPath = Self->FullDllName;
    SelfPath.Length -= Self->BaseDllName.Length;

    Py_SetPath(ml::String::Format(
        L"%wZ;%wZ%s;%wZ%s\\site-packages;%wZlib;%wZDLLs;%wZUserSite",
        &SelfPath,                      // exe path
        &SelfPath, PythonZip,           // ./python.zip
        &SelfPath, PythonZip,           // ./python.zip/site-packages
        &SelfPath,                      // ./lib
        &SelfPath,                      // ./DLLs
        &SelfPath                       // ./UserSite
    ));

    ml::String      PathEnv, UserSite;
    PWSTR           EnvBuffer;
    ULONG           Length;
    UNICODE_STRING  Path;

    RtlInitEmptyString(&Path);
    RtlExpandEnvironmentStrings_U(nullptr, &USTR(L"%Path%"), &Path, &Length);

    EnvBuffer = (PWSTR)AllocStack(Length);
    RtlInitEmptyString(&Path, EnvBuffer, Length);

    RtlExpandEnvironmentStrings_U(nullptr, &USTR(L"%Path%"), &Path, nullptr);

    UserSite = SelfPath;
    UserSite += L"UserSite";

    PathEnv = SelfPath;
    PathEnv += L"DLLs;";

    EnumDirectoryFiles(
        nullptr, L"*.*", 0, UserSite, nullptr,
        [] (PVOID Buffer, PWIN32_FIND_DATAW FindData, ULONG_PTR Context) -> LONG
        {
            ml::String *PathEnv = (ml::String *)Context;

            if (FLAG_OFF(FindData->dwFileAttributes, FILE_ATTRIBUTE_DIRECTORY))
                return 0;

            (*PathEnv) += FindData->cFileName;
            (*PathEnv) += ';';

            return 0;
        },
        (ULONG_PTR)&PathEnv,
        EDF_PROCDIR | EDF_BEFORE
    );


    PathEnv += Path;
    PathEnv += ';';
    RtlSetEnvironmentVariable(nullptr, &USTR(L"Path"), PathEnv);
}
示例#7
0
static
BOOL
SetUserEnvironmentVariable(PWSTR* Environment,
                           LPWSTR lpName,
                           LPWSTR lpValue,
                           BOOL bExpand)
{
    NTSTATUS Status;
    UNICODE_STRING Name;
    UNICODE_STRING SrcValue, DstValue;
    ULONG Length;
    PVOID Buffer = NULL;
    WCHAR ShortName[MAX_PATH];

    if (bExpand)
    {
        RtlInitUnicodeString(&SrcValue, lpValue);

        Length = 2 * MAX_PATH * sizeof(WCHAR);

        DstValue.Length = 0;
        DstValue.MaximumLength = Length;
        DstValue.Buffer = Buffer = LocalAlloc(LPTR, Length);
        if (DstValue.Buffer == NULL)
        {
            DPRINT1("LocalAlloc() failed\n");
            return FALSE;
        }

        Status = RtlExpandEnvironmentStrings_U(*Environment,
                                               &SrcValue,
                                               &DstValue,
                                               &Length);
        if (!NT_SUCCESS(Status))
        {
            DPRINT1("RtlExpandEnvironmentStrings_U() failed (Status %lx)\n", Status);
            DPRINT1("Length %lu\n", Length);

            if (Buffer)
                LocalFree(Buffer);

            return FALSE;
        }
    }
    else
    {
        RtlInitUnicodeString(&DstValue, lpValue);
    }

    if (!_wcsicmp(lpName, L"TEMP") || !_wcsicmp(lpName, L"TMP"))
    {
        if (GetShortPathNameW(DstValue.Buffer, ShortName, ARRAYSIZE(ShortName)))
        {
            RtlInitUnicodeString(&DstValue, ShortName);
        }
        else
        {
            DPRINT("GetShortPathNameW() failed for %S (Error %lu)\n", DstValue.Buffer, GetLastError());
        }

        DPRINT("Buffer: %S\n", ShortName);
    }

    RtlInitUnicodeString(&Name, lpName);

    DPRINT("Value: %wZ\n", &DstValue);

    Status = RtlSetEnvironmentVariable(Environment,
                                       &Name,
                                       &DstValue);

    if (Buffer)
        LocalFree(Buffer);

    if (!NT_SUCCESS(Status))
    {
        DPRINT1("RtlSetEnvironmentVariable() failed (Status %lx)\n", Status);
        return FALSE;
    }

    return TRUE;
}
示例#8
0
文件: userenv_main.c 项目: bpon/wine
BOOL WINAPI CreateEnvironmentBlock( LPVOID* lpEnvironment,
                     HANDLE hToken, BOOL bInherit )
{
    static const WCHAR env_keyW[] = {'S','y','s','t','e','m','\\',
        'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
        'C','o','n','t','r','o','l','\\',
        'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r','\\',
        'E','n','v','i','r','o','n','m','e','n','t',0};
    static const WCHAR profile_keyW[] = {'S','o','f','t','w','a','r','e','\\',
        'M','i','c','r','o','s','o','f','t','\\',
        'W','i','n','d','o','w','s',' ','N','T','\\',
        'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
        'P','r','o','f','i','l','e','L','i','s','t',0};
    static const WCHAR envW[] = {'E','n','v','i','r','o','n','m','e','n','t',0};
    static const WCHAR volatile_envW[] = {'V','o','l','a','t','i','l','e',' ','E','n','v','i','r','o','n','m','e','n','t',0};
    static const WCHAR ProfilesDirectoryW[] = {'P','r','o','f','i','l','e','s','D','i','r','e','c','t','o','r','y',0};

    static const WCHAR SystemRootW[] = {'S','y','s','t','e','m','R','o','o','t',0};
    static const WCHAR SystemDriveW[] = {'S','y','s','t','e','m','D','r','i','v','e',0};
    static const WCHAR AllUsersProfileW[] = {'A','l','l','U','s','e','r','s','P','r','o','f','i','l','e',0};
    static const WCHAR ALLUSERSPROFILEW[] = {'A','L','L','U','S','E','R','S','P','R','O','F','I','L','E',0};
    static const WCHAR USERNAMEW[] = {'U','S','E','R','N','A','M','E',0};
    static const WCHAR USERPROFILEW[] = {'U','S','E','R','P','R','O','F','I','L','E',0};
    static const WCHAR DefaultW[] = {'D','e','f','a','u','l','t',0};
    static const WCHAR COMPUTERNAMEW[] = {'C','O','M','P','U','T','E','R','N','A','M','E',0};

    WCHAR *env, buf[UNICODE_STRING_MAX_CHARS], profiles_dir[MAX_PATH];
    UNICODE_STRING us_name, us_val;
    DWORD len;
    HKEY hkey, hsubkey;

    TRACE("%p %p %d\n", lpEnvironment, hToken, bInherit );

    if (!lpEnvironment)
        return FALSE;

    if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, env_keyW, 0, KEY_READ, &hkey) != ERROR_SUCCESS)
        return FALSE;

    if (RtlCreateEnvironment(bInherit, &env) != STATUS_SUCCESS)
    {
        RegCloseKey(hkey);
        return FALSE;
    }

    if (!GetEnvironmentVariableW(SystemRootW, buf, UNICODE_STRING_MAX_CHARS))
    {
        if (!get_reg_value(env, hkey, SystemRootW, buf, UNICODE_STRING_MAX_CHARS))
        {
            buf[0] = 0;
            WARN("SystemRoot variable not set\n");
        }
    }
    RtlInitUnicodeString(&us_name, SystemRootW);
    RtlInitUnicodeString(&us_val, buf);
    RtlSetEnvironmentVariable(&env, &us_name, &us_val);

    if (!GetEnvironmentVariableW(SystemDriveW, buf, UNICODE_STRING_MAX_CHARS))
    {
        if (!get_reg_value(env, hkey, SystemRootW, buf, UNICODE_STRING_MAX_CHARS))
        {
            buf[0] = 0;
            WARN("SystemDrive variable not set\n");
        }
    }
    RtlInitUnicodeString(&us_name, SystemDriveW);
    RtlInitUnicodeString(&us_val, buf);
    RtlSetEnvironmentVariable(&env, &us_name, &us_val);

    set_registry_variables(&env, hkey, REG_SZ, !bInherit);
    set_registry_variables(&env, hkey, REG_EXPAND_SZ, !bInherit);

    if (RegOpenKeyExW(hkey, envW, 0, KEY_READ, &hsubkey) == ERROR_SUCCESS)
    {
        set_registry_variables(&env, hsubkey, REG_SZ, !bInherit);
        set_registry_variables(&env, hsubkey, REG_EXPAND_SZ, !bInherit);
        RegCloseKey(hsubkey);
    }

    if (RegOpenKeyExW(hkey, volatile_envW, 0, KEY_READ, &hsubkey) == ERROR_SUCCESS)
    {
        set_registry_variables(&env, hsubkey, REG_SZ, !bInherit);
        set_registry_variables(&env, hsubkey, REG_EXPAND_SZ, !bInherit);
        RegCloseKey(hsubkey);
    }
    RegCloseKey(hkey);

    if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, profile_keyW, 0, KEY_READ, &hkey) == ERROR_SUCCESS)
    {
        if (get_reg_value(env, hkey, ProfilesDirectoryW, profiles_dir, MAX_PATH-sizeof(WCHAR)))
        {
            len = strlenW(profiles_dir);
            if (profiles_dir[len-1] != '\\')
            {
                profiles_dir[len++] = '\\';
                profiles_dir[len] = '\0';
            }

            memcpy(buf, profiles_dir, len*sizeof(WCHAR));
            if (get_reg_value(env, hkey, AllUsersProfileW, buf+len, UNICODE_STRING_MAX_CHARS-len))
            {
                RtlInitUnicodeString(&us_name, ALLUSERSPROFILEW);
                RtlInitUnicodeString(&us_val, buf);
                RtlSetEnvironmentVariable(&env, &us_name, &us_val);
            }
        }
        else
        {
            profiles_dir[0] = 0;
        }

        RegCloseKey(hkey);
    }

    len = sizeof(buf)/sizeof(WCHAR);
    if (GetComputerNameW(buf, &len))
    {
        RtlInitUnicodeString(&us_name, COMPUTERNAMEW);
        RtlInitUnicodeString(&us_val, buf);
        RtlSetEnvironmentVariable(&env, &us_name, &us_val);
    }

    set_wow64_environment(&env);

    if (!hToken)
    {
        if (profiles_dir[0])
        {
            len = strlenW(profiles_dir);
            if (len*sizeof(WCHAR)+sizeof(DefaultW) < sizeof(buf))
            {
                memcpy(buf, profiles_dir, len*sizeof(WCHAR));
                memcpy(buf+len, DefaultW, sizeof(DefaultW));
                RtlInitUnicodeString(&us_name, USERPROFILEW);
                RtlInitUnicodeString(&us_val, buf);
                RtlSetEnvironmentVariable(&env, &us_name, &us_val);
            }
        }

        buf[0] = '.';
        memcpy(buf+1, DefaultW, sizeof(DefaultW));
    }
    else
    {
        TOKEN_USER *token_user = NULL;
        SID_NAME_USE use;
        WCHAR *sidW;
        DWORD size, tmp=0;

        if (GetTokenInformation(hToken, TokenUser, NULL, 0, &len) ||
                GetLastError()!=ERROR_INSUFFICIENT_BUFFER ||
                !(token_user = HeapAlloc(GetProcessHeap(), 0, len)) ||
                !GetTokenInformation(hToken, TokenUser, token_user, len, &len) ||
                !ConvertSidToStringSidW(token_user->User.Sid, &sidW))
        {
            HeapFree(GetProcessHeap(), 0, token_user);
            RtlDestroyEnvironment(env);
            return FALSE;
        }

        len = strlenW(profiles_dir);
        memcpy(buf, profiles_dir, len*sizeof(WCHAR));

        size = UNICODE_STRING_MAX_CHARS-len;
        if (LookupAccountSidW(NULL, token_user->User.Sid,
                    buf+len, &size, NULL, &tmp, &use))
        {
            RtlInitUnicodeString(&us_name, USERNAMEW);
            RtlInitUnicodeString(&us_val, buf+len);
            RtlSetEnvironmentVariable(&env, &us_name, &us_val);

            if (len)
            {
                RtlInitUnicodeString(&us_name, USERPROFILEW);
                RtlInitUnicodeString(&us_val, buf);
                RtlSetEnvironmentVariable(&env, &us_name, &us_val);
            }
        }

        HeapFree(GetProcessHeap(), 0, token_user);
        strcpyW(buf, sidW);
        LocalFree(sidW);
    }

    if (RegOpenKeyExW(HKEY_USERS, buf, 0, KEY_READ, &hkey) == ERROR_SUCCESS)
    {
        if (RegOpenKeyExW(hkey, envW, 0, KEY_READ, &hsubkey) == ERROR_SUCCESS)
        {
            set_registry_variables(&env, hsubkey, REG_SZ, !bInherit);
            set_registry_variables(&env, hsubkey, REG_EXPAND_SZ, !bInherit);
            RegCloseKey(hsubkey);
        }

        if (RegOpenKeyExW(hkey, volatile_envW, 0, KEY_READ, &hsubkey) == ERROR_SUCCESS)
        {
            set_registry_variables(&env, hsubkey, REG_SZ, !bInherit);
            set_registry_variables(&env, hsubkey, REG_EXPAND_SZ, !bInherit);
            RegCloseKey(hsubkey);
        }
        RegCloseKey(hkey);
    }

    *lpEnvironment = env;
    return TRUE;
}
示例#9
0
文件: userenv_main.c 项目: bpon/wine
static void set_wow64_environment(WCHAR **env)
{
    static const WCHAR versionW[] = {'S','o','f','t','w','a','r','e','\\',
        'M','i','c','r','o','s','o','f','t','\\',
        'W','i','n','d','o','w','s','\\',
        'C','u','r','r','e','n','t','V','e','r','s','i','o','n',0};
    static const WCHAR progdirW[]   = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r',0};
    static const WCHAR progdir86W[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r',' ','(','x','8','6',')',0};
    static const WCHAR progfilesW[] = {'P','r','o','g','r','a','m','F','i','l','e','s',0};
    static const WCHAR progw6432W[] = {'P','r','o','g','r','a','m','W','6','4','3','2',0};
    static const WCHAR commondirW[]   = {'C','o','m','m','o','n','F','i','l','e','s','D','i','r',0};
    static const WCHAR commondir86W[] = {'C','o','m','m','o','n','F','i','l','e','s','D','i','r',' ','(','x','8','6',')',0};
    static const WCHAR commonfilesW[] = {'C','o','m','m','o','n','P','r','o','g','r','a','m','F','i','l','e','s',0};
    static const WCHAR commonw6432W[] = {'C','o','m','m','o','n','P','r','o','g','r','a','m','W','6','4','3','2',0};

    UNICODE_STRING nameW, valueW;
    WCHAR buf[64];
    HKEY hkey;
    BOOL is_win64 = (sizeof(void *) > sizeof(int));
    BOOL is_wow64;

    IsWow64Process( GetCurrentProcess(), &is_wow64 );

    if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, versionW, 0,
                KEY_READ|KEY_WOW64_64KEY, &hkey))
        return;

    /* set the ProgramFiles variables */

    if (get_reg_value(*env, hkey, progdirW, buf, sizeof(buf)))
    {
        if (is_win64 || is_wow64)
        {
            RtlInitUnicodeString(&nameW, progw6432W);
            RtlInitUnicodeString(&valueW, buf);
            RtlSetEnvironmentVariable(env, &nameW, &valueW);
        }
        if (is_win64 || !is_wow64)
        {
            RtlInitUnicodeString(&nameW, progfilesW);
            RtlInitUnicodeString(&valueW, buf);
            RtlSetEnvironmentVariable(env, &nameW, &valueW);
        }
    }
    if (is_wow64 && get_reg_value(*env, hkey, progdir86W, buf, sizeof(buf)))
    {
        RtlInitUnicodeString(&nameW, progfilesW);
        RtlInitUnicodeString(&valueW, buf);
        RtlSetEnvironmentVariable(env, &nameW, &valueW);
    }

    /* set the CommonProgramFiles variables */

    if (get_reg_value(*env, hkey, commondirW, buf, sizeof(buf)))
    {
        if (is_win64 || is_wow64)
        {
            RtlInitUnicodeString(&nameW, commonw6432W);
            RtlInitUnicodeString(&valueW, buf);
            RtlSetEnvironmentVariable(env, &nameW, &valueW);
        }
        if (is_win64 || !is_wow64)
        {
            RtlInitUnicodeString(&nameW, commonfilesW);
            RtlInitUnicodeString(&valueW, buf);
            RtlSetEnvironmentVariable(env, &nameW, &valueW);
        }
    }
    if (is_wow64 && get_reg_value(*env, hkey, commondir86W, buf, sizeof(buf)))
    {
        RtlInitUnicodeString(&nameW, commonfilesW);
        RtlInitUnicodeString(&valueW, buf);
        RtlSetEnvironmentVariable(env, &nameW, &valueW);
    }

    RegCloseKey(hkey);
}