Solved

LoaderLock exception in VB.net

Posted on 2009-05-14
5
753 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

896 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now