How to check the Windows setting-Location permission is enabled or not for my Application using win32 C ++

67883078 41 Reputation points
2024-04-05T14:01:22.9633333+00:00

Hi,

How to check the Windows setting-Location permission is enabled or not from a Win32 C++ Application.

User's image

I would like to know the win32 API which can get the status about the Location setting, whether it's enabled for the application or not.

I

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
10,636 questions
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,423 questions
{count} votes

Accepted answer
  1. Xiaopo Yang - MSFT 11,496 Reputation points Microsoft Vendor
    2024-04-12T09:01:22.8866667+00:00

    This is an example code to do the same thing as I said in comment using a console CppWinRT project.

    #include "pch.h"
    #include <winrt/Windows.Devices.Geolocation.h>
    using namespace winrt;
    using namespace Windows::Foundation;
    using namespace winrt::Windows::Devices::Geolocation;
    int main()
    {
    	init_apartment();
    	Uri uri(L"http://aka.ms/cppwinrt");
    	printf("Hello, %ls!\n", uri.AbsoluteUri().c_str());
    	// Create geolocator object
    	Geolocator geolocator;
    	auto operation = geolocator.GetGeopositionAsync();
    	switch (operation.wait_for(TimeSpan(50000000)))
    	{
    	case AsyncStatus::Completed:
    		printf("result %d\n", operation.GetResults());
    		break;
    	case AsyncStatus::Canceled:
    		puts("canceled");
    		break;
    	case AsyncStatus::Error:
    	{
    		winrt::hresult h = operation.ErrorCode();
    		puts("failed");
    	}
    	break;
    	case AsyncStatus::Started:
    		puts("still running");
    		break;
    	}
    	// Make the request for the current position
    	auto status = geolocator.LocationStatus();
    }
    

    User's image

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Xiaopo Yang - MSFT 11,496 Reputation points Microsoft Vendor
    2024-04-18T01:23:51.31+00:00

    Hello @67883078,

    You may also use the ILocationPermissions interface to check the location capability. For more information, see Sensor API.


  2. 67883078 41 Reputation points
    2024-04-18T08:51:58.9833333+00:00

    Hi @Xiaopo Yang - MSFT

    Thanks for your suggestion.

    I have gone through the ILocationPermissions Microsoft documentation, it says this interface gets the location capability of the Windows Store app. I want to try with a win32 C++ application.

    I would like to know whether I can use the ILocationPermissions interface in a pure win32 C++ application.

    I found another way to check the location status using the WinRT:

    param::hstring capabilityName(L"Location");
    auto s = Windows::Security::Authorization::AppCapabilityAccess::AppCapability::Create(capabilityName);
    
    Windows::Security::Authorization::AppCapabilityAccess::AppCapabilityAccessStatus ss = s.CheckAccess();
    
    0 comments No comments