使用 C++ 具有不同编译器版本的 DLL

发布于 2024-07-09 11:20:09 字数 1037 浏览 13 评论 0原文

这个问题与“如何跨VS制作一致的dll二进制文件”相关版本?”

  • 我们已经构建了应用程序和 DLL 使用 VC6 和构建的新应用程序 与VC9。 VC9-app 必须使用 用VC6编译的DLL,大部分 其中一个是用 C 编写的,一个是 C++。
  • C++ 库有问题,原因是 名称修饰/修饰问题。
  • 用VC9编译一切是 目前没有这样的选择 似乎有一些副作用。 解决这些问题需要很长时间 消耗。
  • 我可以修改C++库,但它必须用VC6编译。
  • C++ 库本质上是另一个 C 库的 OO 包装器。 VC9-app 使用一些静态函数以及一些非静态函数。

虽然静态函数可以用类似的方法来处理,但

// Header file
class DLL_API Foo
{
    int init();
}

extern "C"
{
    int DLL_API Foo_init();
}

// Implementation file
int Foo_init()
{
    return Foo::init();
}

使用非静态方法就不那么容易了。

据我了解,克里斯·贝克的建议使用类似 COM 的接口对我没有帮助,因为接口成员名称仍然会被修饰,因此无法从使用不同编译器创建的二进制文件中访问。 我就在那里吗?

唯一的解决方案是使用对象的处理程序编写一个 C 风格的 DLL 接口,还是我遗漏了一些东西? 在这种情况下,我想,直接使用包装的 C 库可能会花费更少的精力。

This question is related to "How to make consistent dll binaries across VS versions ?"

  • We have applications and DLLs built
    with VC6 and a new application built
    with VC9. The VC9-app has to use
    DLLs compiled with VC6, most of
    which are written in C and one in
    C++.
  • The C++ lib is problematic due to
    name decoration/mangling issues.
  • Compiling everything with VC9 is
    currently not an option as there
    appear to be some side effects.
    Resolving these would be quite time
    consuming.
  • I can modify the C++ library, however it must be compiled with VC6.
  • The C++ lib is essentially an OO-wrapper for another C library. The VC9-app uses some static functions as well as some non-static.

While the static functions can be handled with something like

// Header file
class DLL_API Foo
{
    int init();
}

extern "C"
{
    int DLL_API Foo_init();
}

// Implementation file
int Foo_init()
{
    return Foo::init();
}

it's not that easy with the non-static methods.

As I understand it, Chris Becke's suggestion of using a COM-like interface won't help me because the interface member names will still be decorated and thus inaccessible from a binary created with a different compiler. Am I right there?

Would the only solution be to write a C-style DLL interface using handlers to the objects or am I missing something?
In that case, I guess, I would probably have less effort with directly using the wrapped C-library.

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

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

发布评论

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

