Solved

LoaderLock exception in VB.net

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

746 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

12 Experts available now in Live!

Get 1:1 Help Now