0

I have written the following function which it is going to print out the value of an environment variable, but when I compile it, it shows me the following error:

 error C2760 : syntax error : unexpected token 'new', expected ';'
warning C4244: 'initializing': conversion from 'DWORD' to 'TCHAR', possible loss of data

The code I have written:

template<typename T>
void EnvironmentVariableParser(T arg_variable_name) 
{
    PTSTR pszTemp, pszValue = NULL;
    auto dwResult = GetEnvironmentVariable(arg_variable_name, pszValue, 0);

    if (dwResult != 0) 
    {
        auto size = dwResult * sizeof(TCHAR);
        pszTemp = new PTSTR(size);
        pszValue = reinterpret_cast<PTSTR>(pszTemp);
        GetEnvironmentVariable(arg_variable_name, pszValue, size);
        std::cout << arg_variable_name << "=" << pszValue << std::endl;
        delete pszValue;
    } 
    else 
    {
        std::cout << arg_variable_name << "=<unknown value>" << std::endl;
    }

}

is that possible to cast new operator output to something like PTSTR?

  • 3
    The correct syntax is `pszValue = new TCHAR[size];`, also `free(pszValue);` is invalid as well because it does not match allocation. – user7860670 Aug 29 '19 at 08:41
  • 1
    You can't replace `malloc(x)` with `new x`. There is a list of good books [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Aug 29 '19 at 08:52
  • 2
    OT: By _operator new_ you likely mean [_new-expression_](http://eel.is/c++draft/expr.new). Just not to be confused with [`operator new`](http://eel.is/c++draft/basic.stc.dynamic). – Daniel Langr Aug 29 '19 at 08:53
  • 2
    I personally recommend not using Hungarian notation. It's imposing more problems than it solves... – Aconcagua Aug 29 '19 at 09:06
  • 1
    @Adrian Exactly. I never saw an example where they actually were superior to `TCHAR*` and so on. Their corresponding base types (`TCHAR` itself) is another topic. Hungarian notation (prefixes like `psz`, `i`, ...), not Hungarian names. About the latter, though: Same as with any other non-English names: You *will* get into the situation of having to share your code with others not speaking that language some day (and if only here on SO), any non-English identifier will make it much harder to understand the intention behind the code for these... – Aconcagua Aug 29 '19 at 09:16
  • @Adrian `leistung = spannung / stromStaerke;` – unless you share my mother tongue, can you tell where the bug is? And no, you didn't want to consult an online translator for every newly discovered variable... – Aconcagua Aug 29 '19 at 09:19
  • @Adrian And why should I want to use these? The C++ casts are superior anyway. If C, I still can fallback to normal casts (again, I'd prefer them anyway; and exactly same characters and number of, just different order). And you fell into a trap yourself: `PTSTR* pszMagyar` – did you notice that you placed an extra asterisk? About typedefs, you're right (just looked them up), but that doesn't change anything on the actual problem with: It's just information hiding for no true benefit, be it a `typedef` or a `#define`... – Aconcagua Aug 29 '19 at 09:25

1 Answers1

4

The comments given are sort of answers, but imprecise. The code fixes you need (with comments) are:

auto size = dwResult * sizeof(TCHAR); // Maybe not needed (see notes).
pszValue = new TCHAR[dwResult];       // NOTE: Not 'size'!!!
GetEnvironmentVariable(arg_variable_name, pszValue, size);
std::cout << arg_variable_name << "=" << pszValue << std::endl;
delete[] psvValue;                    // Don't use: free(pszValue);

I don't know what the 3rd argument to the GetEnvironmentVariable function is: is it the actual size of the buffer (in bytes) or the max. number of TCHARS? If the latter, you give it dwResult and never need size.

Hope this helps!

EDIT: Just checked: The 3rd argument is the TCHAR count, so you can completely dispense with size !!

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83