question

wl-1660 avatar image
0 Votes"
wl-1660 asked ·

How to use uwp api in win32 program

win32程序如何使用uwp的api

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

我在 https://docs.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://docs.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?

winapi-general
· 4
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

wl-1660 avatar image wl-1660 StriveSun-MSFT ·

I have tried and can open the browser with LaunchUriAsync. Thank you. But I have a new problem.

71476-image.png

As shown in the figure, how to solve this problem, if you use /clr, you cannot use /zw and /zi

I want to encapsulate an interface UwpOpenWeb, which can open the connection through a variable (const wchar_t*), and cannot write constants.



我已经尝试,可以用LaunchUriAsync打开浏览器了. 谢谢. 但是我有一个新的问题.

如上图所示. 这个问题该怎么解决, 如果使用/clr, 则无法使用/zw 和 /zi

我要封装一个接口UwpOpenWeb,可以通过一个变量(const wchar_t*)来打开连接,不能写死常量.


0 Votes 0 ·
image.png (67.0 KiB)

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,

 auto tt = ref new Platform::String(L"https://blogs.windows.com/buildingapps//");



0 Votes 0 ·
Show more comments
wl-1660 avatar image
0 Votes"
wl-1660 answered ·

还有个问题,在符合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.

· 3 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

CreateProcess can call each other. And it can support uwp app and desktop app.

71552-image.png


0 Votes 0 ·
image.png (4.9 KiB)
wl-1660 avatar image wl-1660 StriveSun-MSFT ·

Thank you, then I use CreateProcess to open the browser.

0 Votes 0 ·
wl-1660 avatar image wl-1660 StriveSun-MSFT ·

Thank you, then I use CreateProcess to open the browser.

0 Votes 0 ·
wl-1660 avatar image
0 Votes"
wl-1660 answered ·

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";" 就是好的




image.png (35.7 KiB)
· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

wl-1660 avatar image
0 Votes"
wl-1660 answered ·

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的安装目录后执行运行目录下的其他程序.

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

StriveSun-MSFT avatar image
0 Votes"
StriveSun-MSFT answered ·

Hello, @wl-1660

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.


· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.