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

燕鹏 李 301 Reputation points
2022-05-26T07:48:37.837+00:00

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 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).

how does the concept activation context can be migrated to the .net environment to solve this problem?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,440 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,352 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,130 questions
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 40,861 Reputation points
    2022-05-26T16:39:11.913+00:00

    It should be possible but I think you will have to manually manage activation contexts.

    For example, in my proof of concept test a C# Winform app uses a class from a referenced C# Class Library. That class references a C++/CLI class library that loads a native DLL. The test code in the C++/CLI class library uses LoadLibrary to first load version 2.0 of the native DLL from one file system location. It specifies a path in the call to LoadLibrary. An exported function is called that indicates that version 2.0 of the DLL was used. Then the C++/CLI code manually creates and activates an activation context for its preferred DLL, version 1.0. LoadLibrary is called again without specifying a path. This time, the exported function from the preferred version 1.0 is used to resolve the export. The default activation context is then restored and LoadLibrary is called a third time without specifying a path and the exported function from version 2.0 is called again.

    205878-dbgmsg.png

    And here you can see that both DLLs are loaded in the process -

    205951-dlls.png

    3 people found this answer helpful.

0 additional answers

Sort by: Most helpful