评论(5

强辩 2024-07-16 11:20:10

当使用使用与调用 EXE 不同的 C++ 编译器编译的 DLL 时,要考虑的最大问题是内存分配和对象生存期。

我假设你可以通过名称修改(和调用约定),如果你使用具有兼容修改的编译器(我认为 VC6 与 VS2008 广泛兼容),或者如果你使用 extern "C",这并不困难。

当您使用new(或malloc)从 DLL 中分配某些内容,然后将其返回给调用者时,就会遇到问题。 调用者的delete(或free)将尝试从不同的堆中释放该对象。 这会出错。

您可以执行 COM 样式的 IFoo::Release 操作,也可以执行 MyDllFree() 操作。 因为它们回调到 DLL,所以它们都将使用 delete(或 free())的正确实现,因此它们将删除正确的对象。

或者,您可以确保使用 LocalAlloc(例如),以便 EXE 和 DLL 使用相同的堆。

The biggest problem to consider when using a DLL compiled with a different C++ compiler than the calling EXE is memory allocation and object lifetime.

I'm assuming that you can get past the name mangling (and calling convention), which isn't difficult if you use a compiler with compatible mangling (I think VC6 is broadly compatible with VS2008), or if you use extern "C".

Where you'll run into problems is when you allocate something using new (or malloc) from the DLL, and then you return this to the caller. The caller's delete (or free) will attempt to free the object from a different heap. This will go horribly wrong.

You can either do a COM-style IFoo::Release thing, or a MyDllFree() thing. Both of these, because they call back into the DLL, will use the correct implementation of delete (or free()), so they'll delete the correct object.

Or, you can make sure that you use LocalAlloc (for example), so that the EXE and the DLL are using the same heap.

孤城病女 2024-07-16 11:20:10

接口成员名称​​不会被修饰——它们只是虚函数表中的偏移量。 您可以在头文件中定义一个接口(使用 C 结构,而不是 COM“接口”),从而:

struct IFoo {
    int Init() = 0;
};

然后,您可以从 DLL 导出一个函数,而无需修改:

class CFoo : public IFoo { /* ... */ };
extern "C" IFoo * __stdcall GetFoo() { return new CFoo(); }

这将工作得很好,前提是您'正在使用生成兼容 vtable 的编译器。 从(至少我认为)MSVC6.1 开始,Microsoft C++ 已经为 DOS 生成了相同格式的 vtable,其中 vtable 是指向函数的简单指针列表(在多重继承情况下使用 thunking)。 GNU C++(如果我没记错的话)生成带有函数指针和相对偏移量的虚函数表。 这些彼此不兼容。

Interface member names will not be decorated -- they're just offsets in a vtable. You can define an interface (using a C struct, rather than a COM "interface") in a header file, thusly:

struct IFoo {
    int Init() = 0;
};

Then, you can export a function from the DLL, with no mangling:

class CFoo : public IFoo { /* ... */ };
extern "C" IFoo * __stdcall GetFoo() { return new CFoo(); }

This will work fine, provided that you're using a compiler that generates compatible vtables. Microsoft C++ has generated the same format vtable since (at least, I think) MSVC6.1 for DOS, where the vtable is a simple list of pointers to functions (with thunking in the multiple-inheritance case). GNU C++ (if I recall correctly) generates vtables with function pointers and relative offsets. These are not compatible with each other.

故事未完 2024-07-16 11:20:10

好吧,我认为 Chris Becke 的建议是正好。 我不会使用 Roger 的第一个解决方案,它使用一个接口只是名义上的,正如他提到的,可能会遇到抽象类和虚拟方法的编译器处理不兼容的问题。 Roger 在他的后续内容中指出了有吸引力的 COM 一致案例。

  1. 痛点:需要学会发出COM接口请求并正确处理IUnknown,至少依赖IUnknown:AddRef和IUnknown:Release。 如果接口的实现可以支持多个接口或者方法也可以返回接口,那么您可能还需要熟悉 IUnknown:QueryInterface。

  2. 这是关键思想。 所有使用接口实现(但不实现它)的程序都使用通用的#include“*.h”文件,该文件将接口定义为结构体 (C) 或 C/C++ 类 (VC++) 或struct(非 VC++,而是 C++)。 *.h 文件会根据您是编译 C 语言程序还是 C++ 语言程序自动进行适当调整。 您无需了解该部分即可使用 *.h 文件。 *.h 文件的作用是定义 Interface 结构或类型,例如 IFoo,及其虚拟成员函数(并且只有函数,在此方法中对数据成员没有直接可见性)。

  3. 头文件的构造是为了遵循 COM 二进制标准,其方式既适用于 C,也适用于 C++,无论使用什么 C++ 编译器。 (Java JNI 人员解决了这个问题。)这意味着它可以在任何来源的单独编译的模块之间工作,只要完全由函数入口指针(虚函数表)组成的结构被所有模块映射到相同的内存(例如,它们必须全部是 x86 32 位,或者全部是 x64)。

  4. 在通过某种包装类实现 COM 接口的 DLL 中,您只需要一个工厂入口点。 类似于

    extern "C" HRESULT MkIFooImplementation(void **ppv);

它返回一个 HRESULT(您也需要了解这些),并且还将在您提供的用于接收 IFoo 接口指针的位置返回一个 *pv。 (我正在浏览,这里您需要更仔细的细节。不要相信我的语法)为此使用的实际函数构造型也在 *.h 文件中声明。

  1. 重点是工厂条目(始终是未修饰的 extern“C”)执行所有必要的包装类创建,然后将 Ifoo 接口指针传递到您指定的位置。 这意味着用于创建类的所有内存管理以及用于完成类的所有内存管理等都将在构建包装器的 DLL 中进行。 这是您必须处理这些细节的唯一地方。

  2. 当您从工厂函数获得 OK 结果时,您已获得一个接口指针,并且它已为您保留(已经代表您传递的接口指针执行了隐式 IFoo:Addref 操作) 。

  3. 使用完接口后,可以通过调用接口的 IFoo:Release 方法来释放它。 最终版本实现(如果您制作了更多 AddRef 副本)将拆除工厂 DLL 中的类及其接口支持。 这就是让您正确依赖接口背后一致的动态存储分配和释放的原因,无论包含工厂函数的 DLL 是否使用与调用代码相同的库。

  4. 您可能也应该实现 IUnknown:QueryInterface (作为方法 IFoo:QueryInterface),即使它总是失败。 如果您想更复杂地使用 COM 二进制接口模型,因为您有更多的经验,您可以学习提供完整的 QueryInterface 实现。

这可能是太多信息,但我想指出,您所面临的有关 DLL 异构实现的许多问题都在 COM 二进制接口的定义中得到了解决,即使您不需要所有这些问题,事实上,它提供了可行的解决方案,这一点很有价值。 根据我的经验,一旦掌握了这一点,您将永远不会忘记它在 C++ 和 C++ 互操作情况下的强大功能。

我没有概述您可能需要参考示例的资源,以及为了制作 *.h 文件并实际实现您想要共享的库的工厂函数包装器而必须学习的内容。 如果你想深入挖掘,请大声喊叫。

Well, I think Chris Becke's suggestion is just fine. I would not use Roger's first solution, which uses an interface in name only and, as he mentions, can run into problems of incompatible compiler-handling of abstract classes and virtual methods. Roger points to the attractive COM-consistent case in his follow-on.

  1. The pain point: You need to learn to make COM interface requests and deal properly with IUnknown, relying on at least IUnknown:AddRef and IUnknown:Release. If the implementations of interfaces can support more than one interface or if methods can also return interfaces, you may also need to become comfortable with IUnknown:QueryInterface.

  2. Here's the key idea. All of the programs that use the implementation of the interface (but don't implement it) use a common #include "*.h" file that defines the interface as a struct (C) or a C/C++ class (VC++) or struct (non VC++ but C++). The *.h file automatically adapts appropriately depending on whether you are compiling a C Language program or a C++ language program. You don't have to know about that part simply to use the *.h file. What the *.h file does is define the Interface struct or type, lets say, IFoo, with its virtual member functions (and only functions, no direct visibility to data members in this approach).

  3. The header file is constructed to honor the COM binary standard in a way that works for C and that works for C++ regardless of the C++ compiler that is used. (The Java JNI folk figured this one out.) This means that it works between separately-compiled modules of any origin so long as a struct consisting entirely of function-entry pointers (a vtable) is mapped to memory the same by all of them (so they have to be all x86 32-bit, or all x64, for example).

  4. In the DLL that implements the the COM interface via a wrapper class of some sort, you only need a factory entry point. Something like an

    extern "C" HRESULT MkIFooImplementation(void **ppv);

