How to embed 32/64bit dll's with same name in

2019-08-11 09:23发布

问题:

I'm working on a mixed mode exe that performs 7z extraction. I'm using SevenZipExtractor and need to specify the 7z lib path, so I added the 7z.Libs package which has both 32/64bit dlls and I want to embed them in my exe.

How do I make the build embed the same name dlls with different name? /bin/x86/7zxa.dll -> 7zxa_x86.dll /bin/x64/7zxa.dll -> 7zxa_x64.dll

So I can do something like this:

SevenZipExtractor.SetLibraryPath(util.is_64bit_process ? "7zxa_x64.dll" : "7zxa_x86.dll");

I'm using Fody/Costura to load other dlls, but can't figure out if and how to use it to do what I want here.

Also I really don't want to forced to add the dlls directly (with different names) to my project code dirs (repository issues).

Edit I've added the dll's as links to the project:

<ItemGroup>
    <EmbeddedResource Include="..\packages\7z.Libs.15.12.0\bin\x86\7zxa.dll">
      <Link>7z\x86\7zxa.dll</Link>
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </EmbeddedResource>
    <EmbeddedResource Include="..\packages\7z.Libs.15.12.0\bin\x64\7zxa.dll">
      <Link>7z\x64\7zxa.dll</Link>
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </EmbeddedResource>
</ItemGroup>

The dll's are indeed embedded into the exe:

ystem.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames()
        [0] "xxx.g.resources"   string
        [1] "xxx.Properties.Resources.resources"    string
        [2] "xxx.version"   string
        [3] "xxx._7z.x86.7zxa.dll"  string
        [4] "xxx._7z.x64.7zxa.dll"  string
        [5] "costura.asyncbridge.net35.dll" string
        [6] "costura.hardcodet.wpf.taskbarnotification.dll" string
        [7] "costura.newtonsoft.json.dll"   string
        [8] "costura.sevenzipsharp.dll" string
        [9] "costura.system.threading.dll"  string

However I'm still not able to get the SevenZip lib to load the dll:

SevenZipExtractor.SetLibraryPath(util.is_64bit_process ? @"7z\x64\7zxa.dll" : @"7z\x86\7zxa.dll");

also tried @"_7z.x64.7zxa.dll" as appears in the Assemblies list, no luck.

Any suggestions?

回答1:

You can't add two files of the same name to the zip file if they are in the same folder - but you can add folders to the zip file.

So if you create two folders in the zip file called "x86" and "x64" (say) then you can add the dll to relevant dll and your code will become:

SevenZipExtractor.SetLibraryPath(util.is_64bit_process ? @"x64/7zxa.dll" : @"x86/7zxa.dll");


标签: c# .net dll