LoaderLock exception in VB.net

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..)

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.

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

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.

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…