c++classdll.lib

Export DLL string from library class function


I'm trying to export a string from my DLL, the string is from the library included in my DLL. My DLL includes the CMMCore.lib library, and there is a CMMCore class have a getDeviceAdapterNames() function. This function returns a string.

My c++ code to libary(.lib) is :

#include "PluginManager.h"
...
std::vector<std::string> CMMCore::getDeviceAdapterNames() throw (CMMError)
{
      return pluginManager_->GetAvailableDeviceAdapters();
}
...

It call another function from PluginManager.cpp:

CPluginManager::GetAvailableDeviceAdapters()
{
   std::vector<std::string> searchPaths = GetActualSearchPaths();

   std::vector<std::string> modules;

   for (std::vector<std::string>::const_iterator it = searchPaths.begin(), end =     searchPaths.end(); it != end; ++it)
      GetModules(modules, it->c_str());

   std::set<std::string> moduleSet;
   for (std::vector<std::string>::const_iterator it = modules.begin(), end =     modules.end(); it != end; ++it) {
      if (moduleSet.count(*it)) {
         std::string msg("Duplicate libraries found with name \"" + *it + "\"");
         throw CMMError(msg.c_str(), DEVICE_DUPLICATE_LIBRARY);
      }
   }
   return modules;
}

And my DLL Code is:

#include <MMCore.h>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <vector>
using namespace std;
EXPORT void getDevice_dll(char* input_string) 
{   
    CMMCore * v = new CMMCore;
    v->CMMCore::getDeviceAdapterNames();
    memcpy(input_string, v, 20);
}

I want to put the string from CMMCore::getDeviceAdapterNames to *(input_string), but it doesn't work.

Is there any way to put the string from CMMCore::getDeviceAdapterNames to the pointer or variable value named by myself?


Solution

  • As CMMCore::getDeviceAdapterNames return a vector of string, the code should be:

        //max_input_string_size <- size of the input buffer.
        //return the number of device found and saved in input_string, -1 if error.
        EXPORT int getDevice_dll(char* input_string, int max_input_string_size )
        {
                std::vector<std::string> modules;
                try {
                        //if getDeviceAdapterNames was declared as a static member function
                        modules = CMMCore::getDeviceAdapterNames();
    
                        //if non static member function:
                        modules = CMMCore().getDeviceAdapterNames();
    
                }catch( CMMError &e ){
                        //handle the error here
                        return -1; //error DEVICE_DUPLICATE_LIBRARY
                }
    
                string strOutput;
    
                //change this if your application use other separator(e.g : "\r\n" or "," or "|")
                string strSeparator(";");
                for(int i = 0; i < modules.size(); i++ ){
                        strOutput += modules[i] + strSeparator;
                }
                strncpy( input_string, strOutput.c_str(), max_input_string_size );
                return modules.size();
    
        }