多个DLL的问题

Issues with multiple DLL

本文关键字:问题 DLL 多个      更新时间:2023-10-16

我有一个引用2个COM DLL的应用程序。在应用程序启动时调用的2个DLL中有一个Init函数。

这两个DLL几乎相似,只是它们有不同的uid和不同的接口名称。除此之外,逻辑是一样的。。。。并且所使用的变量也是相同的。

这就是这个系统的工作原理——1.StartApp()2.初始化DLL1(appVar1,appVar2)。。在这里,应用程序将这个DLL中定义的var x,y设置为从应用程序传递的值。假设x=appVar1,y=appVar2。x和y是全局变量。3.初始化DLL2(appVar1a,appVar2a)。。。应用程序将DLL2中定义的var x设置为从应用程序传递的值。x=appVar1a;y=appVar2。

现在,当我尝试在DLL1上运行一些东西时,x和y的值神秘地变为了其他值。

x现在变成appVar2,y为空。

这里,x、y和应用程序在InitDLL函数中传递的所有变量(如appVar1等)都是BSTR。

我踩过了密码。。。x、 y在InitDLL1中正确设置为appVar1、appVar2。但一旦这个函数返回,并且我们正在初始化第二个DLL(InitDLL2),情况就会改变。

有趣的是,我在DLL2中没有看到任何这样的问题……尽管代码/逻辑非常相似。。。。除了它调用的接口。

在DLL1和DLL2中,在InitDLL函数中,我们创建了一个新线程,在其中我们在各种函数中使用x和y。由于上述问题,DLL1总是失败。。DLL2没有问题。

有什么线索可以说明哪里出了问题吗?

你不能只分配一个BSTR,你需要复制它

// caller - by COM memory management rules, for [in] parameters
// caller needs to allocate and free the memory
BSTR str = SysAllocString("..."); // string is allocated
comObjectPtr->Init(str);          // in the function, pointer is copied to global
SysFreeString(str);               // string released, pointer points to garbage
// callee
ComObject::Init(BSTR str)
{
// wrong code!!! you have no idea about lifetime of str,
// so you can't just assign it. When the caller function
// exits, string memory is released and your global is thrashed
// I suspect you use some BSTR wrapper (bstr_t or CComBSTR) so
// you don't see the deallocation part and that's why the values
// are invalidated on function exit: the destructor of BSTR wrapper
// does its job
global_str = str;             
// correct code:
// global_str = SysAllocString(str);
}