带有托管 C++ 的加载程序锁定(regsvr32 R6033 错误)动态链接库

发布于 2024-08-03 20:17:56 字数 812 浏览 3 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(2

思慕 2024-08-10 20:17:56

您只需将 /clr 编译器标志添加到使用托管代码的文件中,而不是整个项目中。

这就是 Visual Studio“向导”所做的,这是我测试的方法:

  • 创建 Visual C++ ATL 项目
  • 添加 ATL 简单对象,以便有 COM 接口(项目 -> 添加类)
  • 添加 CLR 组件班级。向导提示我“您正在将 CLR 组件添加到本机项目。您的项目将被转换为具有公共语言运行时支持。”
  • 编译项目,编译正常,注册正常。
  • 检查项目设置-> “没有公共语言运行时支持”
  • 检查了 clrcomponennt.cpp 设置 -> “公共语言运行时支持(/clr)”
  • 在OleView中打开dll-> COM 接口存在
  • 在 Red Gate 的 .NET Reflector 中打开 dll -> clr 组件存在

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:

  • Create a Visual C++ ATL Project
  • Added a ATL Simple Object, in order to have a COM interface (Project->Add Class)
  • Added a CLR Component Class. The Wizard prompted me with "You are adding a CLR component to a native project. Your project will be converted to have Common Language Runtime support."
  • Compile project, compiles fine and registers fine.
  • Checked the project settings -> "No Common Language Runtime support"
  • Checked the clrcomponennt.cpp settings -> "Common Language Runtime Support (/clr)"
  • Opened the dll in OleView -> COM interface was present
  • Opened the dll in Red Gate's .NET Reflector -> clrcomponent was present
つ低調成傷 2024-08-10 20:17:56

使用 /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.

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