2016-09-02 5 views
12

일부 CLR 클래스를 내보내는 관리되는 dll 어셈블리를 생성하는 혼합 모드 C++ 프로젝트가 있습니다 (Managed.dll이라고 함). 이 프로젝트는 네이티브 DLL을 사용하고 있습니다 (Native.dll이라고 함).Visual Studio 프로젝트에서 로컬 종속성을 로컬로 복사

Client.exe를 생성하는 다른 프로젝트에서 Managed.dll을 참조하면 Client.exe와 같은 폴더에 Native.dll을 수동으로 복사해야하는 것을 제외하고는 예상대로 작동합니다.

VS가 로컬로 (Client.exe의 bin 폴더에) 복사하도록 설득 할 수있는 방법이 있다면 Managed.dll뿐만 아니라 Native.dll도 가능합니까?

Native.dll을 매니페스트의 종속성 어셈블리로 포함하려고 시도했지만 도움이되지 않았습니다.

편집

는 Managed.dll는 재배포 조립 될 것입니다. 그것은 "C : \ Program Files ....."에있는 폴더에 설치됩니다. Visual Studio를 사용하는 개발자가 Managed.dll에 대한 참조를 추가하면 Native.dll도 프로젝트의 \ bin 폴더에 복사해야합니다.

1.Add 프로젝트의 자원으로 DLL을 :

+0

