如何在 DLL 中返回向量?

发布于 2024-10-23 19:18:31 字数 430 浏览 2 评论 0原文

我为我的应用程序开发了一个插件系统。 我的软件是用 MFC/c++ 编写的,

我选择从 DLL 导入函数。 但我面临一个问题。 我已经使用 std 库进行了编写,但是 SO 向我表明“如果基于模板,则将任何内容传入和传出 DLL 时都会存在危险”,

所以我开始重写它。但现在我找不到好的解决方案。 这个 dll 将解析 Youtube 并以如下结构返回结果:

struct VideoInfo{
wchar_t* strName;
wchar_t* strURL;
int iDuration;
float fRate;
};

但将是 50 个,在我之前的代码中,我只是返回了一个 std::vector。 *vecVideos; 但我怎样才能以安全的方式做到这一点?

I developing a plugin system to my Application.
My software is written in MFC/c++

I Choose import the functions from a DLL.
But i facing a problem.
I had written using the std library, but SO showed my that "There is a danger when passing anything into and out of a DLL if it's based on a template"

So i started to rewrite it. But now i cannot found a good solution.
This dll will parse the Youtube and return the results in a struct like this:

struct VideoInfo{
wchar_t* strName;
wchar_t* strURL;
int iDuration;
float fRate;
};

But will be 50 of those, in my previous code i just returned a std::vector<VideoInfo*> *vecVideos; but how can i do that in a safe way ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尸血腥色 2024-10-30 19:18:31

您可以创建一个链接列表并返回它,而不是返回向量。

无需担心不兼容,而且使用起来很简单:

struct Node
{
    VideoInfo vi;
    Node *Next;
};

当您构建视频列表时,只需执行以下操作:

Node *ptr = new Node();
ptr->vi = MyVideoInfo; // Assumes MyVideoInfo is the new video
ptr->Next = current;
current = ptr; // Assuming current is our current linked list

您甚至可以在 c 中使用它,因为您只需将其包装在 extern "C " {} 并将 typedef 添加到节点声明的前面。

Instead of returning a vector, you can create a linked list and return that.

No need to worry about incompatibility, and it's simple to use:

struct Node
{
    VideoInfo vi;
    Node *Next;
};

When you're building your list of videos, just do the following:

Node *ptr = new Node();
ptr->vi = MyVideoInfo; // Assumes MyVideoInfo is the new video
ptr->Next = current;
current = ptr; // Assuming current is our current linked list

You could even use this in c, since you'd just need to wrap it in an extern "C" {} and add typedef to the front of your declaration of the node.

绳情 2024-10-30 19:18:31

仅当 DLL 和 EXE 不是使用相同的项目配置构建、使用 /MT 而不是 /MD 构建或使用不同版本的运行时库构建时,返回 C++ 对象才是不安全的。在很多情况下这不是一个实际问题,ymmv。

安全的方法是避免要求 EXE 释放 DLL 从堆中分配的内存。或者反过来。首先,您需要更改结构,不能有 wchar_t*。您可以编写导出的函数来获取 VideoInfo*,以便调用者可以传递该函数填充的缓冲区。

但重申一下,只要 DLL 不存在过时的危险,那么您就不会有麻烦与 std::vector 或 std::wstring 。

It is only unsafe to return C++ objects when the DLL and the EXE were not built with the same project configuration, built with /MT instead of /MD or built with different versions of the runtime library. In many cases this is not a practical problem, ymmv.

The safe way is to avoid requiring the EXE to release memory that was allocated from the heap by the DLL. Or the other way around. You'll need to change your structure for starters, can't have wchar_t*. You'd write your exported function to take a VideoInfo* so that the caller can pass a buffer that the function fills in.

But reiterating, as long as there's no danger that the DLL is going to get obsolete then you won't have trouble with std::vector nor std::wstring.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文