从库 class 函数中导出 DLL 字符串
Export DLL string from library class function
我正在尝试从我的 DLL 中导出一个字符串,该字符串来自我的 DLL 中包含的库。我的 DLL 包含 CMMCore.lib 库,并且有一个 CMMCore class 具有 getDeviceAdapterNames()
函数。这个函数returns一个字符串。
我的 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
的字符串放到自己命名的指针或变量值中?
作为CMMCore::getDeviceAdapterNames
return一个字符串向量,代码应该是:
//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();
}
我正在尝试从我的 DLL 中导出一个字符串,该字符串来自我的 DLL 中包含的库。我的 DLL 包含 CMMCore.lib 库,并且有一个 CMMCore class 具有 getDeviceAdapterNames()
函数。这个函数returns一个字符串。
我的 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
的字符串放到自己命名的指针或变量值中?
作为CMMCore::getDeviceAdapterNames
return一个字符串向量,代码应该是:
//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();
}