Home
%3CLINGO-SUB%20id%3D%22lingo-sub-723207%22%20slang%3D%22en-US%22%3EGet%20a%20list%20of%20virtual%20serial%20COM%20ports%20from%20UWP%20app%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-723207%22%20slang%3D%22en-US%22%3E%3CP%3EAt%20previous%20article%2C%20I%20wrote%20a%20limitation%20to%20use%20System.IO.Ports.SerialPort%20class%20on%20UWP.%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fforums%2Fpostpage%2Fboard-id%2FWindowsDevAppConsult%22%20target%3D%22_self%22%3EHow%20to%20access%20virtual%20COM%20port%20from%20UWP%20app%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3EBut%2C%20there%20is%20a%20limitation.%20SerialPort%20class%20on%20UWP%20can't%20list%20ports%20using%20GetPortNames%20method.%20%3CBR%20%2F%3EIf%20the%20method%20was%20used%2C%20then%20PlatformNotSupportedException%20would%20be%20occurred.%3C%2FPRE%3E%0A%3CP%3EGetPortNames%20method%20can't%20call%20on%20UWP.%20But%2C%20the%20method%20is%20available%20on%20.NET%20Framework.%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYes%2C%20UWP%20app%20can%20launch%20a%20full%20trust%20process%20that%20is%20in%20same%20package%2C%20and%20then%20UWP%20can%20communicate%20to%20the%20full%20trust%20process.%3C%2FP%3E%0A%3CP%3EAt%20DesktopBridgeToUWP-Samples%20repository%2C%20there%20is%20a%20sample%20program%20about%20this.%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2FDesktopBridgeToUWP-Samples%2Ftree%2Fmaster%2FSamples%2FAppServiceBridgeSample%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EAppService%20Bridge%20Sample%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20thought%20that%20using%20GetPortNames%20method%20is%20good%20example.%20So%2C%20I%20created%20a%20sample%20program%20that%20is%20changed%20previous%20article's%20sample.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%224%22%3E%3CSTRONG%3EKey%20technologies%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EWindows%20Application%20Package%20Project(MSIX)%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EFirst%2C%20we%20can%20create%20a%20msix%20package%20file%20that%20includes%20a%20UWP%20app%20and%20a%20Win32%20app%20using%20Windows%20Application%20Package%20Project.%3C%2FP%3E%0A%3CP%3EThe%20following%20picture%20is%20solution%20explorer%20of%20the%20sample%20app.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20550px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F120808i8AFB304C4F1D694F%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-27%20125811.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-27%20125811.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EVirtualCOMPortApp%20(Universal%20Windows)%3CBR%20%2F%3EThis%20is%20a%20UWP%20app%20project.%20It%20communicates%20to%20VirtualCOMPortApp.Win32%20to%20call%20GetPortNames%20method.%3C%2FLI%3E%0A%3CLI%3EVirtualCOMPortApp.Package%3CBR%20%2F%3EThis%20is%20a%20Windows%20Application%20Package%20Project.%20This%20is%20to%20create%20a%20msix%20package%20file%20that%20includes%20the%20UWP%20app%20and%20the%20Win32%20app.%3C%2FLI%3E%0A%3CLI%3EVirtualCOMPortApp.Win32%3CBR%20%2F%3EThis%20is%20a%20Console%20App(.NET%20Framework).%20This%20is%20to%20call%20GetPortNames%20method.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSTRONG%3EApp%20Services%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EApp%20Services(It%20is%20not%20Azure%20service!)%20is%20one%20of%20UWP%20features%20to%20provide%20functions%20to%20other%20apps%20from%20UWP.%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fwindows%2Fuwp%2Flaunch-resume%2Fapp-services%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EUse%20app%20services%20and%20extensions%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20sample%20app%2C%20the%20feature%20is%20used%20to%20communicate%20between%20the%20UWP%20app%20and%20the%20Win32%20app.%3C%2FP%3E%0A%3CP%3EUWP%20app%20side%20is%20hosting%20App%20Service%2C%20and%20Win32%20app%20connect%20to%20it%2C%20and%20then%20they%20can%20send%20%2F%20receive%20messages%20together%20using%20AppServiceConnection%20class.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOf%20course%2C%20you%20can%20choice%20other%20methods%20to%20communicate%20between%20UWP%20app%20and%20Win32%20app%20like%20ApplicationData.Current.LocalSettings.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EUsing%20Windows%2010%20APIs%20from%20.NET%20Framework%20app%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EYou%20can%20use%20Windows%2010%20APIs%20from%20.NET%20Framework%20app.%20It%20is%20written%20on%20following%20document%3A%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fwindows%2Fapps%2Fdesktop%2Fmodernize%2Fdesktop-to-uwp-enhance%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3ECall%20UWP%20APIs%20in%20desktop%20apps%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThere%20are%20a%20few%20steps%2C%20however%20we%20can%20do%20that%20more%20easier%20using%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fwww.nuget.org%2Fpackages%2FMicrosoft.Windows.SDK.Contracts%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EMicrosoft.Windows.SDK.Contracts%20NuGet%20package%3C%2FA%3E.%20In%20this%20sample%2C%20I'm%20using%20the%20package.%3C%2FP%3E%0A%3CP%3EThere%20is%20just%20one%20step.%20it%20is%20to%20add%20the%20package%20to%20.NET%20Framework%20project.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ELaunch%20full%20trues%20process%20from%20UWP%20app%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EThere%20is%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fuwp%2Fapi%2Fwindows.applicationmodel.fulltrustprocesslauncher%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EFullTrustProcessLauncher%20Class%3C%2FA%3E%20that%20is%20to%20launch%20full%20trust%20win32%20app%20from%20UWP%20app.%20It%20is%20provided%20through%20Windows%20Desktop%20Extension%20SDK.%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20want%20to%20use%20it%2C%20please%20add%20the%20reference%20to%20UWP%20project.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F120809i9916B7F8DE31BDAA%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-27%20131942.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-27%20131942.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%224%22%3E%3CSTRONG%3ELet's%20get%20list%20of%20port%20names.%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EBase%20of%20sample%20program%20is%20a%20sample%20program%20of%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2FWindows-Dev-AppConsult%2FHow-to-access-virtual-COM-port-from-UWP-app%2Fba-p%2F720864%22%20target%3D%22_self%22%3Ea%20previous%20article%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3EThe%20repository%20is%20here%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Frunceel%2FVirtualCOMPortApp%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Frunceel%2FVirtualCOMPortApp%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20added%20two%20projects%20to%20the%20solution.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20556px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F121848i6FD66A364FF9C347%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-07-02%20143937.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-07-02%20143937.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EVirtualCOMPortApp.Package%20project%3CBR%20%2F%3EThis%20is%20a%20Windows%20Application%20Package%20project.%20I%20added%20VirtualCOMPortApp%20project%20and%20VirtualCOMPortApp.Win32%20project%20to%20Applications%20node.%20And%20then%20set%20VirtualCOMPortApp%20as%20entry%20point.%3C%2FLI%3E%0A%3CLI%3EVirtualCOMPortApp.Win32%20project%3CBR%20%2F%3EThis%20is%20a%20Console%20App(.NET%20Framework)%20project.%20I%20added%20Microsoft.Windows.SDK.Contracts%20NuGet%20package%20to%20this.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3ETo%20launch%20the%20win32%20app%20as%20full%20trust%20process%2C%20I%20added%20desktop%3AExtension%20tag%20to%20Package.appxmanifest%20of%3CFONT%20color%3D%22%23FF0000%22%3E%3CSTRONG%3E%20VirtualCOMPortApp.Package%20project%3C%2FSTRONG%3E%3CFONT%20color%3D%22%23000000%22%3E%2C%20like%20below%3A%3C%2FFONT%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3E%3CEXTENSIONS%3E%0A%20%20%3CEXTENSION%20category%3D%22windows.fullTrustProcess%22%20executable%3D%22VirtualCOMPortApp.Win32%5CVirtualCOMPortApp.Win32.exe%22%3E%3C%2FEXTENSION%3E%0A%3C%2FEXTENSIONS%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%3CFONT%20color%3D%22%23FF0000%22%3E%3CFONT%20color%3D%22%23000000%22%3EAnd%20to%20use%20app%20service%2C%20I%20added%20an%20uap%3AExtension%20tag%20to%20Package.appxmanifest%20of%20%3CFONT%20color%3D%22%23FF0000%22%3E%3CSTRONG%3EVirtualCOMPortApp.Package%20project%3C%2FSTRONG%3E%3C%2FFONT%3E%2C%20like%20below%3A%3C%2FFONT%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3E%3CEXTENSION%20category%3D%22windows.appService%22%3E%0A%20%20%3CAPPSERVICE%20name%3D%22InProcessAppService%22%3E%3C%2FAPPSERVICE%3E%0A%3C%2FEXTENSION%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EAdd%20implementation%20to%20get%20port%20names%20to%20VirtualCOMPortApp.Win32%2C%20and%20communicate%20to%20UWP%20process%20using%20AppServiceConnection.%3C%2FP%3E%0A%3CP%3ETo%20connect%20the%20app%20service%3A%3C%2FP%3E%0A%3CPRE%3Evar%20conn%20%3D%20new%20AppServiceConnection()%3B%0Aconn.PackageFamilyName%20%3D%20Package.Current.Id.FamilyName%3B%0Aconn.AppServiceName%20%3D%20%22InProcessAppService%22%3B%0Aconn.RequestReceived%20%2B%3D%20Conn_RequestReceived%3B%0Aconn.ServiceClosed%20%2B%3D%20Conn_ServiceClosed%3B%0Aawait%20conn.OpenAsync()%3B%3C%2FPRE%3E%0A%3CP%3EImplements%20RequestReceived%20event%20handler%20to%20communicate%20to%20UWP%20process%3A%3C%2FP%3E%0A%3CPRE%3Eprivate%20static%20async%20void%20Conn_RequestReceived(AppServiceConnection%20sender%2C%20AppServiceRequestReceivedEventArgs%20args)%0A%7B%0A%20%20%20%20var%20d%20%3D%20args.GetDeferral()%3B%0A%20%20%20%20var%20requestMessage%20%3D%20args.Request.Message%3B%0A%20%20%20%20if%20(requestMessage.TryGetValue(%22command%22%2C%20out%20var%20commandName))%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20switch((string)commandName)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20case%20%22GetPortNames%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20await%20args.Request.SendResponseAsync(new%20ValueSet%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5B%22portNames%22%5D%20%3D%20JsonConvert.SerializeObject(SerialPort.GetPortNames())%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20default%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20noop%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20d.Complete()%3B%0A%7D%3C%2FPRE%3E%0A%3CP%3EAnd%20then%20I%20created%20a%20wrapper%20class%20to%20call%20the%20full%20truest%20process%20from%20UWP%2C%20like%20below%3A%3C%2FP%3E%0A%3CPRE%3E...%0Anamespace%20VirtualCOMPortApp%0A%7B%0A%20%20%20%20public%20static%20class%20SerialPortService%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20private%20static%20AppServiceConnection%20_appServiceConnection%3B%0A%20%20%20%20%20%20%20%20private%20static%20BackgroundTaskDeferral%20_backgroundTaskDeferral%3B%0A%20%20%20%20%20%20%20%20private%20static%20readonly%20TaskCompletionSource%26lt%3Bobject%26gt%3B%20_initialized%20%3D%20new%20TaskCompletionSource%26lt%3Bobject%26gt%3B()%3B%0A%0A%20%20%20%20%20%20%20%20private%20static%20Task%20WaitForInitializedAsync()%20%3D%26gt%3B%20_initialized.Task%3B%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20launch%20the%20full%20trust%20process%0A%20%20%20%20%20%20%20%20public%20static%20Task%20LaunchWin32AppAsync()%20%3D%26gt%3B%20FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync().AsTask()%3B%0A%20%20%20%20%20%20%20%20%2F%2F%20set%20up%20app%20service%0A%20%20%20%20%20%20%20%20public%20static%20void%20AcceptAppServiceConnection(IBackgroundTaskInstance%20instance)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20instance.Canceled%20%2B%3D%20AppServiceCanceled%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20appService%20%3D%20(AppServiceTriggerDetails)instance.TriggerDetails%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20_backgroundTaskDeferral%20%3D%20instance.GetDeferral()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20_appServiceConnection%20%3D%20appService.AppServiceConnection%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20_appServiceConnection.ServiceClosed%20%2B%3D%20AppServiceConnection_ServiceClosed%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20_initialized.SetResult(null)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20get%20port%20names%20using%20AppServiceConnection%0A%20%20%20%20%20%20%20%20public%20static%20async%20Task%26lt%3Bstring%5B%5D%26gt%3B%20GetPortNamesAsync()%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20await%20WaitForInitializedAsync()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20response%20%3D%20await%20_appServiceConnection.SendMessageAsync(new%20ValueSet%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5B%22command%22%5D%20%3D%20%22GetPortNames%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(response.Status%20!%3D%20AppServiceResponseStatus.Success)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20throw%20new%20InvalidOperationException(%24%22%7Bresponse.Status%7D%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20JsonConvert.DeserializeObject%26lt%3Bstring%5B%5D%26gt%3B(response.Message%5B%22portNames%22%5D%20as%20string%20%3F%3F%20%22%5B%5D%22)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20closing%20process%0A%20%20%20%20%20%20%20%20private%20static%20void%20AppServiceCanceled(IBackgroundTaskInstance%20sender%2C%20BackgroundTaskCancellationReason%20reason)%20%3D%26gt%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20_backgroundTaskDeferral.Complete()%3B%0A%0A%20%20%20%20%20%20%20%20private%20static%20void%20AppServiceConnection_ServiceClosed(AppServiceConnection%20sender%2C%20AppServiceClosedEventArgs%20args)%20%3D%26gt%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20_backgroundTaskDeferral.Complete()%3B%0A%0A%20%20%20%20%7D%0A%7D%3C%2FPRE%3E%0A%3CP%3EAnd%20to%20launch%20the%20full%20trust%20process%20when%20UWP%20app%20was%20launched%2C%20so%2C%20let's%20add%20following%20code%20at%20OnLaunched%20method%20of%20App%20class.%3C%2FP%3E%0A%3CPRE%3Eprotected%20override%20void%20OnLaunched(LaunchActivatedEventArgs%20e)%0A%7B%0A%20%20%20%20...%0A%20%20%20%20if%20(rootFrame%20%3D%3D%20null)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20_%20%3D%20SerialPortService.LaunchWin32AppAsync()%3B%20%2F%2F%20Launch%20the%20full%20trust%20process%0A%20%20%20%20%20%20%20%20%2F%2F%20Create%20a%20Frame%20to%20act%20as%20the%20navigation%20context%20and%20navigate%20to%20the%20first%20page%0A%20%20%20%20%20%20%20%20rootFrame%20%3D%20new%20Frame()%3B%0A%20%20%20%20%20%20%20%20...%0A%20%20%20%20%7D%0A%20%20%20%20...%0A%7D%0A%3C%2FPRE%3E%0A%3CP%3EAt%20finally%2C%20implements%20OnBackgroundActivated%20method%20of%20App%20class%2C%20like%20below%3A%3C%2FP%3E%0A%3CPRE%3Eprotected%20override%20void%20OnBackgroundActivated(BackgroundActivatedEventArgs%20args)%0A%7B%0A%20%20%20%20base.OnBackgroundActivated(args)%3B%0A%0A%20%20%20%20if%20(args.TaskInstance.TriggerDetails%20is%20AppServiceTriggerDetails)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20set%20up%20app%20service%0A%20%20%20%20%20%20%20%20SerialPortService.AcceptAppServiceConnection(args.TaskInstance)%3B%0A%20%20%20%20%7D%0A%7D%0A%3C%2FPRE%3E%0A%3CP%3EOK%2C%20all%20preparing%20process%20is%20finished!!%20Create%20a%20page%20using%20SeralPortService%20class.%3C%2FP%3E%0A%3CP%3EIt%20is%20same%20as%20common%20programming%20of%20UWP.%20if%20you%20interested%20about%20that%2C%20then%20please%20see%20following%20github%20repository's%20using-getportnames%20branch.%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Frunceel%2FVirtualCOMPortApp%2Ftree%2Fusing-getportnames%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Frunceel%2FVirtualCOMPortApp%2Ftree%2Fusing-getportnames%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20app's%20result%20is%20as%20below%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20514px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F121852i39F90454A03AF8B1%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22vcom.gif%22%20title%3D%22vcom.gif%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EThe%20app%20can%20get%20list%20of%20port%20names!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%224%22%3E%3CSTRONG%3EWrap%20up%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EUWP%20apps%20have%20some%20limitations%20like%20getting%20port%20names%20of%20virtual%20COM%20port.%3C%2FP%3E%0A%3CP%3EIn%20those%20case%2C%20there%20are%20something%20to%20be%20able%20to%20resolve%20issues%20using%20Desktop%20Bridge%20technology%20written%20in%20this%20article.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThank%20you.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Microsoft

