简体   繁体   English

使用托管C ++ dll加载器锁定(regsvr32 R6033错误)

[英]Loader lock (regsvr32 R6033 error) with managed C++ dll

I have a C++ dll which implements several COM interfaces, that I'm trying to migrate to managed C++. 我有一个C ++ DLL实现了几个COM接口,我正在尝试迁移到托管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. 我设置/ clr编译器标志并将运行时库属性从/ MT更改为/ MD以避免这两个标志之间的冲突,但这就是我所有的变化。 When it attempts to register the dll during the build process, I get the following error: 当它在构建过程中尝试注册dll时,我收到以下错误:

R6033 - Attempt to use MSIL code from this assembly during native code initialization This indicates a bug in your application. R6033 - 在本机代码初始化期间尝试使用此程序集中的MSIL代码这表示应用程序中存在错误。 It is most likely the result of calling an MSIL-compiled (/clr) function from a native constructor or from DllMain. 它很可能是从本机构造函数或DllMain调用MSIL编译(/ clr)函数的结果。

I read about Loader Lock and can't figure it out - I have not added a single call to any managed code. 我读到了Loader Lock并且无法弄明白 - 我没有为任何托管代码添加一个调用。 Here's the entire body of the DllMain procedure: 这是DllMain程序的整个主体:

[Edit - per comment below, I added #pragma unmanaged to the top of the cpp file with no improvement. [编辑 - 根据下面的评论,我将#pragma unmanaged添加到cpp文件的顶部,没有任何改进。 The Module init is all code contained in the ATL libraries from what I can tell.] 模块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
}

You need to add the /clr compiler flag only to the files that use managed code and not for the whole project. 您需要将/ clr编译器标志仅添加到使用托管代码的文件,而不是整个项目。

This is what the Visual Studio "Wizard" does, here is how I've tested: 这就是Visual Studio“向导”所做的,这是我测试的方式:

  • Create a Visual C++ ATL Project 创建一个Visual C ++ ATL项目
  • Added a ATL Simple Object, in order to have a COM interface (Project->Add Class) 添加了一个ATL简单对象,以便拥有一个COM接口(Project-> Add Class)
  • Added a CLR Component Class. 添加了CLR组件类。 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." 向导向我提示“您正在向本机项目添加CLR组件。您的项目将转换为具有公共语言运行时支持。”
  • 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)" 检查clrcomponennt.cpp设置 - >“公共语言运行时支持(/ clr)”
  • Opened the dll in OleView -> COM interface was present 在OleView中打开dll - >存在COM接口
  • Opened the dll in Red Gate's .NET Reflector -> clrcomponent was present 在Red Gate的.NET Reflector中打开了dll - > clrcomponent存在

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. 使用/ clr标志已经使你的方法得到了管理(即它们被编译成MSIL),但是你正在为DllMain调用它们 - 它们没有被管理。 Unfortunately, that's about as far as my limited knowledge can take it. 不幸的是,就我的有限知识而言,这是可以接受的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM