C++ Passing std::string by reference to function in dll(C++ 通过引用 dll 中的函数传递 std::string)
问题描述
我在将引用 std::string 传递给 dll 中的函数时遇到问题.
I have the problem with passing by reference std::string to function in dll.
这是函数调用:
CAFC AFCArchive;
std::string sSSS = std::string("data\gtasa.afc");
AFCER_PRINT_RET(AFCArchive.OpenArchive(sSSS.c_str()));
//AFCER_PRINT_RET(AFCArchive.OpenArchive(sSSS));
//AFCER_PRINT_RET(AFCArchive.OpenArchive("data\gtasa.afc"));
这是函数头:
#define AFCLIBDLL_API __declspec(dllimport)
AFCLIBDLL_API EAFCErrors CAFC::OpenArchive(std::string const &_sFileName);
我尝试通过调用函数并查看函数内部的 _sFileName
值来逐步调试.
I try to debug pass-by-step through calling the function and look at _sFileName
value inside function.
_sFileName
在函数中设置任意值(例如,t4gs..
).
_sFileName
in function sets any value(for example, t4gs..
).
我尝试检测任何堆损坏,但编译器说没有错误.
I try to detect any heap corruption, but compiler says, that there is no error.
DLL 已在调试设置中编译..exe 程序也是在调试中编译的.
DLL has been compiled in debug settings. .exe programm compiled in debug too.
怎么了??帮助...!
What's wrong?? Help..!
附言我使用的是 Visual Studio 2013.WinApp.
P.S. I used Visual Studio 2013. WinApp.
编辑
我已将 func 的标题更改为此代码:
I have change header of func to this code:
AFCLIBDLL_API EAFCErrors CAFC::CreateArchive(char const *const _pArchiveName)
{
std::string _sArchiveName(_pArchiveName);
...
我真的不知道,如何修复这个错误...
I really don't know, how to fix this bug...
关于堆:它分配在我们进程的虚拟内存中,对吗?在这种情况下,共享虚拟内存很常见.
About heap: it is allocated in virtual memory of our process, right? In this case, shared virtual memory is common.
推荐答案
该问题与 STL 无关,而与跨应用程序边界传递对象有关.
The issue has little to do with STL, and everything to do with passing objects across application boundaries.
1) DLL 和 EXE 必须使用相同的项目设置进行编译.你必须这样做,这样结构体的对齐和打包是相同的,成员和成员函数没有不同的行为,甚至更微妙的是,一个引用和引用参数的低级实现是完全一样的.
1) The DLL and the EXE must be compiled with the same project settings. You must do this so that the struct alignment and packing are the same, the members and member functions do not have different behavior, and even more subtle, the low-level implementation of a reference and reference parameters is exactly the same.
2) DLL 和 EXE 必须使用相同的运行时堆.为此,您必须使用运行时库的 DLL 版本.
2) The DLL and the EXE must use the same runtime heap. To do this, you must use the DLL version of the runtime library.
如果您创建了一个与 std::string
执行类似操作(在内存管理方面)的类,您会遇到同样的问题.
You would have encountered the same problem if you created a class that does similar things (in terms of memory management) as std::string
.
可能内存损坏的原因是有问题的对象(在本例中为std::string
)分配和管理动态分配的内存.如果应用程序使用一个堆,而 DLL 使用另一个堆,如果您在 DLL 中实例化 std::string
,但应用程序正在调整字符串的大小(意思是可能会发生内存分配)?
Probably the reason for the memory corruption is that the object in question (std::string
in this case) allocates and manages dynamically allocated memory. If the application uses one heap, and the DLL uses another heap, how is that going to work if you instantiated the std::string
in say, the DLL, but the application is resizing the string (meaning a memory allocation could occur)?
这篇关于C++ 通过引用 dll 中的函数传递 std::string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ 通过引用 dll 中的函数传递 std::string
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- C++ 协变模板 2021-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 近似搜索的工作原理 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 从python回调到c++的选项 2022-11-16
- STL 中有 dereference_iterator 吗? 2022-01-01
- 静态初始化顺序失败 2022-01-01