none
How to load a managed C# DLL from a path instead of loading from GAC using CLR profiler? RRS feed

  • Question

  • The managed DLL can be loaded when it is in GAC using the public key token and assembly name.

    MetaDataAssemblyEmit->DefineAssemblyRef(pbPublicKeyOrToken, publicKeySize, assemblyName, &assemblyMetaData, NULL, 0, 0, &mdAssemblyRef);


    I want to load the DLL reference from a physical path instead of loading from the GAC.

    I tried the below code,

    LoadLibrary(L"C:\Packages\MyDotNetAssembly.dll");

    The above code loaded the DLL into the process, It is not loading the DLL into the CLR runtime.

    So DefineAssemblyRef could not find the DLL. Getting the below error,

    Could not load file or assembly 'MyDotNetAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8b6549a3d401ea66' or one of its dependencies. The system cannot find the file specified.

    How can I load the managed DLL into the current runtime or app domain, to make it work via C++ code?

    --Anbu--





    • Edited by anbu2388 Thursday, April 19, 2018 4:18 AM
    Wednesday, April 18, 2018 10:57 AM

All replies

  • Hi anbu2388,

    Thank you for posting here.

    For your question, if you want to edit the probing path, you could use a config file to set the path or AppDomainSetup.PrivateBinPath as suggested. But, if you want to do this dynamically (after the AppDomain has been created), you can use the assembly resolve event.  Hook up a handler that probes wherever you want via Assembly.LoadFrom.  These will be visible to the load context (because they originated from Load) and will keep you from tripping over loader context problems.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 23, 2018 5:47 AM
    Moderator
  • Hi Zang,

    I tried to load the assembly in Assembly Resolve event. Also I got the hook up and loaded the assembly in the app domain using Assembly.LoadFrom.

    But when I access a method from the assembly in the same app domain I got assembly not found error as below,


    Could not load file or assembly 'MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8b6549a3d401ea66' or one of its dependencies. The system cannot find the file specified.
    Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
    Running under executable  C:\WINDOWS\SysWOW64\inetsrv\w3wp.exe
    --- A detailed error log follows. 
    
    === Pre-bind state information ===
    LOG: DisplayName = MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8b6549a3d401ea66
     (Fully-specified)
    LOG: Appbase = file:///C:/WINDOWS/SysWOW64/inetsrv/
    LOG: Initial PrivatePath = NULL
    Calling assembly : (Unknown).
    ===
    LOG: This bind starts in default load context.
    LOG: No application configuration file found.
    LOG: Using host configuration file: C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet.config
    LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
    LOG: Post-policy reference: MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8b6549a3d401ea66
    LOG: Attempting download of new URL file:///C:/WINDOWS/SysWOW64/inetsrv/MyAssembly.DLL.
    LOG: Attempting download of new URL file:///C:/WINDOWS/SysWOW64/inetsrv/MyAssembly/MyAssembly.DLL.
    LOG: Attempting download of new URL file:///C:/WINDOWS/SysWOW64/inetsrv/MyAssembly.EXE.
    LOG: Attempting download of new URL file:///C:/WINDOWS/SysWOW64/inetsrv/MyAssembly/MyAssembly.EXE.

    It tries to start binding assembly in default load context. Please help me to load the assembly in default load context without putting in GAC.

    --Anbu




    • Edited by anbu2388 Thursday, April 26, 2018 4:32 AM
    Tuesday, April 24, 2018 6:37 AM
  • Why do u want to load without installing in GAC ..?
    Friday, May 4, 2018 10:09 AM
  • Hi Anbu,

    In .NET profiler are you loaded managed dll without GAC, Is this issue fixed ?

    Thanks in advance

    Wednesday, October 21, 2020 10:12 AM