Retrieving PATH Environment Variable

This is a discussion on Retrieving PATH Environment Variable within the Windows Programming forums, part of the Platform Specific Boards category; Does anyone know if there is a way to read the PATH Environment Variable that the System uses in Windows ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    88

    Retrieving PATH Environment Variable

    Does anyone know if there is a way to read the PATH Environment Variable that the System uses in Windows XP/2k without directly reading it from the Registry? I am trying to find out if there is an API that accomplishes it.

    GetEnvironmentVariable and GetEnvironmentStrings both say they retrieve the information from the environment block the program is being run under (i.e. a user), but I want to retrieve the one the System uses.

    Also, does anyone know how the paths in path are enumerated when searching for a file. Is it in the order they exist in the registry value data?

    Thank you very much for any information you can provide.

  2. #2
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Code:
    #include <stdlib.h>
    
    char* path = getenv("PATH");

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    88
    OnionKnight: I was under the impression that the path retrieved with getenv returns to path environment of the current environment block and not the System path (which can be different depending on how your process was created). Is that incorrect?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Well if something chooses to modify the environment before calling your process, there isn't a lot you can do about it.

    Unless you want to reconsider your "without using the registry" condition.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    88
    Salem: I am extremely comfortable with using the registry to obtain the information, I just found it odd that Microsoft has no direct API call to retrieve information that is rather essential. If there is no function, I will implement it that way, but I do not want to reinvent the wheel (and possibly be faced with changes in where the information is kept between versions of windows).

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The paths are searched in the order they are in PATH. In other words, by placing a path earlier you can override later paths.

    I don't see how the information here is rather essential. Your child processes inherit your environment. system() uses your process's PATH, ShellExecute(Ex) don't say anything, but I don't think they use any path at all, only the current working directory (this is my best guess based on the docs). CreateProcess is explicitely documented to not use the search path. exec() uses your process's PATH, but only in the *p variants. Same for spawn(). _popen() is incompletely documented, but if it follows the POSIX spec it behaves like system().

    So unless you want to display and modify the "system PATH" as a system configuration utility, you really don't need it. And if you want to do that, messing with the registry is appropriate.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    88
    CornedBee: Thank you for the info. Viewing and modifying important aspects of the OS is obviously a large part of the reason the API exists to begin with. PATH is an important part of most OS's. I just found it a little strange that I was unable to find an API function that views and modifies it.

    Edit: Also, to understand what the OS will launch when something is added to the registry to launch, you must know the system path and not your evironment block path. I just found it a little odd that something that is useful is not wrapped with some API function.

    By the way, were you ever on vbforums? I believe there was a CornedBee on the forums back when I programmed more heavily in VB and was on the forums. You were very helpful then too if it was you.
    Last edited by mercury529; 11-11-2006 at 12:35 PM.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It was me and I'm still there. The name CornedBee is unique across the internet. (Something I'm rather proud of.)

    I rather think the registry should always contain the full path to any program specified therein and never rely on PATH. Relying on PATH is always a security concern in that a malicious program might be able to place itself in a spot of low security that happens to be on the PATH before a program of the same name. Then, a system account could try to execute the program with that name and execute the malicious program instead - with full privileges. (Of course, the real security issue here is that an improperly secured directory is on the PATH of the privileged user in the first place, but you know how it works. Multiple layers of defence and all that.)

    I don't agree that the default PATH is an important aspect of the OS that a dedicated API should be provided for. But whether or not I agree is not important; the fact is that no such API exists and you do have to modify the registry.

    Don't forget that CURRENT_USER and LOCAL_MACHINE each have their own PATH, which get merged into the full PATH whenever a program is started by the shell.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    88
    Thanks to everyone.

    CornedBee: I agree completely with the PATH hijacking mechanism you described. It becomes a security hole but does exist for a reasonable purpose. As for the existence of the API, like you said, it does not really matter whether either of us thinks it should exist or not, it does not.

    What I am trying to accomplish is determining what file an auto-starting location will run in the registry. To accomplish this initially I was using SearchPath to resolve the file path of entries that contained only a file name. However, this mechanism does not work because the directory the executable using SearchPath is run from takes precedence over all other locations. Also the documentation for the API is vague with how the PATH variable is acquired. Thus, the file path returned by SearchPath will not necessarily match what file is being launched by the registry. So I am attempting now to implement the technique used by SearchPath in a way that is appropriate for windows launching the file.

    So my SearchPath is
    1.) The System Directory (GetSystemDirectory usually System32)
    2.) The 16-bit System Directory (System)
    3.) The Windows Directory (GetWindowsDirectory)
    4.) The PATH variable (HKLM\System\CCS\Control\Session Manager\Environment\Path)

    My testing seems to confirm this is how Windows resolves the path when it automatically runs a file. I am not 100% certain, so if you know anything to the contrary, I'd appreciate the knowledge. I imagine the Current User Path is not taken into account unless something is being run from the Current User reg hive? Does CU Path have a higher or lower precedence than the Local Machine PATH variable? Thank you very much for all your help.

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    88
    The shell function PathFindOnPath seems to be the exact functionality I was looking for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (VC++ related) Unicode or Multi-byte environment?
    By PaulBlay in forum C Programming
    Replies: 3
    Last Post: 05-22-2009, 08:42 AM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Path / Environment in unix
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-10-2001, 04:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21