I am trying to access a managed function from native code. I created a c++/cli dll in visual studio 2010 with CLR option. This is my c# code:
namespace ManagedNamespace { public class Managed { public void CSharpFunc() { . . . } } } This is my c++\CLI code (the project refer to the c# project - and I compile this code to 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(); } I call this function from another dll and get EXCEPTION_EXECUTE_HANDLER when I create ManagedNamespace::Managed object. This is the code:
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); } } Appreciate any help, Thanks