Automaticaly copy native dll to the bin folder of referencing project in Visual Studio

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 05:34:50

问题


I have some native dll which must be copied in the bin folder with platform conditions. And I want them to be copied to the bin folder of projects referencing this project.

If I set the build action to Content, they will be copied to the bin folder but with the folder structure kept so they will not be in the bin folder but in sub folders. So when running the program, it will not be able to resolve the dll because they are in a subfolder.

For example if I have this code in the project file

<Choose>
    <When Condition=" '$(Platform)'=='x86' ">
      <ItemGroup>
        <Content Include="nativedll\somelib\x86\somelib.dll">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </Content>
      </ItemGroup>
    </When>
    <When Condition=" '$(Platform)'=='x64' ">
      <ItemGroup>
        <Content Include="nativedll\somelib\x64\somelib.dll">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </Content>
      </ItemGroup>
    </When>
  </Choose>

The dll will be in the folder bin\nativedll\somelib\x86\somelib.dll in the case of x86.

So I tried to use Post build script

<PostBuildEvent>

            IF "$(Platform)" == "x86" (
            xcopy /s /y "$(ProjectDir)\nativedll\somelib\x86" "$(TargetDir)"
            )
            IF "$(Platform)" == "x64" (
            xcopy /s /y "$(ProjectDir)\nativedll\somelib\x64" "$(TargetDir)"
            )
</PostBuildEvent>

But the dll are copied in the bin folder of the project but not in the bin folder of projects referencing it.

So my solution right now is to add a post build script in all projects using this one.

Is there a better way to do this in Visual Studio?


回答1:


Try using this for each file that has to be copied (csproj file - make an item group):

<ItemGroup>
   <ContentWithTargetPath Include="mySourcePath\myFile.dll">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    <TargetPath>myFile.dll</TargetPath>
 </ContentWithTargetPath >
</ItemGroup>

The referencing project should also contain the copied files.


The following may help as well:

Copy native dependencies locally in a Visual Studio project

Add native files from NuGet package to project output directory @kjbartel




回答2:


I give the solution I used before I got @dajuric anwser. I prefer dajuric solution because it doesn't involve code looking in an other folder.

I used conditional Content in the csproj.

<When Condition=" '$(Platform)'=='x86' ">
      <ItemGroup>
        <Content Include="nativedll\somelib\x86\somelib.dll">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </Content>
      </ItemGroup>
    </When>
//same for x64
    ...

Then before initialising the library using the native dll, I added the folder to the dll lookup folder list with kernel32 SetDllDirectory.

        [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SetDllDirectory(string lpPathName);

 SetDllDirectory(@".\nativedll\somelib\x"+ (Environment.Is64BitProcess ? "64" : "32"));


来源:https://stackoverflow.com/questions/46361127/automaticaly-copy-native-dll-to-the-bin-folder-of-referencing-project-in-visual

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!