1

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?

Alex F
  • 42,307
  • 41
  • 144
  • 212
  • You are playing a pretty dangerous game, the CLR needs to be loaded and initialized before that managed function can execute. You probably got that by accident now, the C++/CLI compiler will create a stub if necessary to get that done. That stub is *not* cheap. You also have no control whatsoever over what version of the CLR you get. Using COM or hosting the CLR yourself is the safe version. – Hans Passant Dec 11 '13 at 15:58
  • 1
    @HansPassant: This library is compiled with /cli, and referenced from the client through linker dependencies list. AFAIK, this means, that CLR is loaded when the process starts. Is this correct? – Alex F Dec 11 '13 at 16:01

0 Answers0