无法从CLI代码创建c#对象

时间:2013-07-17 08:38:37

标签: c++-cli

我正在尝试从本机代码访问托管函数。 我在带有CLR选项的visual studio 2010中创建了一个c ++ / cli dll。 这是我的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调用此函数,并在创建ManagedNamespace :: Managed对象时获取EXCEPTION_EXECUTE_HANDLER。这是代码:

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

感谢任何帮助, 感谢

1 个答案:

答案 0 :(得分:0)

我认为LoadLibrary仅适用于非托管代码