ArrayList marshaling and useLegacyV2RuntimeActivationPolicy
See the question and my original answer on StackOverflowThis is because ArrayList is, from a COM point a view, built on old versions of .NET Framework.
ArrayList COM registration in 64-bit, in mscorlib.dll built on .NET Framework 2:
ArrayList COM registration in 32-bit (on a 64-bit system), in mscorlib.dll built on .NET Framework 1:
So, if you build your .dll with .NET Framework 4, and call it from VB6 (which is 32-bit), you'll mix mscorlib in different versions (4 and 1 here) in the same process, which creates issues.
As stated in official documentation on <supportedRuntime>
element
If your application uses legacy activation paths [...] and you want those paths to activate version 4 of the CLR instead of an earlier version, or if your application is built with the .NET Framework 4 but has a dependency on a mixed-mode assembly built with an earlier version of the .NET Framework, it is not sufficient to specify the .NET Framework 4 in the list of supported runtimes. In addition, in the element in your configuration file, you must set the
useLegacyV2RuntimeActivationPolicy
attribute totrue
. [...]
This is a basically "magic" switch, as explained here: What is useLegacyV2RuntimeActivationPolicy for?
The
useLegacyV2RuntimeActivationPolicy
attribute basically lets you say, “I have some dependencies on the legacy shim APIs. Please make them work the way they used to with respect to the chosen runtime.
Also from the same link:
Ultimately, this attribute has to do with the behavior of the “legacy shim APIs”. You can think of these as encompassing several categories of CLR activation:
[...]
Pre-v4 COM activation – This includes CoCreateInstance of a CLSID (or type identifier) whose latest registration is against a pre-v4 runtime version. Note this includes both the “new” operator on such a co-class from managed code, or the result of Activator.CreateInstance against a type created by Type.GetTypeFromCLSID on such a CLSID.
Pre-v4 IJW (mixed mode) activation – For example, calling into a native export on such an assembly
Native activation of a native runtime-provided COM CLSID – Such as CoCreateInstance on ICLRRuntimeHost’s CLSID
Native activation of a managed framework CLSID – Such as CoCreateInstance on System.ArrayList’s CLSID (extremely rare)