1

I'm currently coding a MFC DLL with only exported function (no class) and usually I'm using the format

extern "C" void EXPORT_DLL function_name(parameters)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    // do something here
}

but now I need to export an array of data. A list of usernames and an IDs (from MongoDB).

Is it possible to export a function that returns a vector of pair like so or will it break because it's not an exportable type?

extern "C" vector<pair<std::string, std::string>> EXPORT_DLL function_name(parameters)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    // do something here
    return a_vector_of_pair;
}

If it's not possible, what are the other options?

Thanks.

user3842408
  • 89
  • 1
  • 10
  • And how exactly you declare a vector in `extern "C"` function? vector means c++. you should try to do it in c++. – SHR Aug 19 '14 at 16:32
  • Yeah that's what i thought, I'm gonna try to pass it as a parameter as a pointer. `extern "C" void EXPORT_DLL function_name(vector>* param)` – user3842408 Aug 19 '14 at 16:42
  • Don't pass vector through DLL boundaries. The reason is explained very well here:http://stackoverflow.com/questions/5661738/how-can-i-use-standard-library-stl-classes-in-my-dll-interface-or-abi/5664491#5664491 – Matt Aug 19 '14 at 17:16
  • But I'm using it in a MFC DLL, so i stay in Visual Studio, C++, in the same compiler. Doesn't seem to apply to my situation, does it? – user3842408 Aug 19 '14 at 17:49

1 Answers1

0

You can put the functions without the extern "C".

It means you can use this function only from c++, not from c. (you may use other extern "C" functions from the DLL from c)

Anyway there is no use of the vector in c...

Put the following code in your dll header:

//header
typedef std::pair<std::string,std::string> pair_strs;
typedef std::vector<pair_strs> vec_pair_strs;
vec_pair_strs DLL_EXPORT SomeFunction();

Put the following code in your dll source:

//cpp:
vec_pair_strs DLL_EXPORT SomeFunction()
{
    vec_pair_strs v;
    //...
    return v;
}
SHR
  • 7,940
  • 9
  • 38
  • 57
  • Note that if the interface contains STL types, you can use the DLL only from code compiled with the same compiler version as the DLL, since each compiler version has different STL which are not compatible with other. – SHR Aug 19 '14 at 18:05