아마도 [제목] [Visual Studio에서 DLL의 종속성 복사] (http://stackoverflow.com/questions/1043913/copying-a-dlls-dependencies-in-visual-studio)가 도움이 될 수 있습니까? –

+0

나는 이것이 내가 필요한 것이 아닌가 걱정된다. Managed.dll 및 Native.dll을 배포하려고합니다. 라이브러리 사용자가 Managed.dll을 참조하면 프로젝트의 bin 폴더에 로컬로 복사 된 Native.dll이 필요합니다. – Trifon

답변

8

대상 폴더에 DLL을 복사 VS 말할 수있는 몇 가지 방법이 있습니다. dll이 더 새로운 경우 복사하도록 VS에게 알려주십시오.

2. dll 프로젝트를 참조하는 새 프로젝트를 추가하고 OutDir을 원하는 폴더로 설정하십시오. 이 프로젝트는 dll을 복사 만합니다. vcxproj 파일 메이크 6.Use vcxproj 파일

<ItemGroup> 
<CustomBuild Include="..\..\xxx.dll"> 
    <FileType>Document</FileType> 
    <Command> 
    call mkdir &quot;$(OutDir)&quot; 2&gt;nul &amp; 
    copy /Y &quot;..\..\xxx.dll&quot; &quot;$(OutDir)xxx.dll&quot; 
    </Command> 
    <Message>Copying xxx.dll to $(OutDir)\xxx.dll</Message> 
    <Outputs>$(OutDir)\xxx.dll</Outputs> 
</CustomBuild> 
</ItemGroup> 

에서

5.Use CustomBuild에서 PreBuildEvent 4.Use vcxproj 파일

<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> 
    <ClCompile> 
    </ClCompile> 
    <Link> 
    </Link> 
    <PostBuildEvent> 
    <Command> 
     echo off 
     mkdir "$(ProjectDir)..\..\bin\$(Configuration)\" 
     copy "$(OutDir)xxx.dll" "$(ProjectDir)..\..\lib\$(Configuration)\" 
     echo on 
    </Command> 
    </PostBuildEvent> 
</ItemDefinitionGroup> 

에서 PostBuildEvent 3.Use

makefile에 dll을 복사합니다. 및

7.Write 복사 작업을 박쥐 파일을 구축하고,에서와 박쥐 파일을 호출하는 nmake를 사용하여 3-6

도에서 DLL을 다운로드 할 수 있습니다 파이썬 등

8.Use 스크립트, 인터넷. 그리고 3-6에서와 같이 py 파일을 호출하십시오.

은 Gradle을 같은 도구도 도움이 될 수 있습니다 구축 9.Other

10.Make 그것을 NuGet 플러그인

11.Sometimes 난 그냥 방망이를 쓰고, 수동으로 방망이를 실행합니다.

업데이트 01 (자체 추출 DLL 예) : 관리 DLL의 자원

2.Add이 초기화() 메소드로

1.Add하면 네이티브 DLL

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Reflection; 
using System.Security.Cryptography; 
using System.Text; 
using System.Threading.Tasks; 

namespace DllSelfExtract 
{ 
    public class SelfExtract 
    { 
     public static void Init() 
     { 
      String managedDllPath = System.Reflection.Assembly.GetExecutingAssembly().CodeBase; 
      String nativeDllPath = managedDllPath.Replace("file:///", "").Replace("DllSelfExtract.DLL", "TestDll.dll"); 
      if(!File.Exists(nativeDllPath)) 
      { 
       Stream dllIn = Assembly.GetExecutingAssembly().GetManifestResourceStream("DllSelfExtract.TestDll.dll"); 
       if (dllIn == null) return; 

       using (Stream outFile = File.Create(nativeDllPath)) 
       { 
        const int sz = 4096; 
        byte[] buf = new byte[sz]; 
        while (true) 
        { 
         int nRead = dllIn.Read(buf, 0, sz); 
         if (nRead < 1) 
          break; 
         outFile.Write(buf, 0, nRead); 
        } 
       } 
      } 

      //LoadLibrary Here 
     } 
    } 
} 

3 .

1.Create 새로운 NuGet 프로젝트

2.Place가 관리 : 관리되는 DLL을 사용하는 프로젝트에서 먼저 초기화() 메소드

SelfExtract.Init(); 

업데이트 02 (NuGet 예)를 호출/lib 디렉토리의 어셈블리

3. 관리되지 않는 공유 라이브러리 및 관련 파일을/build 하위 디렉토리에 배치하고 관리되지 않는 모든 * .dll의 이름을 * .dl_

로 변경합니다 10

4.Add 다음과 같은 내용을 같은과/빌드 하위 디렉토리에서 사용자 정의 .targets 파일 :

<?xml version="1.0" encoding="utf-8"?> 
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
    <ItemGroup> 
    <AvailableItemName Include="NativeBinary" /> 
    </ItemGroup> 
    <ItemGroup> 
    <NativeBinary Include="$(MSBuildThisFileDirectory)*"> 
     <TargetPath></TargetPath> 
    </NativeBinary> 
    </ItemGroup> 
    <PropertyGroup> 
    <PrepareForRunDependsOn> 
     $(PrepareForRunDependsOn); 
     CopyNativeBinaries 
    </PrepareForRunDependsOn> 
    </PropertyGroup> 
    <Target Name="CopyNativeBinaries" DependsOnTargets="CopyFilesToOutputDirectory"> 
    <Copy SourceFiles="@(NativeBinary)" 
      DestinationFiles="@(NativeBinary->'$(OutDir)\%(TargetPath)\%(Filename).dll')" 
      Condition="'%(Extension)'=='.dl_'"> 
     <Output TaskParameter="DestinationFiles" ItemName="FileWrites" /> 
    </Copy> 
    <Copy SourceFiles="@(NativeBinary)" 
      DestinationFiles="@(NativeBinary->'$(OutDir)\%(TargetPath)\%(Filename).%(Extension)')" 
      Condition="'%(Extension)'!='.dl_'"> 
     <Output TaskParameter="DestinationFiles" ItemName="FileWrites" /> 
    </Copy> 
    </Target> 
</Project> 

5.Add이

<files> 
    <file src="lib\" target="lib" /> 
    <file src="tools\" target="tools" /> 
    <file src="content\" target="content" /> 
    <file src="build\" target="build" /> 
</files> 

6 Package.nuspec에서 빌드 폴더에 대한 규칙을 구축 패키지를 빌드하십시오.

7. 다른 C# 프로젝트에서이 NuGet 패키지를 추가하십시오.

+0

답변 해 주셔서 감사합니다. 이러한 모든 방법은 유효하고 유용하며 MY 프로젝트에서 사용할 수 있습니다. 문제는 Visual Studio 프로젝트가 아닌 Managed.dll과 Native.dll로 구성된 다중 파일 어셈블리를 배포하려고한다는 것입니다. – Trifon

+0

@Trifon Naged.dll을 Managed.dll의 리소스로 추가하는 것은 어떻습니까? Managed.dll에서 Native.dll을로드하기 전에 Managed.dll에서 Native를 추출하지 않으면 Native.dll이 올바른 위치에 있는지 여부를 감지합니까? 하지만 인터넷에서 다운로드하는 것이 더 좋을 것이라고 생각합니다. – neohope

+0

네,이게 내가 마음에 가지고있는 해결책입니다. [link] (http://stackoverflow.com/a/768429/3260013) – Trifon

5

링커 속성에서 /ASSEMBLYLINKRESOURCE 옵션을 사용하는 것이 가장 간단한 해결책 인 것 같습니다. Visual Studio에서 네이티브 DLL을 어셈블리의 일부로 간주합니다.

  • 오른쪽 프로젝트 이름을 클릭하고 : 또한, Microsoft에서 제공하는 설명서에 따라, 네이티브 DLL은 비주얼 C++ 프로젝트에서이 링커 옵션을 설정하려면 캐시

    전역 어셈블리에 설치할 수 있습니다 속성

  • 입력 속성 페이지에서
  • 총회 링크 Resourc 찾기 폴더 링커를 선택 e 옵션
  • 네이티브 어셈블리의 파일 이름을 씁니다. 는 MyNative.dll

당신은 포스트 출력 폴더에 네이티브 DLL을 복사하는 이벤트를 구축해야합니다.

다른 Visual Project에서 관리되는 어셈블리를 참조하면 네이티브 dll이 관리되는 어셈블리와 함께/bin 폴더에 강제로 복사됩니다.

+0

이것을 읽으십시오, 당신은 nuget 포장을 시도 할 수있다. http://stackoverflow.com/questions/19478775/add-native-files-from-nuget-package-to-project-output-directory – neohope

관련 문제