At previous article, I wrote a limitation to use System.IO.Ports.SerialPort class on UWP.

How to access virtual COM port from UWP app

 

But, there is a limitation. SerialPort class on UWP can't list ports using GetPortNames method. 
If the method was used, then PlatformNotSupportedException would be occurred.

GetPortNames method can't call on UWP. But, the method is available on .NET Framework. 

Yes, UWP app can launch a full trust process that is in same package, and then UWP can communicate to the full trust process.

At DesktopBridgeToUWP-Samples repository, there is a sample program about this.

AppService Bridge Sample

 

I thought that using GetPortNames method is good example. So, I created a sample program that is changed previous article's sample.

 

Key technologies

Windows Application Package Project(MSIX)

First, we can create a msix package file that includes a UWP app and a Win32 app using Windows Application Package Project.

The following picture is solution explorer of the sample app.

コメント 2019-06-27 125811.jpg

  • VirtualCOMPortApp (Universal Windows)
    This is a UWP app project. It communicates to VirtualCOMPortApp.Win32 to call GetPortNames method.
  • VirtualCOMPortApp.Package
    This is a Windows Application Package Project. This is to create a msix package file that includes the UWP app and the Win32 app.
  • VirtualCOMPortApp.Win32
    This is a Console App(.NET Framework). This is to call GetPortNames method.

