带有托管 C++ 的加载程序锁定(regsvr32 R6033 错误)动态链接库
我有一个 C++ dll,它实现了多个 COM 接口,我正在尝试将其迁移到托管 C++。我设置了 /clr 编译器标志并将运行时库属性从 /MT 更改为 /MD 以避免这两个标志之间的冲突,但这就是我所做的全部更改。当它尝试在生成过程中注册 dll 时,出现以下错误:
R6033 - 尝试在本机代码初始化期间使用此程序集中的 MSIL 代码 这表明您的应用程序中存在错误。这很可能是从本机构造函数或 DllMain 调用 MSIL 编译的 (/clr) 函数的结果。
我读到了有关 Loader Lock 的内容,但无法弄清楚 - 我没有添加对任何托管代码。以下是 DllMain 过程的整个正文:
[编辑 - 根据下面的评论,我将 #pragma unmanaged 添加到 cpp 文件的顶部,没有任何改进。据我所知,模块 init 是 ATL 库中包含的所有代码。]
extern "C"
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
lpReserved;
if (dwReason == DLL_PROCESS_ATTACH)
{
_Module.Init(ObjectMap, hInstance, &MYGUID);
DisableThreadLibraryCalls(hInstance);
}
else if (dwReason == DLL_PROCESS_DETACH)
_Module.Term();
return TRUE; // ok
}
I have a C++ dll which implements several COM interfaces, that I'm trying to migrate to managed C++. I set the /clr compiler flag and changed the Runtime Library property from /MT to /MD to avoid the conflict between these two flags, but that's all I've changed. When it attempts to register the dll during the build process, I get the following error:
R6033 - Attempt to use MSIL code from this assembly during native code initialization
This indicates a bug in your application. It is most likely the result of calling an MSIL-compiled (/clr) function from a native constructor or from DllMain.
I read about Loader Lock and can't figure it out - I have not added a single call to any managed code. Here's the entire body of the DllMain procedure:
[Edit - per comment below, I added #pragma unmanaged to the top of the cpp file with no improvement. The Module init is all code contained in the ATL libraries from what I can tell.]
extern "C"
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
lpReserved;
if (dwReason == DLL_PROCESS_ATTACH)
{
_Module.Init(ObjectMap, hInstance, &MYGUID);
DisableThreadLibraryCalls(hInstance);
}
else if (dwReason == DLL_PROCESS_DETACH)
_Module.Term();
return TRUE; // ok
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需将 /clr 编译器标志添加到使用托管代码的文件中,而不是整个项目中。
这就是 Visual Studio“向导”所做的,这是我测试的方法:
You need to add the /clr compiler flag only to the files that use managed code and not for the whole project.
This is what the Visual Studio "Wizard" does, here is how I've tested:
使用 /clr 标志使您的方法成为托管的(即,它们被编译为 MSIL),但您为 DllMain 调用它们,但它不是托管的。不幸的是,这只是我有限的知识所能承受的范围。
Using /clr flag has made your methods managed (ie. they are being compiled down to MSIL), but you're calling them for DllMain which -isn't- managed. Unfortunately, that's about as far as my limited knowledge can take it.