2
votes

If I try to create an environment block from within a 32 bit process (on a 64 bit OS) using CreateEnvironmentBlock() the environment block I get seems to be mostly correct but it has a few differences from one that gets created automatically by CreateProcess. Most notably PROCESSOR_ARCHITECTURE is usually x86 on 32 bit processes but the one from CreateEnvironmentBlock is amd64.

BOOL bResult = FALSE;
LPWSTR wszEnvBlock = NULL;
HANDLE tokenHandle;

OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &tokenHandle);

bResult = ::CreateEnvironmentBlock((LPVOID*)&wszEnvBlock, tokenHandle, TRUE);

LPWSTR wszCurrentItem = wszEnvBlock;
LPWSTR wszEqualsSign = NULL;
LPWSTR wszCurrentItemInuse = NULL;

ATL::CStringW wstrCurrentName;
ATL::CStringW wstrCurrentValue;

while (L'\0' != *wszCurrentItem)
{
    // Find the equals and temporarily set it to NULL
    wszCurrentItemInuse = wszCurrentItem;
    wszEqualsSign = wcschr(wszCurrentItem, L'=');
    *wszEqualsSign = L'\0';

    // Copy the Name and then set the equals back as it was
    wstrCurrentName = wszCurrentItem;
    *wszEqualsSign = L'=';

    // Move the current item to the next character after the equals sign,
    // Then copy the Value
    wszCurrentItem = ++wszEqualsSign;
    wstrCurrentValue = wszCurrentItem;

    // Move the current item to the next character after the terminating NULL character.
    wszCurrentItem = wcschr(wszCurrentItem, L'\0');
    wszCurrentItem++;

    // Insert the two read strings into the map
    wprintf(L"%s:%s\n", wstrCurrentName, wstrCurrentValue);
}

bResult = DestroyEnvironmentBlock((LPVOID)wszEnvBlock);

Produces the following output:

ADMSOURCE:\ddwds02\platform8\DesktopPersonalisation\Win7\GO\Source\adm ALLUSERSPROFILE:C:\ProgramData APPDATA:C:\Users\bens\AppData\Roaming asl.log:Destination=file AS_WDK6_DIR:C:\WinDDK\6000 AS_WDK7_DIR:C:\WinDDK\7600.16385.0 CLASSPATH:.;C:\Program Files (x86)\Java\jre6\lib\ext\QTJava.zip CommonProgramFiles:C:\Program Files (x86)\Common Files CommonProgramFiles(x86):C:\Program Files (x86)\Common Files CommonProgramW6432:C:\Program Files\Common Files COMPUTERNAME:APWADEV03 ComSpec:C:\Windows\system32\cmd.exe CYGWIN:nodosfilewarning DEFAULT_CA_NR:CA100 DEVELOPMENT:c:\development DEVLIBS:C:\development\libs FP_NO_HOST_CHECK:NO HOME:c:\users\bens HOMEDRIVE:C: HOMEPATH:\Users\bens INCLUDE:C:\Program Files (x86)\Microsoft Visual Studio .NET 2003\SDK\v1.1\includ e\ LIB:C:\Program Files (x86)\Microsoft Visual Studio .NET 2003\SDK\v1.1\Lib\ LOCALAPPDATA:C:\Users\bens\AppData\Local LOCALHOMESHARE:C:\Users\bens LOGONSERVER:\APWADC01 NUMBER_OF_PROCESSORS:2 OS:Windows_NT PATHEXT:.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC PROCESSOR_ARCHITECTURE:AMD64 PROCESSOR_ARCHITEW6432:AMD64 PROCESSOR_IDENTIFIER:Intel64 Family 6 Model 23 Stepping 10, GenuineIntel PROCESSOR_LEVEL:6 PROCESSOR_REVISION:170a ProgramData:C:\ProgramData ProgramFiles:C:\Program Files (x86) ProgramFiles(x86):C:\Program Files (x86) ProgramW6432:C:\Program Files PSModulePath:C:\Windows\system32\WindowsPowerShell\v1.0\Modules\ PUBLIC:C:\Users\Public PVC_DDK_DIR:C:\WinDDK\6000 QTJAVA:C:\Program Files (x86)\Java\jre6\lib\ext\QTJava.zip SESSIONNAME:Console SP2C_ROOT:C:\Development\SP2C_Win7 SystemDrive:C: SystemRoot:C:\Windows TEMP:C:\Users\bens\AppData\Local\Temp TMP:C:\Users\bens\AppData\Local\Temp USERPROFILE:C:\Users\bens VisualStudioDir:C:\Users\bens\Documents\Visual Studio 2010 VS100COMNTOOLS:C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools \ VS71COMNTOOLS:C:\Program Files (x86)\Microsoft Visual Studio .NET 2003\Common7\T ools\ VS80COMNTOOLS:C:\Program Files (x86)\Microsoft Visual Studio 8\Common7\Tools\ VS90COMNTOOLS:c:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\ windir:C:\Windows WIX:C:\Program Files (x86)\Windows Installer XML v3\ WTTBIN:C:\Program Files\Microsoft Driver Test Manager\Controller\ _NT_SYMBOL_PATH:srv*c:\ websymbols*http://msdl.microsoft.com/download/symbols;sr v*c:\pdbs PATH:C:\Perl\site\bin;C:\Perl\bin;C:\Windows\system32;C:\Windows;C:\Windows\Syst em32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\GTK 2-Runtime\bin;C:\Program Files\Broadcom\Broadcom 802.11\Driver;C:\Program Files\ Microsoft Driver Test Manager\Controller\;C:\Program Files (x86)\FogBugz\FogBugz Command Line Client;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\ ;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsof t SQL Server\100\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools \Binn\VSShell\Common7\IDE\;C:\Program Files (x86)\Microsoft SQL Server\100\DTS\B inn\;C:\Program Files\TortoiseSVN\bin;C:\Program Files\SlikSvn\bin\;C:\cygwinx\b in;C:\Program Files\Microsoft Windows Performance Toolkit\;C:\Program Files (x86 )\Common Files\Teleca Shared;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Progr am Files (x86)\AMD\CodeAnalyst\bin;C:\Program Files (x86)\Nmap;c:\Users\bens\scr ipts\win;C:\Program Files (x86)\Microsoft Visual Studio 10.0\;C:\Program Files ( x86)\Microsoft Visual Studio 10.0\VC\bin;

So is CreateEnvironmentBlock just broken on wow64? Also, the system environment seems to be defined in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

How come there's no wow64 equivalent of this?

1
Hmm, looks like I'm not the first to see this. Maybe it's a bug? eggheadcafe.com/microsoft/Platform-SDK-Shell/32819766/…Benj
The bug is at CreateEnviromentBlock, however, the product team currently does not have the plan to fix it in Vista though the fix has been checked into the next version of Windows.... did you try this on Win7?user541686
Yes, I'm on Win 7, bug still seems to be there.Benj
The bug at the link you post is not the same as what you report hereDavid Heffernan
No, it's not exactly the same, that's true. It does however look like CreateEnvironmentBlock is still fairly broken.Benj

1 Answers

0
votes

This is a bug with CreateEnvironmentBlock(), MS are aware of the issue and have promised to fix it in some unspecified future release.