How do I obtain an environment Variable?

have 35 years of experience. I have mostly avoided the C world. when I've had to write in it, I've done OK in VS6. VS2005 C++ is so cantankerous.

I'm writing a program that must run standalone and it must be very lean and not rely upon redistributable dlls.

I am not in need of a routine that returns the value of the %windir% environment variable in string format. My project is set for a unicode build.

The design goal is to have only the executable image and no additional dlls other than
Native XP API dlls.

char* buffer;

string x;

buffer = getenv("windir");

x = string(buffer);

will not work because it requires MSVCR80D.dll.

Thanks... Renee.




Answer this question

How do I obtain an environment Variable?

  • J. Clark

    Bugsz that did it.. thanks!

    I still have tons of questions.

    I have the Windir and got it thusly:

    GetWindowsDirectory( infoBuf, INFO_BUFFER_SIZE );

    Infobuf is defined as:

    TCHAR infoBuf[INFO_BUFFER_SIZE];

    Questions:

    What does a TCHAR look like in memory

    How do I convert from a TCHAR to a string



  • Mykre

    The code has had that.

    What I am asking is how do I convert the contents of the TCHAR to a std::string



  • AnwarBasha

    The project is now failing when executed because it's me telling that DLL is not installed.

    This is what I have.... so far....

    Project is set for unicode build ...

    // Win32a.cpp : Defines the entry point for the application.

    //

    #include "stdafx.h"

    #include "Wintest.h"

    #include <cstring>

    #include <stdio.h>

    #include <string>

    #include <io.h>

    #include <windows.h>

    using namespace std;

    //Method Declarations

    bool IfFileExists(string FileSpec);

    string Decode(string inStr);

    //Member Variables

    #define INFO_BUFFER_SIZE 32767

    TCHAR infoBuf[INFO_BUFFER_SIZE];

    DWORD bufCharCount = INFO_BUFFER_SIZE;

    string FrameWDir = "E8^UKLFMYQ^Kkatmumhr0LGR^Dt_ocymti^t4,2,7.909\0";

    bool InstallFramework;

    int APIENTRY _tWinMain(HINSTANCE hInstance,

    HINSTANCE hPrevInstance,

    LPTSTR lpCmdLine,

    int nCmdShow)

    {

    string s = Decode(FrameWDir);

    InstallFramework =! IfFileExists(s.c_str());

    }

    bool IfFileExists(string FileSpec)

    { return ((_access(FileSpec.c_str(),0) != -1)); }

    string Decode( string inStr)

    {

    const int len = inStr.length();

    for (int i = 0;i<len;++i)

    {

    inStrIdea += (i%2) 2 : -2;

    }

    return string(inStr);

    }



  • Rogue1

    is there a conversion routine to go from TChar to string

    I am leaning towarding leaving this project setting in Unicode. I vaguely think that reasonable because I'm going to be doing directory look ups etc... although in primitives like the level I'm working in now.

    Any suggestions



  • Ingwanjawa

    I have a question.... will i have to carry the dlls meaning have them with me

    Also... I thought I did set this project for static linkage..... let me run and check....



  • Jeremy Holt

    If you want to use a standard string class, consider using std::wstring, #include <string>. It has very few features though. I usually don't bother and just work with native "C" strings, just like the Windows API.



  • Bosikov

  • Satanas

    Statically link to the CRT DLLs so you won't need them installed. Projects + Properties, Configuration properties, C/C++, Code generation, Runtime library = Multi threaded debug (/MTd). Do the same thing for the Release configuration.



  • krycek

    Instead of using a std::string, use a std::basic_string<TCHAR> (which allows std::string to work with TCHAR strings). To save on typing, you can use a macro:

    typedef basic_string<TCHAR> tstring;

    Now converting a TCHAR array to a std::tstring is as simple as:

    yourTString = TCharVar;

    PS. Now that you're statically linking, your original code works and no longer requires MSVCR80[ D ].DLL:

    TCHAR *buffer;
    std::basic_string<TCHAR> x;
    buffer = _tgetenv(TEXT("windir"));
    x = buffer;

     



  • Nick W

    A TCHAR is either a char (8 bits) or a wchar_t (16 bits). It is wchar_t when UNICODE is #defined. A TCHAR[] is a native "C" string.




  • James Farmer

    yanno,

    Thank you Oshaw.

    In this, I am constantly being reminded that in C++ it seems like there's very little actual computing that goes on. It's all conversions between fifty million datatypes.

    Since my last question, I've pretty much gone back to char dattypes.



  • Grit

    Use GetEnvironmentVariable or GetWindowsDirectory function.
  • How do I obtain an environment Variable?