Heres a theoretical question for you assembly experts working in C++. I am often wanting to return a string (char * or wchar *) from a local variable within a function. As you know this cannot be done, as the local variable loses scope. This gets me curious, would it be possible to push the data stored at this location from within the function?
Let me illustrate:
char *IllegalFunc()
{
char *ret = "Hello World";
return ret; //warning: returning the adress of a local variable
}
//what we are attempting
char *IllegalAndMostlyIllogicalFunc()
{
char *ret = "Hello World";
__asm {
//somehow copy *ret to ESP and return
}
}
char *ARightWayToDoIt()
{
char *ret = new char[12];
strcpy(ret, "Hello World");
return ret;
}
This is just for curiousity, I wouldn't acctually use __asm method... I will probably just declare a static char * so i can import the function like so in c#
[dllimport(...)]
string IllegalFunc();
[dllimport(...)]
string ARightWayToDoIt(); //memory leak, I would need a FreeLastString() method
ESP?