简体   繁体   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++. 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++. So I wanna create a uwp class library to call LaunchUriAsync and win32 call this library.所以我想创建一个 uwp class 库来调用 LaunchUriAsync 和 win32 调用这个库。 I find an example and now I can load the library sucessfully, but GetProcAddress always returns null.我找到了一个示例,现在我可以成功加载库,但 GetProcAddress 总是返回 null。 Not sure whether it is feasible calling uwp class library in win32 console.不确定在win32控制台中调用uwp class库是否可行。 Pls help me out.请帮帮我。 The project is at https://github.com/vincent1000/win32CallUwpLibrary The code is very simple:项目在https://github.com/vincent1000/win32CallUwpLibrary代码很简单:

UWP Classy Library: 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"));
    }
}
}

And Win32 Console:和 Win32 控制台:

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

Also, is any other method to call LaunchUriAsync in win32?另外,还有其他方法可以在 win32 中调用 LaunchUriAsync 吗? I have searched some methods but none works for me.我已经搜索了一些方法,但没有一个对我有用。

The solution is trivial: Simply call Launcher.LaunchUriAsync from your console application.解决方案很简单:只需从控制台应用程序调用Launcher.LaunchUriAsync The easiest route is to use C++/WinRT .最简单的方法是使用C++/WinRT Assuming that you are using Visual Studio with the C++/WinRT extension installed, create a "Windows Console Application (C++/WinRT)" , and replace the wizard generated code with this:假设您使用安装了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();
}

This will compile, but fail at runtime due to "www.bing.com" not being a valid URI.这将编译,但由于"www.bing.com"不是有效的 URI 而在运行时失败。 This needs to be replaced with a valid URI (eg "https://www.bing.com" ).这需要替换为有效的 URI(例如"https://www.bing.com" )。

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

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