簡體   English   中英

C ++ DLL返回指向std :: list的指針<std::wstring>

[英]C++ DLL returning pointer to std::list<std::wstring>

我有以下原型的dll:

DLL_EXPORT std::list<std::wstring>* c_ExplodeWStringToList(std::wstring in_delimiter, std::wstring in_string, int in_limit);

該應用程序使用這樣的方式:

std::list<std::wstring>* exploded = mydllclass->c_ExplodeWStringToList(L" ", in_command.c_str(), 0);

在XP 32下,此方法效果很好,但是當我在自己的Vista 64上在家中嘗試該程序時,程序將自行關閉。 沒有錯誤也沒有警告?

幾天前,DLL直接返回了列表-沒有指針。 但是我切換到了VC ++ 2010 Express,並且沒有進行此修改就無法編譯DLL。

我在這里沒看到什么嗎?

謝謝 :)

更新:

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;
}

Ť

我沒有深入研究代碼,但是得出的關於使用DLL的結論是,除了DLL函數的原始類型外,不返回任何內容。 這是因為由於不同的編譯器或不同的開關或項目設置,結構和類在DLL和調用DLL的代碼中沒有相同的對齊方式而沒有相同的大小。

因此,從DLL返回列表可能在調用者應用程序中被認為是錯誤的。

對於從DLL引發異常,同樣的事情-捕獲的代碼可能會誤解所引發的類。

因此,最好僅導出返回原始類型(表示錯誤代碼)的C函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM