What is the expected behavior of LoadLibrary() when called from an unmanaged process (native C++) on a managed assembly (C#)

StackOverflow https://stackoverflow.com/questions/17983516

Question

I've added a hosting interface to a native C++ application which instantiates the CLR, creates a custom appDomainManager, and provides calls to load managed assemblies into the native process. In my native C++ LoadDLL() function I was expecting to be able to test the incoming dll for .net vs C++ by calling LoadLibrary(dllPath) which I assumed would return failure (NULL) for managed assemblies, but I'm finding that it is returning a handle instead (this is with no CLR currently running in the unmanaged process). Is this normal behavior for an unmanaged LoadLibrary() call on a managed assembly?

I'm not sure I understand how LoadLibrary can even find a proper entry point to test in a managed assembly. I know (one possible) way to solve the problem, and the way I'm planning to implement, is simply to use the CLR instance to access the .net reflection APIs and check if the DLL is managed there first...but I'm puzzled by the fact that LoadLibrary() isn't returning failure and I'd like to understand what I'm missing here. Is the behavior undefined, should it always return a handle, or does it depend on the configuration of the managed assembly? Any link to references is appreciated.

Edit:

Question answered in comments, closing.

Was it helpful?

Solution

No, they won't do anything, the dll/exe of managed program is just a stub(stub means template but essential) program that will attempt to call mscoree, which is the .Net runtime executor, also called the shim, because it would attempt to select the version of .net framework(and the native function name is called _CorExeMain), then the shim itself will bootstrap the .Net framework, and launch a clr instance, then start the procession of IL codes inside the PE, besides that it's just some metadata generated by the CIL compiler, such as resource pools(string pool, constant values), function prototypes, inheritance, and generics types.

For more information, take a deep look at this: http://blog.vuscode.com/malovicn/archive/2007/12/25/net-foundations-net-execution-model.aspx , You won't be disappointed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top