簡體   English   中英

將std :: string作為參數從一個DLL傳遞到另一個DLL會引發訪問沖突錯誤

[英]Passing std::string as parameter from one DLL to another DLL throwing access violation error

該應用程序具有許多C ++編譯的DLL,每個DLL都公開許多C類型的接口。 該應用程序具有一些std :: string類型的配置變量,這些變量需要在DLL接口邏輯中使用。 在將此std :: string類型參數傳遞給這些DLL時,拋出了“ 0xC0000005:訪問沖突執行位置”。 這與DLL項目的VS項目設置有關嗎? 請澄清。

您可能不會使它們輕松工作。

std::string在不同庫的不同編譯之間可能不兼容。

當您說“應用程序具有許多C ++編譯的DLL”時,很可能您處於這種情況:

庫A:

// STL
class std::string
{
    ... under the hood implementation of std::string (version A)
};

// Library code
std::string someFunctionInA();

圖書館B:

// STL
class std::string
{
    ... under the hood implementation of std::string (version B)
};

// Library code
void someFunctionInB(const std::string& myString);

崩潰程序:

std::string stringFromA = someFunctionInA();
someFunctionInB(stringFromA);

得到它了? 您有2個版本的std::string並進行了程序編譯,因為在程序編譯期間使用的是相同的標頭...但是在運行時,它們期望使用2種不同的類型。

對象的大小,數據的順序以及分配器可能不匹配...它將崩潰!

解決方法:

  • 如果可以從源代碼編譯,請確保使用相同的STL版本。
  • 如果您不能從源代碼進行編譯,請為它們創建C包裝程序並使用C-String作為接口...它們將始終有效。

暫無
暫無

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

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