1

Is it possible to open Windows' settings dialogs using Win32 API calls from C? Such as the monitor settings (the one that allows you to set the monitor's resolution), and the network settings (the one that allows you to set the Wifi settings).

If it is possible, how to do that?

Or, is this not possible at all, and the user has to manually open them?

IInspectable
  • 46,945
  • 8
  • 85
  • 181
Andreas
  • 9,245
  • 9
  • 49
  • 97
  • [Launch the Windows Settings app](https://learn.microsoft.com/en-us/windows/uwp/launch-resume/launch-settings-app). – IInspectable Mar 07 '22 at 18:50
  • Most of the control panel can be opened using `Process.Start` (or [`CreateProcessW`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw) for Win32 applications). See [How to open "Network Connections" window programmatically](https://stackoverflow.com/questions/5032667/how-to-open-network-connections-window-programmatically). The process names you're looking for are likely ["control desk.cpl,,3"](https://stackoverflow.com/a/5162437/14956277) and "netcpl.cpl". – D M Mar 07 '22 at 18:50
  • 1
    Win32 is the native API and is not necessary to ask "is it possible". If computer is able to do something under Windows, you can do it from Win32. – i486 Mar 07 '22 at 18:51
  • 3
    @i48 Feel free to read *"is it possible"* as *"is it possible using a supported API"*. And no, the Win32 API is **not** the native API. It's the API of the Win32 subsystem. There are things you can do with the Native API that aren't possible using the Win32 API. – IInspectable Mar 07 '22 at 18:55
  • @IInspectable: Can that Windows settings app be launched from a plain C app too? The link that you posted only shows how to launch it from C# and C++/WinRT. – Andreas Mar 07 '22 at 19:31
  • I don't know whether you can pass the listed URI's into `ShellExecuteEx`, which would be immediately accessible from C code. If you do have to call `LaunchUriAsync` instead, that'd be problematic. While the API can be called from a desktop application, to my knowledge there are no C projections readily available. You'd have to call that from C++ instead. – IInspectable Mar 07 '22 at 19:39
  • 1
    @i486 "*If computer is able to do something under Windows, you can do it from Win32*" - not necessarily. A lot of Windows' internal functionality is behind hidden/restricted APIs that Windows can use internally but user-mode apps can't access. – Remy Lebeau Mar 07 '22 at 20:17
  • 1
    I added the [tag:c] tag since that is relevant here. The same question would have a different answer for C++. – IInspectable Mar 08 '22 at 09:25

1 Answers1

2

Launch the Windows Settings app explains how to pull up the Windows Settings app using the ms-settings: URI scheme. It also lists the supported URIs, including the ones this question is asking for (ms-settings:network-wifi and ms-settings:display).

While the documentation proposes using the Windows Runtime API Launcher.LaunchUriAsync this is a fair bit complex with C (as opposed to C++). Since the URIs can be invoked from the command prompt using the start command (e.g. start ms-settings:display), it's reasonable to assume that ShellExecuteExW can handle the ms-settings: URI scheme as well.

And indeed, this does appear to work:

#include <Windows.h>

#include <stdio.h>

int main() {
    SHELLEXECUTEINFOW sei = {
        .cbSize = sizeof(sei),
        .hwnd = NULL,
        .lpVerb = L"open",
        .lpFile = L"ms-settings:display",
        //.lpFile = L"ms-settings:network-wifi",
        .nShow = SW_SHOWNORMAL,
    };
    if (!ShellExecuteExW(&sei))
    {
        printf("Failed with error code %d", GetLastError());
    }
}

I wasn't able to find any documentation that specifies this behavior, so this may well be an unsupported implementation detail. I will also mention that while SHELLEXECUTEINFOW has an lpClass field that can be used to specify a URI protocol scheme, none of my iterations to use it worked for the ms-settings: URI scheme.

IInspectable
  • 46,945
  • 8
  • 85
  • 181