C ++传递std :: string通过引用dll中的函数 [英] C++ Passing std::string by reference to function in dll

查看:633
本文介绍了C ++传递std :: string通过引用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 在函数中设置任何值(例如)。



t4gs..\ n \t )。



我尝试检测任何堆损坏,说,没有错误。



DLL已在调试设置中编译。 .exe程序在调试过程中编译。



有什么问题?帮助..!



我使用Visual Studio 2013. WinApp。



EDIT



of func到此代码:

  AFCLIBDLL_API EAFCErrors CAFC :: CreateArchive(char const * const _pArchiveName)
{
std :: string _sArchiveName(_pArchiveName);
...

我真的不知道,如何修复这个bug。 。



关于heap:它被分配在我们进程的虚拟内存中,对吧?在这种情况下,共享虚拟内存是常见的。

解决方案

这个问题与STL无关,对象跨应用程序边界。



1)DLL和EXE必须使用相同的项目设置进行编译。你必须这样做,使得结构对齐和打包是相同的,成员和成员函数没有不同的行为,甚至更微妙,引用和引用参数的低级实现是完全相同的。



2)DLL和EXE必须使用相同的运行时堆。为此,必须使用运行时库的DLL版本。



如果您创建了一个类似于std :: string类似的东西(在内存管理方面),您会遇到同样的问题。



可能是内存损坏的原因是有问题的对象(std :: string)分配和管理动态分配的内存。如果应用程序使用一个堆,并且DLL使用另一个堆,如果你实例化std :: string说,DLL,但应用程序正在调整字符串大小(意味着可能发生内存分配),那将如何工作?


I have the problem with passing by reference std::string to function in dll.

This is function call:

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"));

This is function header:

#define AFCLIBDLL_API __declspec(dllimport) 
AFCLIBDLL_API EAFCErrors CAFC::OpenArchive(std::string const &_sFileName);

I try to debug pass-by-step through calling the function and look at _sFileName value inside function.

_sFileName in function sets any value(for example, t4gs..\n\t).

I try to detect any heap corruption, but compiler says, that there is no error.

DLL has been compiled in debug settings. .exe programm compiled in debug too.

What's wrong?? Help..!

P.S. I used Visual Studio 2013. WinApp.

EDIT

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.

解决方案

The issue has little to do with STL, and everything to do with passing objects across application boundaries.

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) The DLL and the EXE must use the same runtime heap. To do this, you must use the DLL version of the runtime library.

You would have encountered the same problem if you created a class that does similar things (in terms of memory management) as std::string.

Probably the reason for the memory corruption is that the object in question (std::string) allocates and manages dynamically allocated memory. If the app 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 ++传递std :: string通过引用dll中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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