从库类函数导出 DLL 字符串
Export DLL string from library class function
我正在尝试从我的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();
}
相关文章:
- Visual c ++,使用字符串引用/指针调用 dll 函数
- 从DLL导出函数,LoadLibrary()需要用TEXT转换的字符串才能编译而不会出错
- 如何将 VB.Net 字符串作为字符*传递给C++dll?
- C# 和 C++ dll - 封送字符串
- 为什么在调用C#DLL时不需要提供字符串缓冲区
- 随机字符,从强制转换 Arduino 字符串到 Excel BSTR 通过 c++ DLL
- Java 调用 dll 字符串返回类型方法
- 如何在 VBA/Excel 中创建字符串数组并将其发送到C++ DLL,以便可以在 DLL 中循环访问它
- 将字符串向量从 C# 传递到C++ DLL
- 使用 DLL 在 c++ 和 C# 之间交换字符串
- 为什么 C# 封送字符串不适用于C++ DLL
- C++中具有"::"字符串名称的 DLL 导出调用
- 放置在外部 DLL 中的类中的字符串数据的内存损坏
- 从 dll 返回多个字符串
- 将字符串从 C# 发送到 dll 文件C++
- 从dll传递c字符串,穿过外部C
- 从库类函数导出 DLL 字符串
- 从 DLL 字符串表中获取字符串
- C# DLL 字符串 C++ 中的函数
- c++ Dll字符串函数崩溃