码迷,mamicode.com
首页 > Windows程序 > 详细

通过读源码win10驱动下实现3环的GetEnvironmentVariable

时间:2018-12-27 18:35:18      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:starting   效果   uml   imu   pre   text   erro   getenv   sch   

效果图:
技术分享图片

NTSTATUS NTAPI
RtlQueryEnvironmentVariable_U(PWSTR Environment,
    PCUNICODE_STRING Name,
    PUNICODE_STRING Value)
{
    NTSTATUS Status;
    PWSTR wcs;
    UNICODE_STRING var;
    PWSTR val;
    BOOLEAN SysEnvUsed = FALSE;

    DbgPrint("RtlQueryEnvironmentVariable_U Environment %p Variable %wZ Value %p\n",
        Environment, Name, Value);

    if (Environment == NULL)
    {
        MPPEB Peb = RtlGetCurrentPeb();
        if (Peb) {
            //RtlAcquirePebLock();

            Environment = Peb->ProcessParameters->Environment;
            SysEnvUsed = TRUE;
        }
    }

    if (Environment == NULL)
    {
        //if (SysEnvUsed)
            //RtlReleasePebLock();
        return(STATUS_VARIABLE_NOT_FOUND);
    }

    Value->Length = 0;

    wcs = Environment;
    DbgPrint("Starting search at :%p\n", wcs);
    while (*wcs)
    {
        var.Buffer = wcs++;
        wcs = wcschr(wcs, L‘=‘);
        if (wcs == NULL)
        {
            wcs = var.Buffer + wcslen(var.Buffer);
            DbgPrint("Search at :%S\n", wcs);
        }
        if (*wcs)
        {
            var.Length = var.MaximumLength = (USHORT)(wcs - var.Buffer) * sizeof(WCHAR);
            val = ++wcs;
            wcs += wcslen(wcs);
            DbgPrint("Search at :%S\n", wcs);

            if (RtlEqualUnicodeString(&var, Name, TRUE))
            {
                Value->Length = (USHORT)(wcs - val) * sizeof(WCHAR);
                if (Value->Length <= Value->MaximumLength)
                {
                    memcpy(Value->Buffer, val,
                        min(Value->Length + sizeof(WCHAR), Value->MaximumLength));
                    DbgPrint("Value %S\n", val);
                    DbgPrint("Return STATUS_SUCCESS\n");
                    Status = STATUS_SUCCESS;
                }
                else
                {
                    DbgPrint("Return STATUS_BUFFER_TOO_SMALL\n");
                    Status = STATUS_BUFFER_TOO_SMALL;
                }

                //if (SysEnvUsed)
                    //RtlReleasePebLock();

                return(Status);
            }
        }
        wcs++;
    }

    /*if (SysEnvUsed)
        RtlReleasePebLock();
*/
    DbgPrint("Return STATUS_VARIABLE_NOT_FOUND: %wZ\n", Name);
    return(STATUS_VARIABLE_NOT_FOUND);
}
//获取环境变量 
DWORD My_Get_Environment_Variable(IN LPCWSTR lpName,
    IN LPWSTR lpBuffer,
    IN DWORD nSize) {
    UNICODE_STRING VarName, VarValue;
    NTSTATUS Status;
    USHORT UniSize;

    if (nSize <= (UNICODE_STRING_MAX_CHARS - 1))
    {
        if (nSize)
        {
            UniSize = (USHORT)nSize * sizeof(WCHAR) - sizeof(UNICODE_NULL);
        }
        else
        {
            UniSize = 0;
        }
    }
    else
    {
        UniSize = UNICODE_STRING_MAX_BYTES - sizeof(UNICODE_NULL);
    }

    Status = RtlInitUnicodeStringEx(&VarName, lpName);
    if (!NT_SUCCESS(Status))
    {
        BaseSetLastNTError(Status);
        return 0;
    }

    RtlInitEmptyUnicodeString(&VarValue, lpBuffer, UniSize);

    Status = RtlQueryEnvironmentVariable_U(NULL, &VarName, &VarValue);
    if (!NT_SUCCESS(Status))
    {
    DbgPrint("RtlQueryEnvironmentVariable_U----------- %S\n", VarValue);
        if (Status == STATUS_BUFFER_TOO_SMALL)
        {
            return (VarValue.Length / sizeof(WCHAR)) + sizeof(ANSI_NULL);
        }
        BaseSetLastNTError(Status);
        return 0;
    }

    lpBuffer[VarValue.Length / sizeof(WCHAR)] = UNICODE_NULL;

    return (VarValue.Length / sizeof(WCHAR));
}

用法:

wchar_t buffer[256];
    wchar_t pwcDevNameBuf = NULL;
    DWORD code = My_Get_Environment_Variable(L"TEMP", buffer, 256);
    DbgPrint("buffer----------- %S\n", buffer);

通过读源码win10驱动下实现3环的GetEnvironmentVariable

标签:starting   效果   uml   imu   pre   text   erro   getenv   sch   

原文地址:http://blog.51cto.com/haidragon/2336002

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!