<p>I am building on C++ dll, by writing code in C#.</p>

<p>I get an error, saying </p>

<blockquote>
  <p>LoaderLock was detected Message:
  Attempting managed execution inside OS
  Loader lock. Do not attempt to run
  managed code inside a DllMain or image
  initialization function since doing so
  can cause the application to hang.</p>
</blockquote>

<p>I tried seraching what this error exactly means, but I am drawing pointless articles, mostly saying that it's just a warning, and I should switch that off in Visual Studio.
The other solutions seem to be due to ITunes, or this problem occurring when programming with DirectX. My problem is connected to neither.</p>

<p>Can anybody explain, what this actually means?</p>

[{'answer_id': 56653, 'author': 'Hershi', 'author_id': 1596, 'author_profile': 'https://Stackoverflow.com/users/1596', 'pm_score': 6, 'selected': False, 'text': '<p>The general idea of loader lock:\nThe system runs the code in DllMain inside a lock (as in - synchronization lock). Therefore, running non-trivial code inside DllMain is &quot;asking for a deadlock&quot;, as described <a href="https://devblogs.microsoft.com/oldnewthing/20040128-00/?p=40853" rel="nofollow noreferrer">here</a>.</p>\n<p>The question is, why are you trying to run code inside DllMain? Is it crucial that this code run inside the context of DllMain or can you spawn a new thread and run the code in it, and not wait for the code to finish execution inside DllMain?</p>\n<p>I believe that the problem with manged code specifically, is that running managed code might involves loading the CLR and suchlike and there\'s no knowing what could happen there that would result in a deadlock... I would not heed the advice of &quot;disable this warning&quot; if I were you because most chances are you\'ll find your applications hangs unexpectedly under some scenarios.</p>\n'}, {'answer_id': 4094795, 'author': 'ghiboz', 'author_id': 349045, 'author_profile': 'https://Stackoverflow.com/users/349045', 'pm_score': 6, 'selected': False, 'text': '<p>you need to go to menu Debug -> Exceptions, open the Managed Debugging Assistants, find LoaderLock and uncheck</p>\n\n<p><img src="https://i.stack.imgur.com/eRNGy.png" alt="http://goo.gl/TGAHV"></p>\n'}, {'answer_id': 11462518, 'author': 'Rakshit Bakshi', 'author_id': 770550, 'author_profile': 'https://Stackoverflow.com/users/770550', 'pm_score': 2, 'selected': False, 'text': '<p>This problem occurs because of the way in which the debugger in Visual Studio runs managed applications that use Microsoft Foundation Classes version 8.0 in one or more DLL files.</p>\n\n<p>Have a thorough reading at: <a href="http://msdn.microsoft.com/en-us/library/aa290048(vs.71).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/aa290048(vs.71).aspx</a></p>\n'}, {'answer_id': 11535564, 'author': 'Bhaskar Singh', 'author_id': 1057132, 'author_profile': 'https://Stackoverflow.com/users/1057132', 'pm_score': 3, 'selected': False, 'text': '<p>Press ctr d+e Then Expend Managed Debugging Assistants Node. Then Unchecked the LoaderLock.</p>\n\n<p>Hope this will help you.</p>\n'}, {'answer_id': 31322244, 'author': 'Stefan Wanitzek', 'author_id': 1995301, 'author_profile': 'https://Stackoverflow.com/users/1995301', 'pm_score': 2, 'selected': False, 'text': '<p>I recently got this error while creating an instance of an COM-Object written in native code:</p>\n\n<pre><code>m_ComObject = Activator.CreateInstance(Type.GetTypeFromProgID("Fancy.McDancy"));\n</code></pre>\n\n<p>This led to the described error. A "LoaderLock was detected"-Exception was thrown.</p>\n\n<p>I overcame this error by creating the object-instance in an extra thread:</p>\n\n<pre><code>ThreadStart threadRef = new ThreadStart(delegate { m_ComObject = Activator.CreateInstance(Type.GetTypeFromProgID("Fancy.McDancy")); });\nThread myThread = new Thread(threadRef);\n\nmyThread.Start();\nmyThread.Join(); // for synchronization\n</code></pre>\n'}, {'answer_id': 36120918, 'author': 'seva titov', 'author_id': 656346, 'author_profile': 'https://Stackoverflow.com/users/656346', 'pm_score': 4, 'selected': False, 'text': '<h3>UPDATE FOR .NET 4.0 AND MORE RECENT FRAMEWORKS</h3>\n\n<p>This is an old question asked at the time of .Net 2.0, when support for mixed mode DLLs had serious initialization problems, prone to random deadlocks. As of .Net 4.0, the initialization of mixed mode DLLs has changed. Now there are two separate stages of initialization: </p>\n\n<ol>\n<li>Native initialization, called at the DLL\'s entry point, which includes native C++ run-time setup and execution of your DllMain method.</li>\n<li>Managed initialization, executed automatically by system loader.</li>\n</ol>\n\n<p>Since step #2 is performed outside of the Loader Lock, there is no deadlocks. The details are described at <a href="https://msdn.microsoft.com/en-us/library/ms173266.aspx" rel="noreferrer">Initialization of Mixed Assemblies</a>. </p>\n\n<p>To ensure your mixed mode assembly can be loaded from a native executable, the only thing you need to check is that DllMain method is declared as native code. <code>#pragma unmanaged</code> could help here:</p>\n\n<pre><code>#pragma unmanaged\n\nBOOL APIENTRY DllMain(HMODULE hModule,\n    DWORD  ul_reason_for_call,\n    LPVOID lpReserved\n    )\n{\n    ... // your implementation here\n}\n</code></pre>\n\n<p>It is also important that any code that DllMain might call directly or indirectly is also unmanaged. It makes sense to limit the type of functionality used by DllMain so you trace all code reachable from DllMain and ensure it is all compiled with <code>#pragma unmanaged</code>. </p>\n\n<p>Compiler helps a little by giving you warining C4747 if it detects that DllMain is not declared as unmanaged:</p>\n\n<pre><code>1&gt;  Generating Code...\n1&gt;E:\\src\\mixedmodedll\\dllmain.cpp : warning C4747: Calling managed \'DllMain\': Managed code may not be run under loader lock, including the DLL entrypoint and calls reached from the DLL entrypoint\n</code></pre>\n\n<p>However compiler won\'t generate any warnings if DllMain indirectly calls some other managed function, so you need to ensure that never happens, otherwise your application could deadlock randomly.</p>\n'}, {'answer_id': 38396996, 'author': 'Menace', 'author_id': 1100751, 'author_profile': 'https://Stackoverflow.com/users/1100751', 'pm_score': 2, 'selected': False, 'text': "<p>I'm building a C++ CLR DLL (MSVS2015) that has to make calls into an unmanaged DLL and define unmanaged code. I use #pragma managed and #pragma unmanaged to control what mode it is in for a given area of the code. </p>\n\n<p>In my case I simply put #pragma unmanaged in front of my DllMain() and this solved the problem.\nIt seemed to be thinking I wanted a managed version of DllMain().</p>\n"}, {'answer_id': 50424418, 'author': 'Jog Dan', 'author_id': 1190324, 'author_profile': 'https://Stackoverflow.com/users/1190324', 'pm_score': 3, 'selected': False, 'text': '<p>kindly remind those <strong>VS2017 users</strong> that you need to disable "<strong>exception helper</strong>" instead of "<strong>exception assistant</strong>"(before VS2017) to prevent from loader lock error, which setting path is <em>Debug->Exception</em>. Just ran int to this problem and wasted 2 hours searching for solutions... </p>\n'}, {'answer_id': 52935339, 'author': 'Jimmy', 'author_id': 68936, 'author_profile': 'https://Stackoverflow.com/users/68936', 'pm_score': 2, 'selected': False, 'text': '<p>The setting path in my visual studio 2017 instance is Debug -> Windows -> Exception Settings . The exception settings "window" showed up in the bottom tab group (as opposed to a separate window), took me a while to notice it. Search for "loader".</p>\n'}]
['https://Stackoverflow.com/questions/56642', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/895/']