Microsoft KB Archive/305136

From BetaArchive Wiki
Knowledge Base


BUG: CreateEnvironmentBlock Fails With Error 203 If the User's Profile Is Not Loaded

Article ID: 305136

Article Last Modified on 11/21/2006



APPLIES TO

  • Microsoft Win32 Application Programming Interface, when used with:
    • Microsoft Windows NT 4.0
    • Microsoft Windows 2000 Standard Edition
    • Microsoft Windows XP Professional



This article was previously published under Q305136

SYMPTOMS

The CreateEnvironmentBlock API will fail if it is used to retrieve environment variables for a user whose profile is not currently loaded. This is by design. However, when this failure occurs, GetLastError() returns error code 203 (ERROR_ENVVAR_NOT_FOUND), which is not a meaningful error under these circumstances.

RESOLUTION

An application should load the user's profile prior to calling CreateEnvironmentBlock. This can be achieved with the LoadUserProfile API. However, this requires administrative privileges. More specifically, LoadUserProfile can only be called by threads running under the LocalSystem account or a local administrator's account.

NOTED EXCEPTIONS

There are some instances in which it is not necessary to call LoadUserProfile to load a user's profile prior to calling CreateEnvironmentBlock. These are as follows:

  • If the user specified by the hToken member of CreateEnvironmentBlock is the interactive user, the user's profile will already be loaded. In this case, it is not necessary to call LoadUserProfile.
  • CreateEnvironmentBlock can be used to retrieve only system environment variables by specifying NULL for the hToken parameter. In this case, the user's profile does not need to be loaded at all.
  • CreateEnvironmentBlock can be used to build an environment block that is identical to the current process's environment block by specifying TRUE for the bInherit parameter. In this case, the user's profile does not have to be loaded at all.


STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

Keywords: kbapi kbbug kbkernbase kbregistry kbsecurity KB305136