我无法在 UWP 应用程序和 Win32 应用程序之间进行通信

I can't communicate between UWP app and Win32 app

Program.cs

using System;
using System.IO.MemoryMappedFiles;
using Windows.Storage;
using System.Threading;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace XBOXGameBarPoC_Win32
{
    static class Program
    {
        static void Main()
        {

            while (true)
            {
                using (var file = MemoryMappedFile.OpenExisting("XboxGameBarPoc_SharedMemory"))
                {

                    using (var writer = file.CreateViewAccessor(0, 10))
                    {
                        
                        writer.Write(0, 500);
                    }


                }
            }
        }
    }
}

MainPage.xaml.cs

using System.IO.MemoryMappedFiles;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using System.Threading;
using Microsoft.Graphics.Canvas;
using Windows.UI;
using Windows.UI.Popups;
using System;

namespace XBOXGameBarPoC_UWP
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            MemoryMappedFile.CreateOrOpen("XboxGameBarPoc_SharedMemory", 0x644, MemoryMappedFileAccess.ReadWriteExecute);
        }

        private void canvasSwapChainPanel_Loaded(object sender, RoutedEventArgs e)
        {
            CanvasDevice device = CanvasDevice.GetSharedDevice();
            canvasSwapChainPanel.SwapChain = new CanvasSwapChain(device, (float)Window.Current.CoreWindow.Bounds.Width, (float)Window.Current.CoreWindow.Bounds.Height, 96);

            Thread renderThread = new Thread(new ParameterizedThreadStart(RenderThread));
            renderThread.Start(canvasSwapChainPanel.SwapChain);
        }

        private void RenderThread(object parameter)
        {
            CanvasSwapChain swapChain = (CanvasSwapChain)parameter;
            using (MemoryMappedFile memoryMappedFile = MemoryMappedFile.OpenExisting("XboxGameBarPoc_SharedMemory"))
            {
                using (MemoryMappedViewAccessor viewAccessor = memoryMappedFile.CreateViewAccessor())
                {
                    System.Numerics.Vector2 CenterTop;
                    CenterTop.X = 1920 / 2.0f;
                    CenterTop.Y = 0.0f;
                    int count = 0;
                    while (true)
                    {
                        using (CanvasDrawingSession ds = canvasSwapChainPanel.SwapChain.CreateDrawingSession(Colors.Transparent))
                        {
                            
                            viewAccessor.Read<int>(0, out count);
                            ds.DrawRectangle(count, 300, 150, 100, Colors.Red, 5);
                            canvasSwapChainPanel.SwapChain.Present();
                        }
                    }
                }
            }
        }
        struct Box
        {
            public float X;
            public float Y;
            public float Width;
            public float Height;
        }
    }
}

这个问题真的扼杀了我的全部动力。这些代码没有任何反应。我正在尝试在 Win32-UWP 应用程序之间进行通信。 Win32 将不断发送信息,UWP 将根据该信息进行绘制。我几乎尝试了一切,但仍然失败。如果需要,我也可以附加项目。

I can't communicate between UWP app and Win32 app

MemoryMappedFile 与 UWP 平台不兼容。对于进程间通信,我们建议您使用 App 服务或 Loopback 这里是官方的document,您可以参考。

我发现您正在使用共享内存进行进程间通信,请参考此 sharing named objects 可以在 UWP 平台中使用。