What is the underlying implementation of GetEntryAssembly in C#? And why it does return null when dll was loaded from an unmanaged application?
The MSDN documentation says:
The GetEntryAssembly method can return null when a managed assembly has been loaded from an unmanaged application. For example, if an unmanaged application creates an instance of a COM component written in C#, a call to the GetEntryAssembly method from the C# component returns null, because the entry point for the process was unmanaged code rather than a managed assembly.
I have the following setup:
Native executable (C++) -> Mixed mode assembly (C++/cli) -> Managed assembly (C#)
Managed assembly is optional, in mixed mode assembly one can call GetEntryAssembly and get null.
While debugging we've tracked to this call in C# source code:
[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
private static extern void GetEntryAssembly(ObjectHandleOnStack retAssembly);
We can see that the entry assembly should be the native executable. However it is not retrieved. I wonder what are the reasons for this? Shouldn't native to managed transition take care of that?
Edit
GetEntryAssembly
is used internally by .NET. We are just experiencing the side effect when it returns null
.
Reproducible with one line in Managed assembly:
System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForDomain ();
This will throw the following exception: System.IO.IsolatedStorage.IsolatedStorageException: Unable to determine the identity of domain.
The root cause for this exception seems to be the entry assembly being null.