which returns an HRESULT (you'll need to learn about those too) and will also return a *pv in a location you provide for receiving the IFoo interface pointer. (I am skimming and there are more careful details that you'll need here. Don't trust my syntax) The actual function stereotype that you use for this is also declared in the *.h file.

  1. The point is that the factory entry, which is always an undecorated extern "C" does all of the necessary wrapper class creation and then delivers an Ifoo interface pointer to the location that you specify. This means that all memory management for creation of the class, and all memory management for finalizing it, etc., will happen in the DLL where you build the wrapper. This is the only place where you have to deal with those details.

  2. When you get an OK result from the factory function, you have been issued an interface pointer and it has already been reserved for you (there is an implicit IFoo:Addref operation already performed on behalf of the interface pointer you were delivered).

  3. When you are done with the interface, you release it with a call on the IFoo:Release method of the interface. It is the final release implementation (in case you made more AddRef'd copies) that will tear down the class and its interface support in the factory DLL. This is what gets you correct reliance on a consistent dynamic stoorage allocation and release behind the interface, whether or not the DLL containing the factory function uses the same libraries as the calling code.

  4. You should probably implement IUnknown:QueryInterface (as method IFoo:QueryInterface) too, even if it always fails. If you want to be more sophisticated with using the COM binary interface model as you have more experience, you can learn to provide full QueryInterface implementations.

This is probably too much information, but I wanted to point out that a lot of the problems you are facing about heterogeneous implementations of DLLs are resolved in the definition of the COM binary interface and even if you don't need all of it, the fact that it provides worked solutions is valuable. In my experience, once you get the hang of this, you will never forget how powerful this can be in C++ and C++ interop situations.

I haven't sketched the resources you might need to consult for examples and what you have to learn in order to make *.h files and to actually implement factory-function wrappers of the libraries you want to share. If you want to dig deeper, holler.

那请放手 2024-07-16 11:20:10

您还需要考虑其他事情,例如各个库正在使用哪些运行时。 如果没有共享对象,那很好,但乍一看这似乎不太可能。
Chris Becker 的建议非常准确 - 使用实际 COM 接口可以帮助您获得所需的二进制兼容性。 你的旅费可能会改变 :)

There are other things you need to consider too, such as which run-times are being used by the various libraries. If no objects are being shared that's fine, but that seems quite unlikely at first glance.

Chris Becker's suggestions are pretty accurate - using an actual COM interface may help you get the binary compatibility you need. Your mileage may vary :)

我不吻晚风 2024-07-16 11:20:10

不好玩,伙计。 你感到很沮丧,你可能应该给出这个:

唯一的解决办法是写一个
使用处理程序的 C 风格 DLL 接口
到物体还是我失踪了
某物? 在那种情况下,我想,我
可能会花费更少的努力
直接使用封装的 C 库。

非常仔细地观察。 祝你好运。

not fun, man. you are in for a lot of frustration, you should probably give this:

Would the only solution be to write a
C-style DLL interface using handlers
to the objects or am I missing
something? In that case, I guess, I
would probably have less effort with
directly using the wrapped C-library.

a really close look. good luck.

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