简体   繁体   中英

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++. So I wanna create a uwp class library to call LaunchUriAsync and win32 call this library. I find an example and now I can load the library sucessfully, but GetProcAddress always returns null. Not sure whether it is feasible calling uwp class library in win32 console. Pls help me out. The project is at https://github.com/vincent1000/win32CallUwpLibrary The code is very simple:

UWP Classy Library:

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:

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? I have searched some methods but none works for me.

The solution is trivial: Simply call Launcher.LaunchUriAsync from your console application. The easiest route is to use 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:

#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. This needs to be replaced with a valid URI (eg "https://www.bing.com" ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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