我有一個調用c/C++接口DLL的C#表單,這個接口調用了一個win32本機C++ dll。最初這是用VS2010編寫的,並且工作正常 - 我能夠將System :: String編碼爲std :: string,並將其傳遞給本機dll,並輸出值。然後,我將C#和C++/cli項目轉換爲VS2012以啓用智能感知。這需要安裝Service Pack才能在VS2010中重新啓用4.0 .NET框架。我重建了2010年的Win32 dll,VS2012中的C#應用程序和C++/cli dll,現在我在調用dll時收到一個錯誤:將std :: string從C++/cli dll傳遞給本機win32 dll時調試斷言失敗錯誤
Debug Assertion Failed!
計劃: ... 文件:F:\ DD \ vctools \ crt_bld \ self_x86 \ CRT \ SRC \ dbgheap.c 行:1424
表達:_pFirstBlock == PHEAD
public ref class ManagedWrapper
{
CSampleWin32Library* m_pUnmanagedWrapper;
public:
ManagedWrapper() {m_pUnmanagedWrapper = new CSampleWin32Library();}
~ManagedWrapper() {delete m_pUnmanagedWrapper;}
//Test call to prove integration
void Test(int x, System::String^ testString) {
//marshaling example: http://msdn.microsoft.com/en-us/library/bb384865.aspx
std::string tmpStdString = marshal_as<std::string>(testString);
m_pGambitUnmanagedWrapper->Test(x, tmpStdString); //ERROR OCCURS HERE
};
};
希望這很容易,因爲有些設置丟失了,或者現在需要在VS2012中。就我所知,我沒有更改任何代碼。
跨DLL邊界傳遞C++對象是很麻煩的。你會被提醒,你的C++/CLI DLL不使用與C++ DLL相同的CRT版本。您將不得不重建C++ DLL,以便使用CRT的VS2012版本,請確保/ MD有效。進一步通過引用而不是按值傳遞std :: string來改進Test()。 – 2013-03-20 14:16:03
感謝漢斯。你介意提交這個作爲答案,所以我可以接受嗎? – reuscam 2013-03-20 14:34:40
這只是一個猜測,在問題中幾乎沒有提示這是問題的真正原因。您最好發佈自己的答案,並準確描述您找到的內容。 – 2013-03-20 15:29:53