How to use uwp api in win32 program

w l 81 Reputation points
2021-02-22T09:06:58.247+00:00

win32程序如何使用uwp的api

我的win32程序是一个游戏, 语言是c++,想上架微软商店. 我的程序里有打开shell执行命令的功能,还有通过ShellExecute打开浏览器的功能.

我在 https://learn.microsoft.com/zh-cn/windows/msix/desktop/desktop-to-uwp-prepare 这里看到,uwp是不允许调用ShellExecute执行外部程序的:

应用程序启动一个实用工具来执行任务。 避免启动 PowerShell 和 Cmd.exe 等命令实用工具。 实际上,如果用户将应用程序安装在运行 Windows 10 S 的系统上,则应用程序根本无法启动这些实用工具。 这可能会使应用程序无法提交到 Microsoft Store,因为提交到 Microsoft Store 的所有应用都必须与 Windows 10 S 兼容。

我把下面的代码贴到我的代码里报错:

 var uri = new Windows.Foundation.Uri(link.href);  
            var options = new Windows.System.LauncherOptions();  
            // Launch the URI with a warning prompt  
            options.treatAsUntrusted = true;  
            // Launch the URI  
            Windows.System.Launcher.launchUriAsync(uri, options).then(  
                function (success) {  
                    if (success) {  
                        // URI launched  
                    } else {  
                        // URI launch failed  
                    }  
                });  

找了很多的帖子添加了头文件:

#include <winrt/Windows.Foundation.h>  
#include <winrt/Windows.System.h>  
  
using namespace winrt;  
using namespace Windows::Foundation;  
using namespace Windows::System;  
  

添加完毕开始编译,报错提示要使用c++17才行. 但是我的项目是c++11,这个不能随便改动. 请问怎么办? 是我使用方式不对吗?

------

The following is google translation

How to use uwp api in win32 program

My win32 program is a game, the language is c++, and I want to put it on the Microsoft Store. My program has the function of opening the shell to execute commands, and the function of opening the browser through ShellExecute.

I saw at https://learn.microsoft.com/zh-cn/windows/msix/desktop/desktop-to-uwp-prepare that uwp does not allow calling ShellExecute to execute external programs:

Your application starts a utility to perform tasks. Avoid starting command utilities such as PowerShell and Cmd.exe. In fact, if users install your application onto a system that runs the Windows 10 S, then your application won’t be able to start them at all. This could block your application from submission to the Microsoft Store because all apps submitted to the Microsoft Store must be compatible with Windows 10 S.

I posted the following code to my code to report an error:

var uri = new Windows.Foundation.Uri(link.href);  
            var options = new Windows.System.LauncherOptions();  
            // Launch the URI with a warning prompt  
            options.treatAsUntrusted = true;  
            // Launch the URI  
            Windows.System.Launcher.launchUriAsync(uri, options).then(  
                function (success) {  
                    if (success) {  
                        // URI launched  
                    } else {  
                        // URI launch failed  
                    }  
                });  

I found a lot of posts and added header files:

#include <winrt/Windows.Foundation.h>  
#include <winrt/Windows.System.h>  
  
using namespace winrt;  
using namespace Windows::Foundation;  
using namespace Windows::System;  
  

After adding it and start compiling, the error message prompts you to use c++17. But my project is c++11, this can’t be changed casually. What should I do? Is it the wrong way I use it?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,427 questions
{count} votes

4 answers

Sort by: Most helpful
  1. w l 81 Reputation points
    2021-02-24T08:18:28.497+00:00

    还有个问题,在符合uwp规范的前提下,在同目录下的多个执行程序怎么样才能相互调用.


    There is another question, on the premise of conforming to the uwp specification, how can multiple execution programs in the same directory call each other.


  2. w l 81 Reputation points
    2021-02-24T10:27:42.627+00:00

    reply:
    "I can reproduce your problem, there is a problem with the assignment of your url variable. In other words, your url variable is invalid. Try the following code,"

    ------------------

        WINDOWS_SDK_API bool UwpOpenWeb(const wchar_t* url) {  
            //auto tt = gcnew Platform::String(url);  
            const wchar_t* yyy = url;  
            auto tt = ref new Platform::String(yyy);  
            auto uri = ref new Windows::Foundation::Uri(tt);  
            //auto options = ref new Windows::System::LauncherOptions();  
            //// Launch the URI with a warning prompt  
            //options->TreatAsUntrusted = true;  
            //options->DisplayApplicationPicker = true;  //如果不设置此值,那么将不会显示app选择界面  
            // Launch the URI  
            auto result = Windows::System::Launcher::LaunchUriAsync(uri);  
            return true;  
        }  
    

    71459-image.png

    UwpOpenWeb is an interface to be written in dll.

    As shown in the figure, UwpOpenWeb is called by the win32 program, and the error "Platform::InvalidArgumentException ^," still appears

    I changed it to "const wchar_t* yyy = L"www.baidu.com";" which is good.

    -----
    UwpOpenWeb是要写在dll里的一个接口.

    如图所示,UwpOpenWeb被win32程序调用, 还是出现 错误"Platform::InvalidArgumentException ^,"

    我改成" const wchar_t* yyy = L"www.baidu.com";" 就是好的

    0 comments No comments

  3. w l 81 Reputation points
    2021-02-24T10:48:37.82+00:00

    How to get the installation directory of the program, for example, after obtaining the installation directory of the app, execute other programs in the running directory.


    请问下怎么获取程序的安装目录,比如获取到app的安装目录后执行运行目录下的其他程序.

    0 comments No comments

  4. Strive Sun-MSFT 426 Reputation points
    2021-02-25T01:42:21.727+00:00

    Hello, @w l

    How to get the installation directory of the program, for example, after obtaining the installation directory of the app, execute other programs in the running directory.

    GetModuleFileName can retrieve the fully qualified path for the file that contains the specified module.

    Some code for reference,

    WCHAR path[MAX_PATH];  
    GetModuleFileNameW(NULL, path, MAX_PATH);  
    PathRemoveFileSpecW(path);  
    PathAppendW(path, L"gifs\\panda.jpg");  
    

    Hope to help you.

    ----------

    Thank you!

    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments