Search code examples
c#fody-costura

Costura not loading Native Dll


I am unable to get Costura to load a Native dll that my project needs to run. This is a full native dll so it is not a reference in the project.

I have added the dll to the costura32 folder in my project and set it as an embedded resource.

When I run the project I can see that costura has extracted the dll to %temp%\costura\1D5629B8D94FC3E9B53C7AB358A0E123\32\native.dll

The project is still unable to find the file with the error Unable to load DLL

When looking in procmon I can see that it looks for the file in the local folder then in %temp%\costura\1D5629B8D94FC3E9B53C7AB358A0E123\native.dll and cannot find it. It doesn't seem to be looking for it in the "32" folder.

I have tried several options in the config file Unmanaged32Assemblies, PreloadOrder but they all have the same result.

I cannot see what I am doing wrong here.


Solution

  • In my case I tried to access temp path for setting library path with below code and it worked.

       private bool SetupSevenZipLibrary()
        {
            string costuraExtractionPath = null;
            try
            {
               DirectoryInfo di = null;
    
                string costuraTempPath = Path.Combine(
                    Path.GetTempPath(),
                    "Costura" //ex: Costura
                );
    
                di = new DirectoryInfo(costuraTempPath);
                if (!di.Exists)
                    return false;
                costuraExtractionPath = di.GetDirectories().First().FullName;
           
                if (!Directory.Exists(costuraExtractionPath))
                    throw new Exception();
    
                string sevenZipPath = Path.Combine(
                    costuraExtractionPath, 
                    Environment.Is64BitProcess ? "64" : "32", "7z.dll"
                );
                if (!File.Exists(sevenZipPath))
                    throw new Exception();
    
                SevenZipBase.SetLibraryPath(sevenZipPath);
                return true;
            }
            catch { return false; }
        }