Loading

UWP(二)调用Win32程序

一、如何构建Win32程序

打开csproj文件,使用如下代码添加引用(Reference)。注意,如果指定位置不存在,请查找正确的位置后添加引用,目前已知安装VS如果不是默认C盘,会出现类似报错。

<Reference Include="Windows">
  <HintPath>$(SystemDrive)\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.16299.0\Windows.winmd</HintPath>
</Reference>
    
<Reference Include="System.Runtime.WindowsRuntime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>$(SystemDrive)\Windows\Microsoft.NET\Framework\v4.0.30319\System.Runtime.WindowsRuntime.dll</HintPath>
</Reference>

二、如何构建UWP工程?

参考 Post

  1. Create your executable .exe file (for example console application)

  2. Copy .exe file to your UWP Application start up folder (for example: Assets folder)

    // 作者注:直接添加编译事件来保证复制
    xcopy /y /s "$(SolutionDir)PrintAppBackendService\bin\$(ConfigurationName)\PrintAppBackendService.exe" "$(TargetDir)\AppX\"
    
  3. In UWP App Solution Explorer, add reference to "Windows Desktop Extensions For The UWP v10.0.14393.0"(or higher) under "References > Universal Windows > Extensions".

    注意,如果列表中没有“Windows Desktop Extensions For The UWP”,则可能是没有安装对应安装包,或者安装包安装位置不是默认位置,参考这里解决。

  4. In UWP App Solution Explorer, open Package .appxmanifest xml file (right click on Package.appxmanifest file --> view code). add these namespace to Package tag.

    xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" 
    xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"
    

    And then, add this extension under Application tag.

    <Extensions>
        <desktop:Extension Category="windows.fullTrustProcess" Executable="Assets\YourExecutableFileName.exe" />
    </Extensions>
    

    And then, add this code into your Capabilities tag.

    <rescap:Capability Name="runFullTrust" />
    

    This step mean: talk to compiler to know that it shuld trust your .exe file in the Assets\YourExecutableFileName.exe location.

  5. In your UWP Application whenever you want to launch the .exe file, you need to execute this code:

    await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
    
  6. reference: Great answer

  7. 作者注 如果需要使用AppServiceConnection,需要在Package.appxmanifest的声明中,选择应用服务声明,点击添加,输入服务名称,名称为win32程序实例化AppServiceConnectionAppServiceName。否则会报错:AppServiceUnavailable

    var connection = new AppServiceConnection
    {
        AppServiceName    = "CommunicationService",
        PackageFamilyName = Windows.ApplicationModel.Package.Current.Id.FamilyName
    };
    

三、Samples

微软提供了 DesktopBridgeToUWP-Samples

Desktop Conversion Extensions is a bridge that enables you to convert your classic desktop application (like Win32, Windows Forms, and WPF) or game to a Universal Windows Platform (UWP) app or game. After conversion, your classic desktop app is packaged, serviced, and deployed in the form of a UWP app package (an .appx or an .appxbundle) targeting Windows 10.

posted @ 2021-02-03 18:04  NLNet  阅读(281)  评论(0编辑  收藏  举报