多个DLL的问题
Issues with multiple DLL
我有一个引用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);
}
相关文章:
- 当我使用 C++ 中的 C# dll 来使用 Selenium 时,存在异常处理问题
- 发生此错误:在0x0F2BFB7C(基于.dll)时引发异常 尝试更改大小是问题吗?
- 系统错误:程序无法启动,因为您的计算机中缺少MSVCP140D.DLL。尝试重新安装该程序以解决此问题
- C++ DLL 堆内存分配问题
- C++ DLL 运行时错误"abc.dll is not a valid WIN32 application" 。请帮助解决这个问题
- DLL 导出导致唯一指针出现问题
- 如何解决python35的问题.dll'没有找到?
- C++ API-MS-WIN-SERVICE-PRIVATE-L1-1-1.DLL依赖项问题
- 问题在具有依赖性时从其他路径上加载DLL的DLL
- 我的自定义 C++ DLL 的 C# 项目中的 DLL 端口有什么问题?
- 潜在 DLL 兼容性问题的摘要?
- 从C++调用 Delphi DLL IStream 参数的问题
- 清理跨越Windows DLL模块边界的堆分配资源时出现问题
- 在Windows Server 2003中注册dll时出现问题
- DLL和EXE中具有相同名称和父级但实现不同的类中的问题
- 在C 中的DLL中解决模板的问题
- 多个DLL的问题
- JNI 问题:DLL 中的文件句柄出现符号查找错误C++
- 如何避免Windows上多个CRT版本的问题(dll地狱重访?)
- 奇怪的编译问题- DLL不编译相同的一些机器上