How to use the concept of activation context in .NET to load unmanaged dlls?

132 views Asked by At

After some help and reading, I am sure the concept activation context can be used to solve my problem. In my application, I have to load an unmanaged dll c.dll by another managed dll b.dll, which is loaded by a.dll, which is loaded by app.exe. The sequence is that

app.exe -> a.dll (managed) -> b.dll (managed) -> c.dll (unmanaged)

I have the source of a.dll and b.dll, but app.exe and c.dll belongs to the third party. Now, regardless of whether a.exe loads other versions of c.dll or not, I have to load my own c.dll (my c.dll resides in a different folder with that carried by a.exe installation).

I used LoadLibraryEx, but that app.exe have loaded a different of c.dll before I can load my own c.dll. These two c.dll are same name with different versions. Since a different version of c.dll has been loaded, when I called LoadLibraryEx, my c.dll won't be loaded. I need a solution to solve this problem.

How does the concept activation context can be migrated to the .NET environment to solve this problem?

1

There are 1 answers

7
Kieran Foot On

I'm not sure what you mean by activation context.

If you want to load an unmanaged DLL in a managed environment you need to do so via pInvoke:

[DllImport("kernel32", SetLastError=true, CharSet = CharSet.Ansi)]
static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)]string lpFileName);

Also, you will need to free the handle stored in the IntPtr returned by LoadLibrary.