Question:
In "MS Visual Studio 2010" and .NET 4.0 Framework my application crashes when making call to "NIST library" DLL. What should I do?

Answer:
Important notes for using "NIST library" in "MS Visual Studio 2010".

1. "NIST library" is a native 32-bit Windows DLL.
In IDE of MS "Visual Studio 2010" you need to set "targeted platform" as "x86".

2. You need to "run as administrator" the IDE compiler and application.

3. .NET 2.0+ assumes that DLL calling convention is Cdecl, while .NET 4.0 does not.
You need to explicitly set calling convention to Cdecl when using "NIST library" in .NET 4.0.

As for example:

Visual C# .NET
NET 2.0+ version:

[DllImport("NIST_library.dll")]
public static extern int _RegisterNIST();

NET 4.0 version:	

[DllImport("NIST_library.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern Int32 _RegisterNIST();


Visual Basic .NET
NET 4.0 version:

<DllImport("NIST_library.dll", CallingConvention:=CallingConvention.Cdecl, CharSet:=CharSet.Ansi)> _
Shared Function _RegisterNIST() As Int32
End Function


4. When trying to run application in "MS Visual Studio 2010" you might receive the error message
"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".

The solution:
In IDE go to the menu "Debug", then go to the menu "Exceptions", and then expand "Managed Debugging Assistants", and uncheck "Thrown" for "LoaderLock".


5. If application is built targeting version 4.0 of the .NET Framework, the call to the unmanaged DLL will always fail with a StackOverflowException under the debugger.
If application is built targeting version 3.5 (or lower) of the .NET Framework, it works correctly.

The solution:
As a workaround solution is to run the application in IDE without debugger (Ctrl-F5), then application will work correctly. Also you can build application and run it outside of IDE.
This is a known bug of "MS VS2010 compiler". Please check out for updates and hotfixes from Microsoft for the latest solution to this problem. 

6. Please also read:
http://www.cognaxon.com/index.php?page=wsqlib_NETnotes
