1

I tried to create a function which is returning a vector in C++. But when it built to dll, the function name seems to be mangled.

I tried to use the extern C but the problem is return type vector cannot support if I use extern C

Error : C Linkage function cannot return C++ class std:: Vector

Below is the code I am using

class __declspec(dllexport) TestClass
        {
        public:
            string sClassName;
            string sName;
            string sDescription;


        };


extern "C"
{
    vector<TestClass> __declspec(dllexport) GetInfoList();
}
underscore_d
  • 6,309
  • 3
  • 38
  • 64
Hari Sankar v m
  • 163
  • 1
  • 12

2 Answers2

2

We see it is not problem with easy solution. Here's an involved solution. Use a non-mangled name (extern "C") with GetProcAddress to access a special function in the higher level DLL. . Define all the functions you want to call, as virtuals, in an abstract base class. Call that function above to retrieve the pointer P to a lazily instantiated concrete class with virtuals overriden. Invoke desired method through the pointer P.

WilliamClements
  • 350
  • 3
  • 8
1

vector is C++ type and so cannot be directly given under extern "C". Give the function inside a class and export the class as C Type.

extern "C"
{
    class __declspec(dllexport) ClassToExport
    {
        vector<TestClass> GetInfoList();
    };
}

Now use this class object to axcess the function.

Anand Paul
  • 224
  • 2
  • 10