App Services

App Services(It is not Azure service!) is one of UWP features to provide functions to other apps from UWP.

Use app services and extensions

 

In this sample app, the feature is used to communicate between the UWP app and the Win32 app.

UWP app side is hosting App Service, and Win32 app connect to it, and then they can send / receive messages together using AppServiceConnection class.

 

Of course, you can choice other methods to communicate between UWP app and Win32 app like ApplicationData.Current.LocalSettings.

 

Using Windows 10 APIs from .NET Framework app

You can use Windows 10 APIs from .NET Framework app. It is written on following document:

Call UWP APIs in desktop apps

 

There are a few steps, however we can do that more easier using Microsoft.Windows.SDK.Contracts NuGet package. In this sample, I'm using the package.

There is just one step. it is to add the package to .NET Framework project. 

 

Launch full trues process from UWP app

There is FullTrustProcessLauncher Class that is to launch full trust win32 app from UWP app. It is provided through Windows Desktop Extension SDK. 

If you want to use it, please add the reference to UWP project.

コメント 2019-06-27 131942.jpg

Let's get list of port names.

Base of sample program is a sample program of a previous article.

The repository is here: https://github.com/runceel/VirtualCOMPortApp

 

I added two projects to the solution.

コメント 2019-07-02 143937.jpg

  • VirtualCOMPortApp.Package project
    This is a Windows Application Package project. I added VirtualCOMPortApp project and VirtualCOMPortApp.Win32 project to Applications node. And then set VirtualCOMPortApp as entry point.
  • VirtualCOMPortApp.Win32 project
    This is a Console App(.NET Framework) project. I added Microsoft.Windows.SDK.Contracts NuGet package to this.

