Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

LoaderLock exception in VB.net

Posted on 2009-05-14
5
Medium Priority
?
769 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 2000 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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 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…
Suggested Courses

661 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