title: Creating .dll MFC Dialog from C++/CLI - Loader lock
body: <p>There are a lot of &quot;similar&quot; questions but none exactly like this.</p>
  <p>My task is to connect existing MFC C++ app with AutoCAD via dotnet api.</p>
  <p>So I've created C# for AutoCAD, C++/CLI as middleman one more C++ for implementing abstract class and then there is MFC C++.
  Every project is compiled as .dll. MFC C++ has this class with one function responsible for creating dialog window.</p>
  <pre><code>//thirdParty.h
  #pragma once
  #ifdef THIRDPARTY_EXPORTS
  #define THIRDPARTY_API __declspec(dllexport)
  #else
  #define THIRDPARTY_API __declspec(dllimport)
  #endif
  
  class THIRDPARTY_API ThirdPartyIO
  {
  public:
      void OpenDialog();
  };
  </code></pre>
  <p>and</p>
  <pre><code>//thirdParty.cpp
  void ThirdParty::ThirdPartyIO::OpenDialog()
  {
      AFX_MANAGE_STATE(AfxGetStaticModuleState());
      GUI::DlgMain dlg;
      dlg.DoModal();
  }
  </code></pre>
  <p>Then I have my C++ where the abstract class will be implemented, but for now I am using it to create instance of ThirdPartyIO</p>
  <pre><code>//PluginTester.h
  #pragma once
  #include &quot;thirdPartyIO.h&quot;
  
  #ifdef PLUGINTESTER_EXPORTS
  #define PLUGINTESTER_API __declspec(dllexport)
  #else
  #define PLUGINTESTER_API __declspec(dllimport)
  #endif
  
  class PLUGINTESTER_API Tester
  {
  public:
      ThirdParty::ThirdPartyIO io;
      void Call();
  };
  </code></pre>
  <p>and</p>
  <pre><code>#include &quot;PluginTester.h&quot;
  
  void Tester::Call()
  {   
      io.OpenDialog();
  }
  </code></pre>
  <p>then there is C++/CLI mediator class</p>
  <pre><code>//Mediator.h
  #pragma once
  
  #include &quot;Tester.h&quot;
  #include &quot;vcclr.h&quot;
  
  
  namespace Wrapper
  {
      public ref class Mediator
      {
      public:
          Mediator();
          ~Mediator();
          !Mediator();
          void Call();
  
      private:
          void Destroy();
          Tester *m_pInterface = nullptr;
      };
  }
  </code></pre>
  <p>and</p>
  <pre><code>//Mediator.cpp
  #include &quot;Mediator.h&quot;
  #include &lt;string&gt;
  #include &lt;Windows.h&gt;
  
  
  Wrapper::Mediator::Mediator() : m_pInterface(new Tester)
  {
  
  }
  
  Wrapper::Mediator::~Mediator()
  {
      Destroy();
  }
  
  Wrapper::Mediator::!Mediator()
  {
      Destroy();
  }
  
  void Wrapper::Mediator::Destroy()
  {
      if (m_pInterface != nullptr)
      {
          delete m_pInterface;
          m_pInterface = nullptr;
      }
  }
  
  void Wrapper::Mediator::Call()
  {
      return m_pInterface-&gt;Call();
  }
  </code></pre>
  <p>and then finally my Plugin C# class</p>
  <pre><code>using Autodesk.AutoCAD.Runtime;
  using Autodesk.AutoCAD.EditorInput;
  using Autodesk.AutoCAD.ApplicationServices;
  using Wrapper;
  
  
  namespace ACADPlugin
  {
      public class ACADPlugin : IExtensionApplication
      {
          private Mediator m_wrapper;
  
          [CommandMethod(&quot;InstancePlugin&quot;, CommandFlags.Modal)]
          public void CreateInstance()
          {
              m_wrapper = new Mediator();
             // m_wrapper.Call();
              Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
              ed.WriteMessage(&quot;\nFirstCommand called&quot;);
          }
  
          void IExtensionApplication.Initialize()
          {
              Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
              ed.WriteMessage(&quot;\nPlugin successfully loaded!&quot;);
          }
  
          void IExtensionApplication.Terminate()
          {
  
          }
      }
  }
  </code></pre>
  <p>Now the problem is, even if I don't call &quot;Call&quot; method, if I use AFX_MANAGE_STATE in thirdParty.cpp somewhere I get OS Loader lock exception. I know it can be turned off, but that's not the point. It seems like the AFX_MANAGE_STATE is calling into managed code or something, but I don't really understand why or how since it is C++ macro.</p>
  <p>If I don't use AFX_MANAGE_STATE I don't get lock exception, but I cannot create dialog as I get mfc140ud.dll error.</p>
  <p>If I turn the exception off, it sometimes works, but it seems to be slow and if I even create the dialog window, ACAD becomes impossible to use, it isn't totally unresponsive but it registers event every 2 seconds or so.</p>
  <p>If anyone has any ideas I would greatly appreciate it.</p>
  <p>Thanks.</p>