I hope the title was good enough to help explain what I am having problems with. I think once I solve this problem my project will be pretty much finished. Just a note, both projects are compiled under Unicode.
I am working with a CLI/C++ DLL that takes in a LPCTSTR and returns a const char*. If I store the value of the return in a const char* in my project while stepping through I can see the value its returning is what I expect to be returned.
Now if I do the following:
LPCTSTR strValue = L"test";
const char* Return = MethodCall(strValue);
LPCTSTR Final = CString(Return);
Return will equal “Xmkk=Asmks” (which is what it should). This method encrypts a string. The problem is when I do CString, Final will equal “ﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮ㹙癞鞮᠀諸²⤐²”. How do I turn the onst char* into a LPCTSTR without changing its data”
Thank you.
After CString(Return) is destructed (this happens “right on next line after its construction”) “Final” pointer is pointing to dealocated chunk of memory (which was internal CString(Return) buffer). At this point contents of memory to which it points is undefined and dereferencing it is undefined behaviour.
To use pointer to internal buffer safelly you should ensure that CString which owns buffer is alive as long as pointer is.