簡體   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