Solved

LoaderLock exception in VB.net

Posted on 2009-05-14
5
764 Views
Last Modified: 2013-11-25
Hi-I have a very simple project where I have imported som .dlls from another project. However I get the following errormessage when I try to run it. Does anyone know what is causing this or how I can avoid it?

"Microsoft.DirectX.DirectSound.dll' is 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."

(After the exception is caught I get the option to continue and the app will then execute as expected..)
0
Comment
Question by:jes12345
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 5

Accepted Solution

by:
burningmace earned 500 total points
ID: 24390218
The reason for this is that you're referencing managed code from what will later become (once compiled) an unmanaged section of code.

The only way around this that I have ever found to work is to tell the compiler not to throw the exception. To do this, go to Debug -> Exceptions, then open the Managed Debugging Assistants section and untick LoaderLock from the list.
0
 

Author Comment

by:jes12345
ID: 24390690
Hi - many thanks for the advise and the workaround. I believe my issue is that I do not really understand what is meant by "managed code". Can I please ask you to give a brief explanation of "managed code" vs "Unmanaged code"? Thanks J
0
 
LVL 5

Assisted Solution

by:burningmace
burningmace earned 500 total points
ID: 24393604
Managed code is basically anything that runs on the .NET framework. Unmanaged is anything imported from DLLs written in anything but .NET (usually C++ or C). For example, if you're referencing the FindWindow API from user32.dll, the FindWindow function is unmanaged (and so is the whole of user32.dll).

The LoaderLock exception isn't actually a real exception, it's a debugging assistant given the form of an exception so it can be caught on throw by the debugger, hence the option to continue and the green(?) colouring of the exception window. This means that if your application runs without a debugger attached, the LoaderLock exception is never thrown. My workaround is to simply tell the debugger not to throw LoaderLock exceptions. It's technically a bit of bad practice, but the only way to fix it is to go through all of the unmanaged code and re-write it in order to handle being loaded into a managed block of code, which may turn out to be impossible if you're loading other libraries that are precompiled.
0
 

Author Comment

by:jes12345
ID: 24395274
Fantastic - I understand now:) Thank you very much for your advises and explanation. J
0
 
LVL 5

Expert Comment

by:burningmace
ID: 24395285
No problem, glad to be a help.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
API v SOA 8 63
Mobile apps - web/native or hybrid? 1 120
T-SQL and CLR parameter strings 9 63
Help with viewing image in ReportViewer 14 28
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question