我可以将 std::string 传递给 DLL 吗?

Can I pass std::string to a DLL?(我可以将 std::string 传递给 DLL 吗?)

本文介绍了我可以将 std::string 传递给 DLL 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个代码片段分成一个 DLL,因为它会经常更新,这样它应该更容易部署.

I separated a code fragment into a DLL because it will be frequently updated and in this way it should be easier to deploy.

但是我对使用 DLL 可以做什么和不能做什么有疑问.

But I have questions about what I can do and what I cannot do with a DLL.

  1. 我可以将 std:stringCString 传递给 DLL 吗?
  2. 我可以将指针传递到带有 std::string 成员struct 并将其填充到 DLL 中吗?
  3. DLL 能否返回一个指向分配在那里的结构的指针?它会有效吗?之后可以删除吗?
  4. 什么应该更好地传递,std::String 还是 Cstring?
  1. Can I pass a std:string or a CString to a DLL?
  2. Can I pass a pointer to a struct with std::string members and fill it in a DLL?
  3. Can a DLL return a pointer to a struct allocated there? Will it be valid? Can I delete it after?
  4. What should better to pass, a std::String or a Cstring?

谢谢!

推荐答案

您有一个选择:

  • 紧密耦合的 DLL:DLL 是使用与应用程序完全相同的编译器版本、打包和调用约定设置、库选项构建的,并且都动态链接到运行时库 (/MD 编译器选项).这使您可以来回传递对象(包括 STL 容器)、从应用程序内部分配 DLL 对象、从其他模块中的基类派生,以及在不使用 DLL 的情况下执行几乎所有您可以执行的操作.缺点是您不能再独立于主应用程序部署 DLL.两者必须一起构建.DLL 只是为了改善您的进程启动时间和工作集,因为应用程序可以在加载 DLL 之前开始运行(使用 /delayload 链接器选项).构建时间也比单个模块快,尤其是在使用整个程序优化时.但是优化不会跨越应用程序-DLL 边界进行.任何重要的更改仍然需要重新构建两者.

  • Tightly coupled DLL: The DLL is built with the exact same compiler version, packing and calling convention settings, library options as the application, and both dynamically link to the runtime library (/MD compiler option). This lets you pass objects back and forth including STL containers, allocate DLL objects from inside the application, derive from base classes in the other module, do just about everything you could without using DLLs. The disadvantage is that you can no longer deploy the DLL independently of the main application. Both must be built together. The DLL is just to improve your process startup time and working set, because the application can start running before loading the DLL (using the /delayload linker option). Build times are also faster than a single module, especially when whole program optimization is used. But optimization doesn't take place across the application-DLL boundary. And any non-trivial change will still require rebuilding both.

松散耦合:应用程序不依赖于 DLL 定义的对象的类布局.您只使用高度兼容的数据类型:原始类型、指针、函数指针和由这些元素组成的用户定义类型.类继承自定义接口且没有数据成员和非虚函数的基类(这意味着没有构造函数和没有共享标准库对象,例如 std::stringCString).所有分配和对象创建都必须通过工厂函数完成.内存必须从分配它的模块中释放.代码和数据是分开的.头文件明确说明了每个导出函数的调用约定以及允许跨越模块边界的每个结构的打包.优点是可以完全独立地更新 DLL 和应用程序.您可以使用新的运行时库、新的编译器版本,甚至使用全新的语言来重新构建一个,甚至不必接触另一个.

Loosely coupled: The application doesn't depend on the class layout of objects defined by the DLL. You use only highly compatible data types: primitive types, pointers, function pointers, and user-defined types made up of these elements. Classes inherit from a base class which defines interface and has no data members and no non-virtual functions (this means no constructors and no sharing of standard library objects such as std::string or CString). All allocation and object creation must be done through a factory function. Memory must be deallocated from the module which allocated it. Code and data are separated. The header file explicitly states the calling convention of each exported function and packing of each structure allowed to cross module boundaries. The advantage is that the DLL and application can be updated completely independently. You can rebuild one with a new runtime library, new compiler version, or even in a completely new language, and don't have to even touch the other.

我总是建议使用松散耦合的方法.

I always advise using the loosely coupled approach.

这篇关于我可以将 std::string 传递给 DLL 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:我可以将 std::string 传递给 DLL 吗?

基础教程推荐