2013-07-17 15 views
-1

我試圖從本機代碼訪問託管函數。 我使用CLR選項在visual studio 2010中創建了一個C++/cli dll。 這是我的C#代碼:未能從CLI代碼創建c#對象

namespace ManagedNamespace 
{ 
    public class Managed 
    { 
     public void CSharpFunc() 
     { 
      . 
      . 
      . 
     } 
    } 
} 

這是我的C++ \ CLI代碼(該項目指的是C#項目 - 我編譯此代碼Wrapper.dll):

#ifdef __cplusplus 
extern "C" 
{ 
#endif 
    __declspec(dllexport) void CLIFunc(const std::string& a, const std::string& b, std::string& c) 
    { 
     System::String^ new_a = gcnew System::String(a.c_str()); //OK 
     System::String^ new_b = gcnew System::String(b.c_str()); //OK 
     Text::StringBuilder^ new_c = gcnew Text::StringBuilder(""); //OK 
     ManagedNamespace::Managed^ m = gcnew ManagedNamespace::Managed(); //this line is the problematic line 
     m->CSharpFunc(); 
    } 

我打電話這個函數從另一個dll獲取EXCEPTION_EXECUTE_HANDLER當我創建ManagedNamespace :: Managed對象。這是代碼:

typedef void (WINAPI *CLIFuncPtr)(const std::string& a, const std::string& b, std::string& c); 
. 
. 
. 
HMODULE mod = LoadLibraryA("c:\\mydir\\Wrapper.dll"); 
if (mod!= NULL) 
{ 
    CLIFuncPtr FuncPtr = (CLIFuncPtr)GetProcAddress(mod, "CLIFunc"); 
    if (FuncPtr != NULL) 
    { 
     FuncPtr (aa, bb, cc); 
    } 
} 

得到任何幫助, 感謝

+0

什麼是確切的錯誤信息? –

+0

這與此有關嗎? http://stackoverflow.com/questions/6628412/c-cli-catching-all-net-win32-crt-exceptions你可以讓調試器打破所有的異常,看看出了什麼問題嗎? – doctorlove

+0

異常:內存位置0x處的EEFileLoadException * _ptr64 .... – pitz

回答

0

我認爲調用LoadLibrary僅適用於非託管代碼