C ++ DLL returns a pointer to std :: list <std :: wstring>
I have a dll with the following prototype:
DLL_EXPORT std::list<std::wstring>* c_ExplodeWStringToList(std::wstring in_delimiter, std::wstring in_string, int in_limit); The application uses this as follows:
std::list<std::wstring>* exploded = mydllclass->c_ExplodeWStringToList(L" ", in_command.c_str(), 0); This works fine under XP 32, but when I try to do this at home with my Vista 64, my program just closes. No errors and warnings?
A few days ago, the DLL returned the list directly - without a pointer. But I switched to VC ++ 2010 Express, and I could not compile my DLL without this modification.
What am I not seeing here?
Thank:)
Update:
DLL_EXPORT std::list<std::wstring>* c_ExplodeWStringToList(std::wstring in_delimiter, std::wstring in_string, int in_limit) { std::list<std::wstring>* returnlist = new std::list<std::wstring>(); std::list<std::wstring>* stringlist = new std::list<std::wstring>(); UINT pos = 0; while(true) { pos = in_string.find(in_delimiter, 0); if(pos == std::string::npos) { stringlist->push_back(in_string.substr(0, pos)); break; } else { stringlist->push_back(in_string.substr(0, pos)); in_string = in_string.substr(pos + in_delimiter.length()); } } // **** // Here is missing some code I've commented out while searching for the error. // **** returnlist = stringlist; return returnlist; } T
I did not dig the code, but the output that I came to work with the DLL is to return nothing but primitive types from DLL functions. This is due to the fact that due to different compilers or different switches or project parameters, the structures and classes are not aligned the same, but do not have the same size in the DLL and in the code that calls the DLL.
Therefore, returning a list from a DLL may be considered invalid in the calling application.
The same goes for throwing exceptions from a DLL - a class throw may be misinterpreted by catch code.
So, it is best to export only C functions that return primitive types (to indicate error codes).