WinRT 应用调用 win32 c++ dll 发送参数

WinRT app calling win32 c++ dll sending parameters

本文关键字:参数 dll c++ 应用 调用 win32 WinRT      更新时间:2023-10-16

嘿,这是第一次与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);
}
}

因此,了解导致错误的哪一方以及修复错误的情况会很棒。

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

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

使所有这些匹配。

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

我在 C# 中添加了一个 WinRT 可以调用的简单小型 Web 服务器。通过执行此调用,我可以在 WinRT 应用程序的沙盒之外运行所需的任何内容。

这是 Web 服务器 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();
}
}
}

还有该计划.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();
}

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