To launch the win32 app as full trust process, I added desktop:Extension tag to Package.appxmanifest of VirtualCOMPortApp.Package project, like below:

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

And to use app service, I added an uap:Extension tag to Package.appxmanifest of VirtualCOMPortApp.Package project, like below:

<uap:Extension Category="windows.appService">
  <uap:AppService Name="InProcessAppService" />
</uap:Extension>

Add implementation to get port names to VirtualCOMPortApp.Win32, and communicate to UWP process using AppServiceConnection.

To connect the app service:

var conn = new AppServiceConnection();
conn.PackageFamilyName = Package.Current.Id.FamilyName;
conn.AppServiceName = "InProcessAppService";
conn.RequestReceived += Conn_RequestReceived;
conn.ServiceClosed += Conn_ServiceClosed;
await conn.OpenAsync();

Implements RequestReceived event handler to communicate to UWP process:

private static async void Conn_RequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
{
    var d = args.GetDeferral();
    var requestMessage = args.Request.Message;
    if (requestMessage.TryGetValue("command", out var commandName))
    {
        switch((string)commandName)
        {
            case "GetPortNames":
                {
                    await args.Request.SendResponseAsync(new ValueSet
                    {
                        ["portNames"] = JsonConvert.SerializeObject(SerialPort.GetPortNames()),
                    });
                    break;
                }
            default:
                // noop
                break;
        }
    }
    d.Complete();
}

And then I created a wrapper class to call the full truest process from UWP, like below:

...
namespace VirtualCOMPortApp
{
    public static class SerialPortService
    {
        private static AppServiceConnection _appServiceConnection;
        private static BackgroundTaskDeferral _backgroundTaskDeferral;
        private static readonly TaskCompletionSource<object> _initialized = new TaskCompletionSource<object>();

        private static Task WaitForInitializedAsync() => _initialized.Task;

        // launch the full trust process
        public static Task LaunchWin32AppAsync() => FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync().AsTask();
        // set up app service
        public static void AcceptAppServiceConnection(IBackgroundTaskInstance instance)
        {
            instance.Canceled += AppServiceCanceled;
            var appService = (AppServiceTriggerDetails)instance.TriggerDetails;
            _backgroundTaskDeferral = instance.GetDeferral();
            _appServiceConnection = appService.AppServiceConnection;
            _appServiceConnection.ServiceClosed += AppServiceConnection_ServiceClosed;
            _initialized.SetResult(null);
        }

        // get port names using AppServiceConnection
        public static async Task<string[]> GetPortNamesAsync()
        {
            await WaitForInitializedAsync();
            var response = await _appServiceConnection.SendMessageAsync(new ValueSet
            {
                ["command"] = "GetPortNames",
            });

            if (response.Status != AppServiceResponseStatus.Success)
            {
                throw new InvalidOperationException($"{response.Status}");
            }

            return JsonConvert.DeserializeObject<string[]>(response.Message["portNames"] as string ?? "[]");
        }

