C++: Enumerating Environment

Objective

Enumerate username, computername and current working directory.

To do list

  • Create a class and enumerate specified information.

Functions

  • GetUserName: Enumerate current username.

    • Definition: GetUserName(LPSTR lpBuffer, LPDWORD pcbBuffer)

      • lpBuffer: Buffer to hold username string. TCHAR userName[ULEN + 1]

      • pcbBuffer: Pointer to buffer size. DWORD bufCharCount

  • GetComputerName: Enumerate current computername.

    • Definition: GetComputerName(LPSTR lpBuffer, LPDWORD

      • lpBuffer: Buffer to hold computername string. TCHAR computerName[ULEN + 1]

      • pcbBuffer: Pointer to buffer size. DWORD bufCharCount

  • GetCurrentDirectoryA: Enumerate current working directory.

    • Definition: GetCurrentDirectoryA(DWORD nBufferLength, LPSTR lpBuffer)

      • nBufferLength: Size of buffer. ULEN + 1

      • buffer: Buffer to hold directory string. TCHAR currentDirectory[ULEN + 1]

Application (enum-env.cpp)

#include <Windows.h>
#include <tchar.h>
#include <lmcons.h>
#include <stdio.h>

class Environment{

    private:
        TCHAR   userName[UNLEN + 1];
        TCHAR   computerName[UNLEN + 1];
        TCHAR   currentDirectory[UNLEN +1];  
        DWORD  bufCharCount = UNLEN + 1;

    public:
        Environment(){
            GetUserName( userName, &bufCharCount );
            GetComputerName( computerName, &bufCharCount );   
            GetCurrentDirectoryA( UNLEN + 1 , currentDirectory );
        }
        TCHAR* getUserName(){return &userName[0];}
        TCHAR* getComputerName(){return &computerName[0];}
        TCHAR* getCurrentDirectory(){return &currentDirectory[0];}
};

int main()
{
Environment env;

printf("Computer name: %s\nUser name: %s\nWorking Directory: %s", env.getComputerName(), env.getUserName(), env.getCurrentDirectory());

return 0;
}

Compile: gcc enum-env.cpp -o enum-env.exe

References:

Last updated

Was this helpful?