Is it safe to call managed function from native class method?

199 Views Asked by At

I have C++ dll with native interface (exported class), which is compiled with /clr switch. Some methods of exported class have ... parameter. Compiler shows C4793 warning on this class: function is compiled as native code.

// ExportedClass.h
class LIBRARY_API ExportedClass
{
public:
    void Method(const char* format, ...);
};

// ExportedClass.cpp

void ManagedFunction()
{
    String^ s = gcnew String(L"");    // OK
}

void ExportedClass::Method(const char* format, ...)
{
    // Not allowed: the function is native
    // String^ s = gcnew String(L"");

    // Call ManagedFunction instead:
    ManagedFunction();
}

Since the class is compiled as native, I cannot use managed code directly in the class methods. However, I can call managed functions, as shown in the code sample. Is it safe to do this?

0

There are 0 best solutions below