【问题标题】:WinRT app calling win32 c++ dll sending parametersWinRT app调用win32 c++ dll发送参数
【发布时间】:2020-06-02 20:46:30
【问题描述】:

嘿,这是第一次使用 C++,所以请善待 :)

我已经制作了一个 WinRT 应用程序,由于某种类型的“沙盒”,它为那些应用程序做了我无法访问除此之外的任何东西(比如我想要访问的东西 - 我的桌面应用程序。

所以在这里和那里阅读我发现如果你制作一个 C++ Dll 并从 WinRT 应用程序调用它,那么这将允许你在“沙盒”之外进行调用。但是这里有个小问题。

我目前收到以下错误:

错误 CS1660 无法将 lambda 表达式转换为类型“字符串”,因为它不是委托类型。

在此处使用此代码:

void Header_Click(object sender, RoutedEventArgs e)
{
    DisplayFromDLL("tester", s =>
    {
        // response is returned in s - do what you want with it
    });
}

DisplayFromDLL 是它抛出该错误的地方。 “s”更是如此。

所以我必须调用 dll 的 C# 代码如下所示:

public sealed partial class GroupedItemsPage : Page
{
    [DllImport("Win32Project1.dll", EntryPoint = "DisplayFromDLL", CallingConvention = CallingConvention.StdCall)]
    public static extern void DisplayFromDLL(string name, String response);

    void Header_Click(object sender, RoutedEventArgs e)
    {
        DisplayFromDLL("tester", s =>
        {
            // response is returned in s
        });
    }

还有 C++ dll 代码:

extern "C"
{
    __declspec(dllexport) void DisplayFromDLL(const char name)
    {
        MessageBox("name is: " + name, "Msg title", MB_OK | MB_ICONQUESTION);
    }
}

所以一些帮助会很好地了解导致错误的原因以及修复它的情况可能是什么。

【问题讨论】:

  • 在同一进程中切换语言不会让您脱离沙箱,这很容易,不是吗?看来您正在使用 UWP(“通用 Windows 平台”)。 WinRT“只是”一个 API。如果你不想要沙盒,就不要使用 UWP,或者在沙盒之外开发另一个进程与之对话(通过某种机制)
  • 那么你有什么建议?我不得不留在 WinRT 的唯一原因是为了 Metro Live Tiles。如果我能在 WPF 中得到同样的东西,那么这将解决这个问题。我听说过很多关于“桥”的内容,但并没有真正理解这一切是如何运作的。
  • 有 WinRT(新的 Windows API)、UWP 平台(xbox、设备、沙盒等)和打包(msix、标识、沙盒使用)。桌面应用程序可以访问某些 WinRT API,有些则不能。有些需要包标识,有些不需要。所有这一切目前正在发生变化,未来显然是 WinUI3:blogs.windows.com/windowsdeveloper/2020/05/19/… 但现在,我相信您确实可以使用桌面应用程序中的实时磁贴 API(也许有包装,我不确定):blogs.windows.com/windowsdeveloper/2018/11/02/…跨度>

标签: c# c++ dll windows-runtime dllimport


【解决方案1】:

您的定义(在 DLL 中)、声明(在 c# 端)和实际调用根本不匹配。

您定义的函数需要一个字符,您的声明说它需要两个字符串,但您的调用提供了一个字符串和一个函数。

使所有这些都匹配。

【讨论】:

    【解决方案2】:

    我想我在这里找到了实现目标的好方法。

    我在 C# 中添加了一个简单的小型 Web 服务器,WinRT 可以调用它。执行此调用允许我在 WinRT 应用程序的沙盒外部运行我需要的任何东西。

    这是网络服务器 C# 代码(SimpleWebServer.cs):

    using System;
    using System.Net;
    using System.Text;
    using System.Threading;
    
    namespace smallWS
    {
        public class WebServer
        {
            private readonly HttpListener _listener = new HttpListener();
            private readonly Func<HttpListenerRequest, string> _responderMethod;
    
            public WebServer(string[] prefixes, Func<HttpListenerRequest, string> method)
            {
                if (!HttpListener.IsSupported)
                    throw new NotSupportedException("Needs Windows XP SP2, Server 2003 or later.");
    
                if (prefixes == null || prefixes.Length == 0)
                    throw new ArgumentException("prefixes");
    
                if (method == null)
                    throw new ArgumentException("method");
    
                foreach (string s in prefixes)
                    _listener.Prefixes.Add(s);
    
                _responderMethod = method;
                _listener.Start();
            }
    
            public WebServer(Func<HttpListenerRequest, string> method, params string[] prefixes)
                : this(prefixes, method) { }
    
            public void Run()
            {
                ThreadPool.QueueUserWorkItem((o) =>
                {
                    Console.WriteLine("Webserver running...");
                    try
                    {
                        while (_listener.IsListening)
                        {
                            ThreadPool.QueueUserWorkItem((c) =>
                            {
                                var ctx = c as HttpListenerContext;
                                try
                                {
                                    string rstr = _responderMethod(ctx.Request);
                                    byte[] buf = Encoding.UTF8.GetBytes(rstr);
                                    ctx.Response.ContentLength64 = buf.Length;
                                    ctx.Response.OutputStream.Write(buf, 0, buf.Length);
                                }
                                catch { }
                                finally
                                {
                                    ctx.Response.OutputStream.Close();
                                }
                            }, _listener.GetContext());
                        }
                    }
                    catch { }
                });
            }
    
            public void Stop()
            {
                _listener.Stop();
                _listener.Close();
            }
        }
    }
    

    还有 Program.cs:

    using System;
    using System.Diagnostics;
    using System.Linq;
    using System.Net;
    
    namespace smallWS
    {
        class Program
        {
            static void Main(string[] args)
            {
                WebServer ws = new WebServer(SendResponse, "http://localhost:8080/metroData/");
                ws.Run();
                Console.WriteLine("Webserver online. Press a key to quit.");
    
                Console.ReadKey();
                ws.Stop();
            }
    
            public static string SendResponse(HttpListenerRequest request)
            {
                Process.Start("C:\\windows\\system32\\notepad.exe");
                var dictionary = request.RawUrl.Replace("/metroData/", "").Replace("?", "").Split('&').ToDictionary(x => x.Split('=')[0], x => x.Split('=')[1]);
                return string.Format("<HTML><BODY>My web page.<br>{0}</BODY></HTML>", DateTime.Now);
            }
        }
    }
    

    现在是 GroupedItemsPage.xaml:

    String mainURL = "http://localhost:8080/metroData/?";
    
    async void Header_Click(object sender, RoutedEventArgs e)
        {
            var group = (sender as FrameworkElement).DataContext;
    
            HttpClient httpClient = new HttpClient();
            HttpResponseMessage response = await httpClient.GetAsync(new Uri(mainURL + "something=here&this=that"));
            string responseText = await response.Content.ReadAsStringAsync();
        }
    

    【讨论】:

      【解决方案3】:

      如果 UWP 沙盒的限制妨碍了您的设计,请开发一个桌面应用程序。对于UI设计上最接近UWP的平台,选择WPF。您仍然可以通过桌面桥将其发布到商店。

      【讨论】:

      • 正确,但总体而言,我需要 Metro Tiles 作为 UI,而且它似乎在 WinRT 应用程序以外的任何东西中都不起作用。
      • 从未尝试过,但this 建议您可以从桌面应用程序发布磁贴。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多