How to Configure Library Files to Run in a Subfolder?

fatih uyanık 100 Reputation points
2024-10-06T10:13:18.32+00:00

Hello

There is a library named TolkDotNet.dll in a c# wpf project. This library references various dll files in the x86 and x64 folders. To get rid of the file confusion, I placed them in the Lib folder as TolkDotNet, x86, x64. Then I showed the Lib/TolkDotNet.dll reference in the project dependencies. But the application looks for these dll files and folders in the project output directory. Therefore, the project is causing problems. How can I configure these references to run in the Lib folder?

Thanks.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,788 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.
11,054 questions
{count} votes

3 answers

Sort by: Most helpful
  1. KOZ6.0 6,590 Reputation points
    2024-10-06T17:45:45.84+00:00

    To change the path that references an assembly, see https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/specify-assembly-location.

    Use LoadLibrary to switch to a native dll at runtime.

    using System;
    using System.IO;
    using System.Runtime.InteropServices;
    
    internal class Program
    {
        static Program() {
            var pathToDll = Environment.Is64BitProcess ? @".\x64" : @".\x86";
            LoadLibrary(Path.Combine(pathToDll, "YourNative.dll"));
        }
    
        [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Auto)]
        static extern IntPtr LoadLibrary(string lpFileName);
    
        [DllImport("YourNative.dll")]
        internal static extern IntPtr YourNativeFunction();
    
        static void Main(string[] args) {
            Console.WriteLine($"{YourNativeFunction()}");
            Console.ReadKey();
        }
    }
    

  2. Digvijay Chauhan 1 Reputation point
    2024-10-07T12:11:38.1566667+00:00

    Hi,

    the above answer would work if the library mentioned by OP (TolkDotNet.dll) is unmanaged/native winPE that exports symbols. I suspect that is not the case (I did not bother to google since the name says it all) - so this TolkDotNet.dll seems to use other native libraries using interop and would load appropriate runtime based on the platform being invoked on.

    that said - you can consider a commercial/free obfuscator tool (I have experience with redgate SmartAssembly) and it allowed me to bundle all custom dependencies in a single executable file/dll with the dependencies embedded as encrypted binary blobs within the main assembly.

    i hope that helps you assess your options -

    1. The x86/64 folder needs to exist wherever the tolkdotnet.dll is , and it can not be the GAC so
    2. add a manifest file and specify the folder you would like to load this DLL from - it can be any folder on the file system - even outside your solution- however that would create a deployment chore for you to ensure that you package and deploy the same configuration to a target machine (maybe it’s not even required in OPs scenario so I would leave it at that)
    0 comments No comments

  3. fatih uyanık 100 Reputation points
    2024-10-09T06:29:52.6733333+00:00

    Hello

    No matter what I did, I couldn't use the dll file in the subdirectory properly at runtime. The error still persists. Could you help me by trying the TolkDotNet library in a WPF project?

    Thank you.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.