从库类函数导出 DLL 字符串

Export DLL string from library class function

本文关键字:DLL 字符串 类函数      更新时间:2023-10-16

我正在尝试从我的DLL导出一个字符串,该字符串来自我的DLL中包含的库。我的DLL包括CMMCore.lib库,还有一个CMMCore类有一个getDeviceAdapterNames()函数。此函数返回一个字符串。

我的 c++ 代码到库 (.lib) 是:

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

它从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;
}

我的DLL代码是:

#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);
}

我想将 CMMCore::getDeviceAdapterNames 中的字符串放入 *(input_string),但它不起作用。

有没有办法将字符串从CMMCore::getDeviceAdapterNames放到我自己命名的指针或变量值?

CMMCore::getDeviceAdapterNames返回字符串向量时,代码应为:

    //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 : "rn" 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();
    }