Visual Studio - use the output of a native DLL project in a C# WinUI project

Peter Hull 0 Reputation points
2025-08-22T13:05:05.2833333+00:00

I have a solution with two projects, one is a C# WinUI3 project which uses a native code DLL in the other project via interop. My DLL itself requires some other DLLs which are provided by vcpkg.

How to I set up the solution/project files in VS2022 so that the DLL part is built first and then everything copied into the required place for the WinUI part?

At the moment I've got a .targets file like this

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Target Name="CopyDLLBinaries" BeforeTargets="GetCopyToOutputDirectoryItems">
      <ItemGroup>
        <PackageDLLBinaries Include="$(SolutionDir)$(Platform)\$(Configuration)\*.dll" />
      </ItemGroup>
        <Copy SourceFiles="@(PackageDLLBinaries)"
              DestinationFolder="$(OutputPath)AppX"
              SkipUnchangedFiles="true"
              OverwriteReadOnlyFiles="true"
        />
    </Target>
</Project>

but this seems unreliable; if I clean the project or change anything in the DLL project I might need to build the solution several times until it works in the debugger. Building the DLL seems reliable, as does building the WinUI exe but I find that the AppX final output directory sometimes is missing the DLLs that should be copied in there.

Is there an approved way to do this, it seems like it would be a common-ish requirement?

Developer technologies | Visual Studio | Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 91,066 Reputation points
    2025-08-23T06:52:28.4933333+00:00

    I have some similar Solutions on GitHub, with WinUI 3 C# project + C++ DLL or EXE project

    For example : https://github.com/castorix/WinUI3_PinToTaskbar

    with mainly, in .csproj :

     <Target Name="CopyPinDll" AfterTargets="Build">
        <Copy SourceFiles="$(SolutionDir)$(Configuration)\Pin.dll" DestinationFolder="$(OutputPath)" />
      </Target>
    
    0 comments No comments

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.