        // closing process
        private static void AppServiceCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason) =>
            _backgroundTaskDeferral.Complete();

        private static void AppServiceConnection_ServiceClosed(AppServiceConnection sender, AppServiceClosedEventArgs args) =>
            _backgroundTaskDeferral.Complete();

    }
}

And to launch the full trust process when UWP app was launched, so, let's add following code at OnLaunched method of App class.

protected override void OnLaunched(LaunchActivatedEventArgs e)
{
    ...
    if (rootFrame == null)
    {
        _ = SerialPortService.LaunchWin32AppAsync(); // Launch the full trust process
        // Create a Frame to act as the navigation context and navigate to the first page
        rootFrame = new Frame();
        ...
    }
    ...
}

At finally, implements OnBackgroundActivated method of App class, like below:

protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args)
{
    base.OnBackgroundActivated(args);

    if (args.TaskInstance.TriggerDetails is AppServiceTriggerDetails)
    {
        // set up app service
        SerialPortService.AcceptAppServiceConnection(args.TaskInstance);
    }
}

OK, all preparing process is finished!! Create a page using SeralPortService class.

It is same as common programming of UWP. if you interested about that, then please see following github repository's using-getportnames branch.

https://github.com/runceel/VirtualCOMPortApp/tree/using-getportnames

 

The app's result is as below:

vcom.gif

The app can get list of port names!

 

Wrap up

UWP apps have some limitations like getting port names of virtual COM port.

In those case, there are something to be able to resolve issues using Desktop Bridge technology written in this article.

 

Thank you.