A specific line of code coming out with code 255 after changing the code generation

Well, I tried to compile a small testing application that I am working on.

So keep things short and simple:

When I set my code generation from “Multi Threaded DLL” to “Multi Threaded” to get rid of some dependencies, the next line of code crashes my application (where it usually works without any flaws)

A crash occurs when I want to convert a short path to a long path. as such:

LPCSTR tmp = reinterpret_cast<LPCSTR>(getenv("Temp"));
GetLongPathNameA(tmp,tempFolder,MAX_PATH);

The accident occurs in the first line:

LPCSTR tmp = reinterpret_cast<LPCSTR>(getenv("Temp"));

So, are there any ideas why it suddenly stops working when you switch the code generation mode? Thank!

EDIT:

After some code recoding, I managed to find out that this is crashing when executed

getenv("Temp");

,

+3
5

, ( ) , , . , undefined. , (MFC ..). , . .Net, .

+1

getenv():

LPCSTR tmp = getenv("Temp");
if(tmp != NULL)
    // do something with tmp

, , NULL- .

Microsoft getenv_s() , MSDN :

char *tmp;
size_t requiredSize;

getenv_s(&requiredSize, NULL, 0, "Temp");
tmp = (char *) malloc(requiredSize * sizeof(char));
if (tmp != NULL)
{
    getenv_s(&requiredSize, tmp, requiredSize, "Temp");
    if(tmp != NULL)
        // do something with tmp

    free(tmp);
}

WinAPI GetEnvironmentVariable() , ( GetLastError(), ), ( , ).

+1

. , , , .ncb, .

0

libcmt.lib libcpmt.lib ? , . .

0

reinterpret_cast, , . static_cast .

0

Source: https://habr.com/ru/post/1775968/


All Articles