我正在尝试从DLL中导出字符串,该字符串来自DLL中包含的库。我的DLL包含CMMCore.lib库,并且CMMCore类具有getDeviceAdapterNames()
函数。该函数返回一个字符串。
我到libary(.lib)的C ++代码是:
#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
中的字符串放置到我自己命名的指针或变量值上?
答案 0 :(得分:-1)
当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 : "\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();
}