繁体   English   中英

如何在win32 C++控制台应用程序中调用uwp class库

[英]how to call uwp class library in win32 C++ console application

I wanna call a uwp app with Uri in a win32 C++ console application.The first thing I thought is using LaunchUriAsync, but I couldn't find Windows.System.Launcher.LaunchUriAsync in win32 C++. 所以我想创建一个 uwp class 库来调用 LaunchUriAsync 和 win32 调用这个库。 我找到了一个示例,现在我可以成功加载库,但 GetProcAddress 总是返回 null。 不确定在win32控制台中调用uwp class库是否可行。 请帮帮我。 项目在https://github.com/vincent1000/win32CallUwpLibrary代码很简单:

UWP 经典库:

namespace ExportedCodeSolution 
{
public class Class1
{
    [DllExport(ExportName = "callUri", CallingConvention = CallingConvention.StdCall)]
    static public async void callUri()
    {
        Console.WriteLine("call URI start");
        await Launcher.LaunchUriAsync(new Uri("www.bing.com"));
    }
}
}

和 Win32 控制台:

using CallUriFn = void(__stdcall*) ();
int main()
{
HMODULE mod = LoadLibraryA("ExportedCodeSolution.dll");
CallUriFn fn = reinterpret_cast<CallUriFn>(GetProcAddress(mod, "callUri"));
fn();
}

另外,还有其他方法可以在 win32 中调用 LaunchUriAsync 吗? 我已经搜索了一些方法,但没有一个对我有用。

解决方案很简单:只需从控制台应用程序调用Launcher.LaunchUriAsync 最简单的方法是使用C++/WinRT 假设您使用安装了C++/WinRT 扩展的 Visual Studio,创建一个“Windows 控制台应用程序 (C++/WinRT)” ,并将向导生成的代码替换为:

#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.System.h>

using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::System;

int main()
{
    init_apartment();

    Uri uri(L"www.bing.com");
    Launcher::LaunchUriAsync(uri).get();
}

这将编译,但由于"www.bing.com"不是有效的 URI 而在运行时失败。 这需要替换为有效的 URI(例如"https://www.bing.com" )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM