我已經繼承了一箇舊的MFC/Win32 C++應用程序,它是我不應該編輯的源代碼。在舊的MFC/Win32應用程序上使用C++ interop時遇到錯誤
此MFC應用程序需要託管一箇舊的MFC/Win32 C++ DLL。此DLL還嘗試通過混合模式包裝器對託管的C++/CLI DLL進行函數調用。我知道這聽起來有點混亂,所以這裏是我的意思的示意圖:
Old MFC/Win32 Application (NO CLR)
---> Hosting old MFC/Win32 DLL (NO CLR)
---> Making function calls to Mixed-Mode wrapper (CLR)
---> Sending function calls to C++/CLI DLL (CLR)
我的問題目前是,當我嘗試裝入C++/CLR包裝類的對象,讓我們說WrapperClass WC;
,在MFC/Win32應用程序遇到「未處理的異常」。
我有一種感覺,我可能需要以某種方式託管CLR在一個單獨的過程,以便能夠使對象。這是正確的想法嗎?或者我完全不在這裏?
代碼編譯時間,這隻發生在運行時。
任何想法?
這裏是我試圖運行的代碼的一個例子:
MFC /的Win32 DLL
#include "WrapperClass.h"
BOOL Test::bTest() //This function is called elsewhere within MFC/Win32 DLL
{
DWORD dwTest;
WrapperClass WC; //Unhandled exception here!
return WC.FunctionToCall(dwTest); //FunctionToCall is a BOOL
}
混合模式封裝類
BOOL WrapperClass::FunctionToCall(DWORD dw)
{
GCHandle h = GCHandle::FromIntPtr(IntPtr(m_impl)); //m_impl def'd as void *
CPPCLIClass^ CCC = safe_cast<CPPCLIClass^>(h.Target);
return (BOOL)CCC->FunctionToCall(dw);
}
C++/CLI DLL
bool CPPCLIClass::FunctionToCall(UInt32 ui32)
{
if (ui32 == 42)
{
return true;
}
}
UPDATE: 我已經成功地哄一個真正的例外出來的方案。我現在接收System.IO.FileNotFound
異常附加信息說明:
An unhandled exception of type 'System.IO.FileNotFoundException' occured in
Unknown Module.
Additional information: Could not load file or assembly 'CPPCLIProject,
Version=1.0.4351.29839, Culture=neutral, PublicKeyToken=null' or one of its
dependencies. The system cannot find the file specified.
這是否意味着什麼?我明白,它顯然無法找到CPPCLIProject(注意:這不是包裝項目),但如果我從混合模式包裝器鏈接.lib文件,那麼我將如何不接收鏈接器錯誤?
您在C++/CLR和C++/CLI之間的區別是什麼?前者只是後者的常見拼寫錯誤,即它們是同一件事。 – ildjarn
我應該說混合模式DLL而不是C++/CLR,我現在要解決這個問題。請參閱更新的問題:) – Brutick