Windows S-Mode is a mode of Windows which restricts programs to those which came from the Microsoft Store. You might have a program that is available from the Microsoft Store (therefore is allowed to run in S-Mode), and you want to know whether to suggest that the user install a companion program that is not allowed in S-Mode. How can a program detect whether the system is running in S-Mode?
You can detect S-Mode by using the Windows. class.
// C#
using Windows.System.Profile;
if (WindowsIntegrityPolicy.IsEnabled) {
// System is in S-Mode
if (WindowsIntegrityPolicy.CanDisable) {
// System is in S-Mode but can be taken out of S-Mode
suggestCompanion = true;
} else {
// System is locked into S-Mode
suggestCompanion = false;
}
} else {
// System is not in S-Mode
suggestCompanion = true;
}
// C++/WinRT
#include <winrt/Windows.System.Profile.h>
namespace winrt
{
using namespace winrt::Windows::System::Profile;
}
if (winrt::WindowsIntegrityPolicy::IsEnabled()) {
// System is in S-Mode
if (winrt::WindowsIntegrityPolicy::CanDisable()) {
// System is in S-Mode but can be taken out of S-Mode
suggestCompanion = true;
} else {
// System is locked into S-Mode
suggestCompanion = false;
}
} else {
// System is not in S-Mode
suggestCompanion = true;
}
// JavaScript
let WindowsIntegrityPolicy = Windows.System.Profile.WindowsIntegrityPolicy;
if (WindowsIntegrityPolicy.isEnabled) {
// System is in S-Mode
if (WindowsIntegrityPolicy.canDisable) {
// System is in S-Mode but can be taken out of S-Mode
suggestCompanion = true;
} else {
// System is locked into S-Mode
suggestCompanion = false;
}
} else {
// System is not in S-Mode
suggestCompanion = true;
}
// C++/CX
using namespace Windows::System::Profile;
if (WindowsIntegrityPolicy::IsEnabled) {
// System is in S-Mode
if (WindowsIntegrityPolicy::CanDisable) {
// System is in S-Mode but can be taken out of S-Mode
suggestCompanion = true;
} else {
// System is locked into S-Mode
suggestCompanion = false;
}
} else {
// System is not in S-Mode
suggestCompanion = true;
}
// C++/WRL
#include <wrl/client.h>
#include <wrl/wrappers/corewrappers.h>
#include <Windows.System.Profile.h>
#include <wil/result_macros.h>
namespace WRL
{
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
}
namespace ABI
{
using namespace ABI::Windows::System::Profile;
}
WRL::ComPtr<ABI::IWindowsIntegrityPolicyStatics> statics;
THROW_IF_FAILED(::RoGetActivationFactory(
WRL::HStringReference(RuntimeClass_Windows_System_Profile_WindowsIntegrityPolicy).Get(),
IID_PPV_ARGS(&statics)));
boolean isEnabled;
THROW_IF_FAILED(statics->get_IsEnabled(&isEnabled));
if (isEnabled) {
// System is in S-Mode
boolean canDisable;
THROW_IF_FAILED(statics->get_CanDisable(&canDisable));
if (canDisable) {
// System is in S-Mode but can be taken out of S-Mode
suggestCompanion = true;
} else {
// System is locked into S-Mode
suggestCompanion = false;
}
} else {
// System is not in S-Mode
suggestCompanion = true;
}
@LB Aaand… you missed the point again. I was asking about a C example code with standard Windows API, not WinRT or C#. In other words, how to check the S mode without those.
Like it or not, that stuff is part of the Windows API, even if it’s not in the “simple” Win32 subset. It’s all accessible via pure C.
Maybe I’m missing something big, but I find the following code much more readable…
Or it ‘s just a matter of taste, anyway…
Speaking of readability, the second s_mode check is redundant, you can just do
No, that’s not readability.
What happened to plain C version?
Or, MFC!
(probably would work out to be the same, more or less?)
Is that even possible? In order to be in S-Mode you have to be running code from the MS Store which means UWP, C++/CX, etc. I don't know that you can build a pure C program that can then be hosted in the MS Store simply because it requires some WRL components which are inherently class-based. I'm not sure how the JS version works though...
If you're able to run a C program then that would, it seems, imply you are not in S-Mode because you can't turn it off and then back on. However I wonder if the C++/WRL...
@Raymond Chen
Your sample code seems to be incorrect / incomplete. The fine manual suggests to also check:
<code>
Because apparently a boolean flag named <code> doesn't mean you can actually do that *rolls eyes* -- so typical of Microsoft devs to make stuff like that, totally not your fault for missing it.
I think this is a perfect case where a single enum should have been used instead of two boolean flags -- something like SModeControl with { Disabled = 0, BlockedByPolicy, Enabled } where you only have to compare the value to Enabled to know whether you can change it.
What I meant is “What happened to plain C version of example code“. If I remember correctly Raymond was quite adamant about always using the simplest possible code for examples.
UWP hasn’t required C++/CX for years now, C++/WinRT has been a pure standard C++ option for a long time and you can adapt it to pure C as well, it’s just more effort. SDL2 is a pure C library that supports UWP for example.