从DLL中返回std :: wstring是否安全? [英] Is it safe to return std::wstring from a DLL?

查看:442
本文介绍了从DLL中返回std :: wstring是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据一些较旧的StackOverflow问题(无法传递std :: wstring跨DLL C ++ DLL将指针返回到std :: list< std :: wstring> ),认为C ++ DLL返回 std :: wstring 是不安全的,因为不能保证主程序具有相同的 std :: wstring 的定义,因此可能导致崩溃。

According to some older StackOverflow questions ( Unable to pass std::wstring across DLL , C++ DLL returning pointer to std::list<std::wstring> ) it's not considered safe for a C++ DLL to return a std::wstring because there's no guarantee the main program has the same definition of std::wstring and therefore it might cause a crash.

但是,在 http://en.cppreference.com/w/cpp/string/basic_string ,看起来 std :: wstring 可以与 WCHAR 数组互换使用:

However, in http://en.cppreference.com/w/cpp/string/basic_string , it seems std::wstring can be used interchangeably with a WCHAR array now:


(从C ++ 11开始)
basic_string的元素是连续存储的,即对于basic_string s,& *(s.begin()+ n)对于[0,s.size()]中的任何n,==& * s.begin()+ n,或者等价地,指向s [0]的指针可以传递给期望指向第一个元素的CharT []数组。

(Since C++11) The elements of a basic_string are stored contiguously, that is, for a basic_string s, &*(s.begin() + n) == &*s.begin() + n for any n in [0, s.size()), or, equivalently, a pointer to s[0] can be passed to functions that expect a pointer to the first element of a CharT[] array.



我已经通过传递& s [0] / code>到一个WINAPI函数,期望一个 WCHAR * 缓冲区,它似乎工作( std :: wstring 正确填充WINAPI的结果)。因此,由于 std :: wstring 现在看起来像一个 WCHAR 数组,我决定重新访问这个问题:can从DLL中安全地返回 std :: wstring ?为什么或为什么不?

I've tested this by passing &s[0] to a WINAPI function that expected a WCHAR* buffer and it appeared to work (the std::wstring was correctly populated with the results of the WINAPI). So since std::wstring can apparently be treated like a WCHAR array now, I decided to revisit this question: can a std::wstring be safely returned from a DLL? Why or why not?

推荐答案

关于跨DLL边界传递C ++对象没有什么变化。这仍然不允许有同样的原因。边界另一侧的模块可能有不同的类定义。

Nothing has changed with regards passing C++ objects across DLL boundaries. That is still not allowed for the same reason as before. The module on the other side of the boundary may have a different definition of the class.

事实上& s [0] code>是一个有效的可修改的指针,字符数组不是真正相关的。因为 std :: basic_string 不仅仅是一个字符数组。

The fact that &s[0] is a valid modifiable pointer to character array is not really relevant. Because a std::basic_string is a lot more than just an array of characters.

请记住, std :: basic_string 的每个实现都可以有不同的内部存储。对于 operator [] 可以有不同的实现。可以分配不同的堆。等等。

Remember that each implementation of std::basic_string can have different internal storage. Can have a different implementation for operator[]. Can be allocated off a different heap. And so on.

我认为它是安全的,假定它永远不会有效的传递C ++对象跨越一般的DLL边界。它只有在你保证边界的两边都链接到同一个运行时实例时才可行。

I think it is safe to assume that it will never be valid to pass C++ objects across general DLL boundaries. It is only viable if you guarantee that both sides of the boundary are linked against the same runtime instance.

这篇关于从DLL中返回std :: wstring是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