Xbox Gamebar SDK - Shutdown Windows

Austin Hanna 0 Reputation points
2023-08-03T23:32:14.7566667+00:00
Hello, I am making a Widget for Xbox Gamebar to shut down my computer without having to get up from my bed. I have loaded up the SDK and am able to build and deploy the sample widgets. However, when I attempt to use the code:

system("c:\\windows\\system32\\shutdown /s /t 0");

When the button is clicked, the terminal appears and then disappears, not completing my request. Am I using the wrong syntax? Do I need to #include any system libraries? Here is the full code, thanks.

#include "pch.h"
#include "Widget1.h"
#include "Widget1.g.cpp"

using namespace winrt;
using namespace winrt::Windows::UI::Xaml;

namespace winrt::WidgetSample::implementation
{
    Widget1::Widget1()
    {
        InitializeComponent();
    }

    int32_t Widget1::MyProperty()
    {
        throw hresult_not_implemented();
    }

    void Widget1::MyProperty(int32_t /* value */)
    {
        throw hresult_not_implemented();
    }

    void Widget1::MyButton_Click(IInspectable const&, RoutedEventArgs const&)
    {
        myButton().Content(box_value(L"Shutting down!"));
        system("c:\\windows\\system32\\shutdown /s /t 0");
        return;
    }
}
Developer technologies | Universal Windows Platform (UWP)
Developer technologies | C++
{count} votes

1 answer

Sort by: Most helpful
  1. Junjie Zhu - MSFT 21,646 Reputation points
    2023-08-10T09:19:48.8766667+00:00

    Hi @Austin Hanna ,

    Welcome to Microsoft Q&A!

    UWP cannot use system to execute the command. UWP contain BeginShutdown method, but it only applicable to IOT device. And it will not work for Win 11, if you do want to shut down device, we suggest you use desktop bridge to make desktop extension for UWP app and shut down computer with command shutdown /s /t 0.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.