Q:

Converting form CString to const char*

I've wrote a method that convert form CString to const char *:
const char* CAESDlg::ConvertToChar(CString str) {
    CStringA charstr(str);
    const char *cstr = (const char *)charstr;
    return cstr;
}

but when trying to put it in a const char * it doesn't return the correct value!
const char *test = ConvertToChar(filePath);

A:

The value of charstr gets destroyed at the end of the function before the caller assigns it to variable.
You don't need a function, the caller can use CStringA directly and note that test is valid before sFilePathA goes out of scope.
CStringA sFilePathA(filePath);
const char *test = sFilePathA;

